#convert #traits #converting #define #external #problem #solve

convert_traits

Define your own conversion traits to solve the problem of converting two external types without using new types

3 stable releases

new 1.1.1 Oct 29, 2024
1.0.1 Oct 21, 2024

#448 in Rust patterns

Download history 279/week @ 2024-10-21

279 downloads per month
Used in cubix

MIT license

12KB
146 lines

convert_traits

Define your own conversion traits to solve the problem of converting two external types without using new types.

Crates.io Docs License

Generate a series of conversion traits with a specified prefix based on: ::std::convert::AsRef, ::std::convert::AsMut, ::std::convert::From, ::std::convert::Into, ::std::convert::TryFrom and ::std::convert::TryInto.

Install

Run the following Cargo command in your project directory:

cargo add convert_traits

Or add the following line to your Cargo.toml:

convert_traits = "1"

Example

#[cfg(test)]
#[allow(non_local_definitions)]
mod tests {
    # extern crate convert_traits;

    use convert_traits::my_convert;

    #[derive(Debug, PartialEq)]
    struct A(B);
    #[derive(Debug, PartialEq)]
    struct B;

    my_convert!(a_Bc);

    #[test]
    fn test_as_ref() {
        impl ABcAsRef<B> for A {
            fn a_bc_as_ref(&self) -> &B {
                &self.0
            }
        }
        assert_eq!(&A(B).0, A(B).a_bc_as_ref())
    }

    #[test]
    fn test_as_mut() {
        impl ABcAsMut<B> for A {
            fn a_bc_as_mut(&mut self) -> &mut B {
                &mut self.0
            }
        }
        assert_eq!(&mut B, A(B).a_bc_as_mut())
    }
    #[test]
    fn test_from_into() {
        impl ABcFrom<B> for A {
            fn a_bc_from(value: B) -> Self {
                Self(value)
            }
        }
        assert_eq!(A(B), A::a_bc_from(B));
        assert_eq!(A(B), B.a_bc_into());
        assert_eq!(B, B.a_bc_into());
        my_convert!(x, DISABLE_FROM_SELF);
        // assert_eq!(B, B.x_into());
        //                 ^^^^^^ the trait `XFrom<B>` is not implemented for `B`, which is required by `B: XInto<_>`
    }
    #[test]
    fn test_try_from_into() {
        impl ABcTryFrom<A> for B {
            type Error = std::convert::Infallible;
            fn a_bc_try_from(value: A) -> Result<Self, Self::Error> {
                Ok(value.0)
            }
        }
        assert_eq!(B, B::a_bc_try_from(A(B)).unwrap());
        assert_eq!(B, A(B).a_bc_try_into().unwrap());
    }
}

Dependencies