Crate convert_traits

Source
Expand description

my_convert! 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.

§Example

my_convert!(a_Bc) Generate the following code:

pub trait ABcAsRef<T: ?Sized> {
    fn a_bc_as_ref(&self) -> &T;
}
impl<T: ?Sized, U: ?Sized> ABcAsRef<U> for &T
where
    T: ABcAsRef<U>,
{
    #[inline]
    fn a_bc_as_ref(&self) -> &U {
        <T as ABcAsRef<U>>::a_bc_as_ref(*self)
    }
}
impl<T: ?Sized, U: ?Sized> ABcAsRef<U> for &mut T
where
    T: ABcAsRef<U>,
{
    #[inline]
    fn a_bc_as_ref(&self) -> &U {
        <T as ABcAsRef<U>>::a_bc_as_ref(*self)
    }
}


pub trait ABcAsMut<T: ?Sized> {
    fn a_bc_as_mut(&mut self) -> &mut T;
}
impl<T: ?Sized, U: ?Sized> ABcAsMut<U> for &mut T
where
    T: ABcAsMut<U>,
{
    #[inline]
    fn a_bc_as_mut(&mut self) -> &mut U {
        (*self).a_bc_as_mut()
    }
}


 pub trait ABcFrom<T>: Sized {
    fn a_bc_from(value: T) -> Self;
}
pub trait ABcInto<T>: Sized {
    fn a_bc_into(self) -> T;
}
impl<T, U> ABcInto<U> for T
where
    U: ABcFrom<T>,
{
    #[inline]
    fn a_bc_into(self) -> U {
        U::a_bc_from(self)
    }
}


pub trait ABcTryFrom<T>: Sized {
    type Error;

    fn a_bc_try_from(value: T) -> ::core::result::Result<Self, Self::Error>;
}
impl<T, U> ABcTryFrom<U> for T
where
    U: ABcInto<T>,
{
    type Error = ::std::convert::Infallible;

    #[inline]
    fn a_bc_try_from(value: U) -> ::core::result::Result<Self, Self::Error> {
        ::core::result::Result::Ok(U::a_bc_into(value))
    }
}

pub trait ABcTryInto<T>: Sized {
    type Error;

    fn a_bc_try_into(self) -> ::core::result::Result<T, Self::Error>;
}
impl<T, U> ABcTryInto<U> for T
where
    U: ABcTryFrom<T>,
{
    type Error = U::Error;

    #[inline]
    fn a_bc_try_into(self) -> ::core::result::Result<U, U::Error> {
        U::a_bc_try_from(self)
    }
}

Macros§

my_convert
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.
paste