[go: up one dir, main page]

Strategy

Struct Strategy 

Source
pub struct Strategy<T: ?Sized, E> { /* private fields */ }
Expand description

Equips a type with a Fallible implementation that chooses E as its error type.

§Example

use rancor::{Failure, Fallible, Strategy};

trait Print<E = <Self as Fallible>::Error> {
    fn print(&self, message: &str) -> Result<(), E>;
}

impl<T: Print<E> + ?Sized, E> Print<E> for Strategy<T, E> {
    fn print(&self, message: &str) -> Result<(), E> {
        T::print(self, message)
    }
}

struct StdOut;

impl<E> Print<E> for StdOut {
    fn print(&self, message: &str) -> Result<(), E> {
        println!("{message}");
        Ok(())
    }
}

Strategy::<_, Failure>::wrap(&mut StdOut)
    .print("hello world")
    .unwrap();

Implementations§

Source§

impl<T: ?Sized, E> Strategy<T, E>

Source

pub fn wrap(inner: &mut T) -> &mut Self

Wraps the given mutable reference, returning a mutable reference to a Strategy.

§Example
use core::ops::Deref;

use rancor::{Failure, Strategy};
fn test() {
    struct Inner {
        value: u64,
    }

    let mut inner = Inner { value: 10 };

    let inner_value_ptr = &inner.value as *const u64;
    let strategy: &mut Strategy<Inner, Failure> =
        Strategy::wrap(&mut inner);
    let strategy_value_ptr = (&strategy.deref().value) as *const u64;
    assert_eq!(inner_value_ptr, strategy_value_ptr);
    // Strategy wraps a type but does not change its memory layout.
}

test();

Trait Implementations§

Source§

impl<T: ?Sized, E> Deref for Strategy<T, E>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: ?Sized, E> DerefMut for Strategy<T, E>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T: ?Sized, E> Fallible for Strategy<T, E>

Source§

type Error = E

The error type associated with this type’s operations.

Auto Trait Implementations§

§

impl<T, E> Freeze for Strategy<T, E>
where T: Freeze + ?Sized,

§

impl<T, E> RefUnwindSafe for Strategy<T, E>

§

impl<T, E> Send for Strategy<T, E>
where T: Send + ?Sized, E: Send,

§

impl<T, E> Sync for Strategy<T, E>
where T: Sync + ?Sized, E: Sync,

§

impl<T, E> Unpin for Strategy<T, E>
where T: Unpin + ?Sized, E: Unpin,

§

impl<T, E> UnwindSafe for Strategy<T, E>
where T: UnwindSafe + ?Sized, E: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.