Idea: trait FromInner & ToInner and derive for one field tuple struct

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))
    }
}

For that use case the From<T> trait should be enough. Are you thinking on some additional meaning?

Check's definition doesn't sound right. To implement a checked fallible conversion (the Check trait) you also need to implement an infallible one (the FromInner trait)?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.