pub struct Extensions { /* private fields */ }Expand description
Facts only one venue has, carried on a shared record: at most one value of each type, read back by its type.
Each product defines one <Record>Ext type per record it fills, such as a
quote’s update id, and inserts one value of it. A value’s type is
Clone + Debug + PartialEq + Send + Sync + 'static, so a record that
carries extensions stays Clone, Debug and PartialEq, and two records
are equal only when their extensions hold equal values of the same types.
There is no serde support: a value’s type is known only to the crate that
defines it.
use polyoxide_venue::Extensions;
#[derive(Debug, Clone, PartialEq)]
struct QuoteExt {
update_id: u64,
}
let mut ext = Extensions::new();
ext.insert(QuoteExt { update_id: 7 })?;
assert_eq!(ext.get::<QuoteExt>(), Some(&QuoteExt { update_id: 7 }));
assert!(ext.insert(QuoteExt { update_id: 8 }).is_err(), "one value per type");
assert_ne!(ext, Extensions::of(QuoteExt { update_id: 8 }));A value whose type cannot be compared is refused when the program is built:
use polyoxide_venue::Extensions;
#[derive(Debug, Clone)]
struct Uncomparable;
let mut ext = Extensions::new();
ext.insert(Uncomparable).unwrap();Nor does it serialize, since a value’s type is known only to the crate that defines it:
fn serializes<T: serde::Serialize>() {}
serializes::<polyoxide_venue::Extensions>();fn deserializes<T: serde::de::DeserializeOwned>() {}
deserializes::<polyoxide_venue::Extensions>();Implementations§
Source§impl Extensions
impl Extensions
Sourcepub fn insert<T>(&mut self, value: T) -> Result<(), DuplicateExtension>
pub fn insert<T>(&mut self, value: T) -> Result<(), DuplicateExtension>
Sourcepub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>
The value of T, to change in place, when one is held.
Sourcepub fn type_ids(&self) -> impl Iterator<Item = TypeId> + '_
pub fn type_ids(&self) -> impl Iterator<Item = TypeId> + '_
The types of the values held, one each.
Sourcepub fn ensure_declared(
&self,
declared: &[TypeId],
) -> Result<(), UndeclaredExtension>
pub fn ensure_declared( &self, declared: &[TypeId], ) -> Result<(), UndeclaredExtension>
Checks that every value held is of a type in declared, the option
types a venue reads.
A request carries the venue’s own options as extensions, and a venue that reads only some option types refuses the rest rather than ignoring them. An empty set always passes.
§Errors
UndeclaredExtension naming the first value held, in TypeId order,
whose type is not in declared.
use std::any::TypeId;
use polyoxide_venue::Extensions;
#[derive(Debug, Clone, PartialEq)]
struct Options;
#[derive(Debug, Clone, PartialEq)]
struct Other;
let ext = Extensions::of(Options);
assert!(ext.ensure_declared(&[TypeId::of::<Options>()]).is_ok());
assert!(ext.ensure_declared(&[TypeId::of::<Other>()]).is_err());
assert!(Extensions::new().ensure_declared(&[]).is_ok());