[][src]Trait trapper::Wrapper

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 { ... }
fn wrap_mut(inner: &mut Self::Inner) -> &mut Self { ... }
fn unwrap_ref(&self) -> &Self::Inner { ... }
fn unwrap_mut(&mut self) -> &mut Self::Inner { ... } }

A type wrapper. This trait provides methods for converting between a wrapper and its inner type. It should only be implemented by types through the newtype macro. If it must be implemented manually, the type should have transparent representation to be safe.

Associated Types

type Inner: Sized

The inner wrapped type

Loading content...

Required methods

fn wrap(inner: Self::Inner) -> Self

Wraps the value, returning a new instance of the wrapper.

Example

use trapper::{Wrapper, newtype};
newtype!(#[derive(PartialEq, Debug)] type NumberWrapper(i32));

let wrapper = NumberWrapper::wrap(12);
let other = NumberWrapper::wrap(12);
assert_eq!(wrapper, other);

fn unwrap(self) -> Self::Inner

Unwraps the wrapper, returning its inner value.

Example

use trapper::{Wrapper, newtype};
newtype!(type NumberWrapper(i32));

let wrapper = NumberWrapper::wrap(12);
assert_eq!(wrapper.unwrap(), 12);
Loading content...

Provided methods

fn wrap_ref(inner: &Self::Inner) -> &Self

Wraps a shared reference to the value in the wrapper type

Example

use trapper::{Wrapper, newtype};
newtype!(type NumberWrapper(i32));

let number = 12;
let wrapper: &NumberWrapper = NumberWrapper::wrap_ref(&number);

fn wrap_mut(inner: &mut Self::Inner) -> &mut Self

Wraps a unique reference to the value in the wrapper type

Example

use trapper::{Wrapper, newtype};
newtype!(type NumberWrapper(i32));

let mut number = 12;
let wrapper: &mut NumberWrapper = NumberWrapper::wrap_mut(&mut number);
*wrapper = NumberWrapper::wrap(13);

assert_eq!(number, 13);

fn unwrap_ref(&self) -> &Self::Inner

Unwraps a shared reference to the wrapper, exposing the underlying type

Example

use trapper::{Wrapper, newtype};
newtype!(type NumberWrapper(i32));

let wrapper = NumberWrapper::wrap(12);

assert_eq!(*wrapper.unwrap_ref(), 12);

fn unwrap_mut(&mut self) -> &mut Self::Inner

Unwraps a unique reference to the wrapper, exposing the underlying type

Example

use trapper::{Wrapper, newtype};
newtype!(#[derive(PartialEq, Debug)] type NumberWrapper(i32));

let mut wrapper = NumberWrapper::wrap(12);
*wrapper.unwrap_mut() = 13;

assert_eq!(wrapper, NumberWrapper::wrap(13));
Loading content...

Implementors

Loading content...