trait FromInner<T> {
fn from_inner(inner: T) -> Self;
}
trait ToInner<T> {
fn to_inner(self) -> T;
}
#[derive(FromInner, ToInner)]
struct Wrapper(u32);
// derived impl
impl FromInner<u32> for Wrapper {
fn from_inner(inner: u32) -> Self {
Self(inner)
}
}
impl ToInner<u32> for Wrapper {
fn to_inner(self) -> u32 {
self.0
}
}
The reason for this is that I want to implement a trait for checking and initializing like this:
struct CheckError;
trait Check<T>: Sized + FromInner<T> {
fn check(inner: &T) -> Result<(), CheckError>;
fn from_checked(inner: T) -> Result<Self, CheckError> {
Self::check(&inner)?;
Ok(Self::from_inner(inner))
}
}