Struct borrow_with_ref_obj::BoxedBorrowWithRefObj[][src]

pub struct BoxedBorrowWithRefObj<'main, T: ?Sized> { /* fields omitted */ }

Dynamic BorrowWithRefObj whose contents can be any BorrowWithRefObj.

This allows you to select a borrower object at runtime, and potentially swap out the borrower for one with an underlying type.

If you don't care about storing &T, you can select 'main to be 'static.

Example

use borrow_with_ref_obj::{BorrowWithRefObj, BoxedBorrowWithRefObj};
 
struct Foo<Ref: for<'a> BorrowWithRefObj<'a, u32>> {
    data: Ref
}
impl<Ref: for<'a> BorrowWithRefObj<'a, u32>> Foo<Ref> {
    fn new(theref: Ref) -> Self {
        Self { data: theref }
    }
    fn test(&self) {
        let v = self.data.borrow();
        assert_eq!(*v, 123);
    }
}
 
// Create foo directly owning a boxed value
let mut foo = Foo::new(BoxedBorrowWithRefObj::from(Box::new(123)));
foo.test(); // passes
 
// Replace foo with one that owns the value via an Rc<RefCell<T>>.
// Note how while the way the data is owned is completely different, the type
// foo hasn't changed.
 
use std::rc::Rc;
use std::cell::RefCell;
let value = Rc::new(RefCell::new(123));
foo = Foo::new(BoxedBorrowWithRefObj::from(Box::new(Rc::clone(&value))));
foo.test(); // passes

Trait Implementations

impl<'refr, 'main, T: ?Sized + 'refr> BorrowWithRefObj<'refr, T> for BoxedBorrowWithRefObj<'main, T>
[src]

Type of the reference object.

Borrows the object immutably. Read more

impl<'main, T: ?Sized, Obj> From<Box<Obj>> for BoxedBorrowWithRefObj<'main, T> where
    Obj: for<'refr> BorrowWithRefObj<'refr, T> + 'main,
    T: 'static, 
[src]

Performs the conversion.

Auto Trait Implementations

impl<'main, T> !Send for BoxedBorrowWithRefObj<'main, T>

impl<'main, T> !Sync for BoxedBorrowWithRefObj<'main, T>