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>
impl<T: ?Sized, E> Strategy<T, E>
Sourcepub fn wrap(inner: &mut T) -> &mut Self
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§
Auto Trait Implementations§
impl<T, E> Freeze for Strategy<T, E>
impl<T, E> RefUnwindSafe for Strategy<T, E>
impl<T, E> Send for Strategy<T, E>
impl<T, E> Sync for Strategy<T, E>
impl<T, E> Unpin for Strategy<T, E>
impl<T, E> UnwindSafe for Strategy<T, E>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more