Struct Xso

Source
pub struct Xso<T: ?Sized> { /* private fields */ }
Expand description

§Dynamic XSO container

This container is very similar to Box<_>, but geared specifically toward the use with T being a dyn Trait. It also implements FromXml (unconditionally) and AsXml if T implements AsXmlDyn.

In order to provide these features, T must implement DynXso and MayContain. Implementations for these traits can be generated using derive_dyn_traits.

Most methods on Xso<dyn Trait> which take type parameters are only available for types U implementing Trait (or, more precisely, where dyn Trait implements MayContain<U>).

Implementations§

Source§

impl<T: ?Sized> Xso<T>

Source

pub fn wrap<U: 'static>(value: U) -> Self
where T: MayContain<U>,

Wrap a value into a Xso<dyn Trait>.

trait Trait: Any {}
derive_dyn_traits!(Trait);

struct Foo;
impl Trait for Foo {}

let x: Xso<dyn Trait> = Xso::wrap(Foo);
Source

pub fn into_boxed(self) -> Box<T>

Convert Xso<T> into Box<T>.

trait Trait: Any {}
derive_dyn_traits!(Trait);

struct Foo;
impl Trait for Foo {}

let x: Xso<dyn Trait> = Xso::wrap(Foo);
let x: Box<dyn Trait> = x.into_boxed();
Source§

impl<T: DynXso + ?Sized + 'static> Xso<T>

Source

pub fn downcast<U: 'static>(self) -> Result<Box<U>, Self>
where T: MayContain<U>,

Downcast self to Box<U>.

If the downcast fails, self is returned without change.

trait Trait: Any {}
derive_dyn_traits!(Trait);

struct Foo;
impl Trait for Foo {}

struct Bar;
impl Trait for Bar {}

let x: Xso<dyn Trait> = Xso::wrap(Foo);
// Does not contain a Bar, so downcast fails.
let x: Xso<dyn Trait> = x.downcast::<Bar>().err().unwrap();
// *Does* contain a Foo, so downcast succeeds.
let f: Foo = *x.downcast().unwrap();
Source

pub fn downcast_ref<U: 'static>(&self) -> Option<&U>
where T: MayContain<U>,

Downcast &self to &U.

trait Trait: Any {}
derive_dyn_traits!(Trait);

struct Foo;
impl Trait for Foo {}

struct Bar;
impl Trait for Bar {}

let x: Xso<dyn Trait> = Xso::wrap(Foo);
// Does not contain a Bar, so downcast fails.
assert!(x.downcast_ref::<Bar>().is_none());
// *Does* contain a Foo, so downcast succeeds.
let f: &Foo = x.downcast_ref().unwrap();
Source

pub fn downcast_mut<U: 'static>(&mut self) -> Option<&mut U>
where T: MayContain<U>,

Downcast &mut self to &mut U.

trait Trait: Any {}
derive_dyn_traits!(Trait);

struct Foo;
impl Trait for Foo {}

struct Bar;
impl Trait for Bar {}

let mut x: Xso<dyn Trait> = Xso::wrap(Foo);
// Does not contain a Bar, so downcast fails.
assert!(x.downcast_mut::<Bar>().is_none());
// *Does* contain a Foo, so downcast succeeds.
let f: &mut Foo = x.downcast_mut().unwrap();
Source§

impl<R: DynXsoRegistryAdd<T> + 'static, T: DynXso<Registry = R> + ?Sized + 'static> Xso<T>

Source

pub fn register_type<U: FromXml + 'static>()
where T: MayContain<U>,

Register a new type to be constructible.

Only types registered through this function can be parsed from XML via the FromXml implementation on Xso<T>. See dynxso for details.

trait Trait: Any {}
derive_dyn_traits!(Trait);

#[derive(FromXml, PartialEq, Debug)]
#[xml(namespace = "urn:example", name = "foo")]
struct Foo;
impl Trait for Foo {}

// Parsing fails, because register_type() has not been called for
// Foo:
assert!(from_bytes::<Xso<dyn Trait>>("<foo xmlns='urn:example'/>".as_bytes()).is_err());

Xso::<dyn Trait>::register_type::<Foo>();
// After registering Foo with Xso<dyn Trait>, parsing succeeds and
// we can downcast to Foo:
let x: Xso<dyn Trait> = from_bytes("<foo xmlns='urn:example'/>".as_bytes()).unwrap();
assert_eq!(Foo, *x.downcast().unwrap());

Trait Implementations§

Source§

impl<T: DynXso + AsXmlDyn + ?Sized + 'static> AsXml for Xso<T>

Source§

type ItemIter<'x> = Box<dyn Iterator<Item = Result<Item<'x>, Error>> + 'x>

The iterator type. Read more
Source§

fn as_xml_iter(&self) -> Result<Self::ItemIter<'_>, Error>

Return an iterator which emits the contents of the struct or enum as serialisable Item items.
Source§

fn as_xml_dyn_iter(&self) -> Result<Self::ItemIter<'_>, Error>

Return the same iterator as as_xml_iter, but boxed to erase the concrete iterator type. Read more
Source§

impl<T: Clone + ?Sized> Clone for Xso<T>

Source§

fn clone(&self) -> Xso<T>

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: DynXso + ?Sized> Debug for Xso<T>

Source§

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

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

impl<T: ?Sized> Deref for Xso<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: ?Sized> DerefMut for Xso<T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T: DynXso + ?Sized + 'static> Extend<Xso<T>> for XsoVec<T>

Source§

fn extend<I: IntoIterator<Item = Xso<T>>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<R: DynXsoRegistryLookup<T> + 'static, T: DynXso<Registry = R> + ?Sized + 'static> FromXml for Xso<T>

Source§

type Builder = DynBuilder<Box<dyn FromEventsBuilder<Output = Box<T>>>>

A builder type used to construct the element. Read more
Source§

fn from_events( name: QName, attrs: AttrMap, ctx: &Context<'_>, ) -> Result<Self::Builder, FromEventsError>

Attempt to initiate the streamed construction of this struct from XML. Read more
Source§

fn xml_name_matcher() -> XmlNameMatcher<'static>

Return a predicate which determines if Self may be parsed from a given XML element. Read more
Source§

impl<T: Hash + ?Sized> Hash for Xso<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Ord + ?Sized> Ord for Xso<T>

Source§

fn cmp(&self, other: &Xso<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq + ?Sized> PartialEq for Xso<T>

Source§

fn eq(&self, other: &Xso<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd + ?Sized> PartialOrd for Xso<T>

Source§

fn partial_cmp(&self, other: &Xso<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq + ?Sized> Eq for Xso<T>

Source§

impl<T: ?Sized> StructuralPartialEq for Xso<T>

Auto Trait Implementations§

§

impl<T> Freeze for Xso<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for Xso<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Send for Xso<T>
where T: Send + ?Sized,

§

impl<T> Sync for Xso<T>
where T: Sync + ?Sized,

§

impl<T> Unpin for Xso<T>
where T: ?Sized,

§

impl<T> UnwindSafe for Xso<T>
where T: UnwindSafe + ?Sized,

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> AsXmlDyn for T
where T: AsXml,

Source§

fn as_xml_dyn_iter<'x>( &'x self, ) -> Result<Box<dyn Iterator<Item = Result<Item<'x>, Error>> + 'x>, Error>

Return an iterator which emits the contents of the struct or enum as serialisable Item items by calling AsXml::as_xml_dyn_iter.

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,