Trait ObjectSpace

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

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§

Source

fn write<T>(&self, obj: T)
where for<'de> T: Serialize + Deserialize<'de> + 'static,

Add a struct to the object space.

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

fn try_read<T>(&self) -> Option<T>
where for<'de> T: Serialize + Deserialize<'de> + 'static,

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"))
);
Source

fn read_all<'a, T>(&'a self) -> Box<dyn Iterator<Item = T> + 'a>
where for<'de> T: Serialize + Deserialize<'de> + 'static,

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"]
);
Source

fn read<T>(&self) -> T
where for<'de> T: Serialize + Deserialize<'de> + 'static,

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")
);
Source

fn try_take<T>(&self) -> Option<T>
where for<'de> T: Serialize + Deserialize<'de> + 'static,

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);
Source

fn take_all<'a, T>(&'a self) -> Box<dyn Iterator<Item = T> + 'a>
where for<'de> T: Serialize + Deserialize<'de> + 'static,

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);
Source

fn take<T>(&self) -> T
where for<'de> T: Serialize + Deserialize<'de> + 'static,

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);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§