Trait object_space::ObjectSpace[][src]

pub trait ObjectSpace {
    fn write<T>(&self, obj: T)
    where
        T: Serialize + Deserialize<'de> + 'static
;
fn try_read<T>(&self) -> Option<T>
    where
        T: Serialize + Deserialize<'de> + 'static
;
fn read_all<'a, T>(&'a self) -> Box<Iterator<Item = T> + 'a>
    where
        T: Serialize + Deserialize<'de> + 'static
;
fn read<T>(&self) -> T
    where
        T: Serialize + Deserialize<'de> + 'static
;
fn try_take<T>(&self) -> Option<T>
    where
        T: Serialize + Deserialize<'de> + 'static
;
fn take_all<'a, T>(&'a self) -> Box<Iterator<Item = T> + 'a>
    where
        T: Serialize + Deserialize<'de> + 'static
;
fn take<T>(&self) -> T
    where
        T: Serialize + Deserialize<'de> + 'static
; }

Basic interface of an ObjectSpace.

This trait includes pushing, reading, and popping structs from the space. An implementation of ObjectSpace should be thread-safe for usage in concurrent programs.

Example

let space = TreeObjectSpace::new();
space.write(String::from("Hello World"));
assert_eq!(
    space.try_read::<String>(),
    Some(String::from("Hello World"))
);

Required Methods

Add a struct to the object space.

Example

let space = TreeObjectSpace::new();
space.write(String::from("Hello World"));

Return a copy of a struct of type T. The operation is non-blocking and will returns None if no struct satisfies condition.

Example

let space = TreeObjectSpace::new();
space.write(String::from("Hello World"));
assert_eq!(
    space.try_read::<String>(),
    Some(String::from("Hello World"))
);

Return copies of all structs of type T. The operation is non-blocking and will returns None if no struct satisfies condition.

Example

let space = TreeObjectSpace::new();
assert_eq!(space.read_all::<String>().count(), 0);
space.write("Hello".to_string());
space.write("World".to_string());

assert_eq!(
    space.read_all::<String>().collect::<Vec<String>>(),
    vec!["Hello", "World"]
);

Return a copy of a struct of type T. The operation blocks until such a struct is found.

Example

let space = TreeObjectSpace::new();
space.write(String::from("Hello World"));
assert_eq!(
    space.read::<String>(),
    String::from("Hello World")
);

Remove and return a struct of type T. The operation is non-blocking and will returns None if no struct satisfies condition.

Example

let space = TreeObjectSpace::new();
space.write(String::from("Hello World"));
assert_eq!(
    space.try_take::<String>(),
    Some(String::from("Hello World"))
);
assert_eq!(space.try_take::<String>(), None);

Remove and return all structs of type T. The operation is non-blocking and will returns None if no struct satisfies condition.

Example

let space = TreeObjectSpace::new();
assert_eq!(space.take_all::<String>().count(), 0);
space.write("Hello".to_string());
space.write("World".to_string());

assert_eq!(
    space.take_all::<String>().collect::<Vec<String>>(),
    vec!["Hello", "World"]
);
assert_eq!(space.take_all::<String>().count(), 0);

Remove and return a struct of type T. The operation blocks until such a struct is found.

Example

let space = TreeObjectSpace::new();
space.write(String::from("Hello World"));
assert_eq!(
    space.take::<String>(),
    String::from("Hello World")
);
assert_eq!(space.try_take::<String>(), None);

Implementors