extern crate self as trapper;
pub unsafe trait Wrapper: Sized {
type Inner: Sized;
fn wrap(inner: Self::Inner) -> Self;
fn unwrap(self) -> Self::Inner;
fn wrap_ref(inner: &Self::Inner) -> &Self {
unsafe { &*(inner as *const Self::Inner as *const Self) }
}
fn wrap_mut(inner: &mut Self::Inner) -> &mut Self {
unsafe { &mut *(inner as *mut Self::Inner as *mut Self) }
}
fn unwrap_ref(&self) -> &Self::Inner {
unsafe { &*(self as *const Self as *const Self::Inner) }
}
fn unwrap_mut(&mut self) -> &mut Self::Inner {
unsafe { &mut *(self as *mut Self as *mut Self::Inner) }
}
}
pub use trapper_macro::newtype;
#[cfg(test)]
mod tests {
use super::newtype;
newtype!(#[allow(dead_code)] type InMod(i32));
newtype!(#[allow(dead_code)] type WithLifetimes<'a>(std::io::StderrLock<'a>));
newtype!(#[allow(dead_code)] type WithTypeParameters<T>(T));
newtype!(#[allow(dead_code)] type WithBoth<'a, T>(&'a T));
newtype!(#[allow(dead_code)] type WithClause<'a, T>(&'a T) where T: Default);
newtype! {
#[allow(dead_code)]
type AttributesDocsLifetimesTypesClausesTrailingSemicolon<'a, 'b, 'c, T>(&'a &'b &'c T) where T: Default + Clone, 'a: 'b;
}
newtype! {
type NoWhereClause<'a: 'b, 'b, T = i32>(&'b &'a T);
}
}