Skip to main content

Extensions

Struct Extensions 

Source
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

Source

pub fn new() -> Self

No extensions.

Source

pub fn of<T>(value: T) -> Self
where T: Clone + Debug + PartialEq + Send + Sync + 'static,

Extensions holding value alone.

Source

pub fn insert<T>(&mut self, value: T) -> Result<(), DuplicateExtension>
where T: Clone + Debug + PartialEq + Send + Sync + 'static,

Adds value.

§Errors

DuplicateExtension when a value of T is already held, which is kept unchanged.

Source

pub fn get<T: 'static>(&self) -> Option<&T>

The value of T, when one is held.

Source

pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>

The value of T, to change in place, when one is held.

Source

pub fn remove<T: 'static>(&mut self) -> Option<T>

Takes the value of T out, when one is held.

Source

pub fn contains<T: 'static>(&self) -> bool

Whether a value of T is held.

Source

pub fn type_ids(&self) -> impl Iterator<Item = TypeId> + '_

The types of the values held, one each.

Source

pub fn len(&self) -> usize

How many values are held.

Source

pub fn is_empty(&self) -> bool

Whether no value is held.

Source

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

Trait Implementations§

Source§

impl Clone for Extensions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Extensions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Extensions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Extensions

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.