Struct crow_util::self_ref::SelfRefHolder [] [src]

pub struct SelfRefHolder<T, U> { /* fields omitted */ }

A struct consisting of both a Holder<T> and a parent which can be referenced by the holder.

There are still many problems with the struct, those will be fixed in case they end up being relevant during the development of the crow_engine.

To prevent undefined behavior the parent must/can not be mutated during the lifetime of this struct. Due to the fact that it is currently impossible to actually move this struct after using insert() once, I did not bother to include any way to get mutable access to the parent at all, this will probably change in the near future.

As most methods of this struct are just a wrapper for Holder<T>, look at its documentation in case there is something you don't understand.

All examples on this page rely on those two structs.

struct Foo {
    field: String
}

impl Foo {
    fn boo(&self,start: usize) -> Boo {
        Boo {
            field: &self.field[start..],
        }
    }
}

struct Boo<'a> {
    field: &'a str,
}

Examples

use crow_util::self_ref;

let self_ref_holder = self_ref::SelfRefHolder::new(Foo { field: "Hi, I am Foo!".to_string()});

self_ref_holder.insert("a", |foo| foo.boo(4));

assert_eq!(self_ref_holder.get("a").unwrap().field,"I am Foo!");

Methods

impl<T, U> SelfRefHolder<T, U>
[src]

Constructs a new SelfRefHolder<T,U> with an empty Holder<T> child.

After this calling this method it is not possible to mutate parent!

Examples

use crow_util::self_ref;

let foo = Foo { field: "Hi, I am Foo!".to_string()};

let self_ref_holder = self_ref::SelfRefHolder::new(foo);

Returns a reference to the element of child corresponding to the key.

Examples

use crow_util::self_ref;

let foo = Foo { field: "Hi, I am Foo!".to_string()};

let self_ref_holder = self_ref::SelfRefHolder::new(foo);
self_ref_holder.insert("a", |foo| foo.boo(4));

let val = self_ref_holder.get("a").unwrap().field;
assert_eq!(val, "I am Foo!");

Inserts an element accessible by key, returning a usable reference element corresponding to this key. In case the key was already present, the old element is returned and the new one is ignored. This method can be used while Holder<T> is already immutably borrowed.

Examples

use crow_util::self_ref;

let foo = Foo { field: "Hi, I am Foo!".to_string()};

let self_ref_holder = self_ref::SelfRefHolder::new(foo);

let a = self_ref_holder.insert("a", |foo| foo.boo(4)).unwrap();

assert_eq!(a.field, self_ref_holder.insert("a", |foo| foo.boo(7)).unwrap().field);