[][src]Struct abi_stable::erased_types::dyn_trait::DynTrait

#[repr(C)]
pub struct DynTrait<'borr, P, I, EV = ()> where
    P: GetPointerKind
{ /* fields omitted */ }

DynTrait implements ffi-safe trait objects,for a selection of traits.

Passing opaque values around with DynTrait<_>

One can pass non-StableAbi types around by using type erasure,using this type.

It generally looks like DynTrait<'borrow,Pointer<()>,Interface>,where:

  • 'borrow is the borrow that the type that was erased had.

  • Pointer implements std::ops::Deref.

  • Interface is an InterfaceType,which describes what traits are required when constructing the DynTrait<_> and which ones it implements.

trait InterfaceType allows describing which traits are required when constructing a DynTrait<_>,and which ones it implements.

Construction

To construct a DynTrait<_> one can use these associated functions:

  • from_value: Can be constructed from the value directly. Requires a value that implements ImplType.

  • from_ptr: Can be constructed from a pointer of a value. Requires a value that implements ImplType.

  • from_any_value: Can be constructed from the value directly.Requires a 'static value.

  • from_any_ptr Can be constructed from a pointer of a value.Requires a 'static value.

  • from_borrowing_value: Can be constructed from the value directly.Cannot unerase the DynTrait afterwards.

  • from_borrowing_ptr Can be constructed from a pointer of a value.Cannot unerase the DynTrait afterwards.

DynTrait uses the impls of the value in methods, which means that the pointer itself does not have to implement those traits,

Trait object

DynTrait<'borrow,Pointer<()>,Interface> can be used as a trait object for any combination of the traits listed bellow.

These are the traits:

  • Send

  • Sync

  • Iterator

  • DoubleEndedIterator

  • std::fmt::Write

  • std::io::Write

  • std::io::Seek

  • std::io::Read

  • std::io::BufRead

  • Clone

  • Display

  • Debug

  • std::error::Error

  • Default: Can be called as an inherent method.

  • Eq

  • PartialEq

  • Ord

  • PartialOrd

  • Hash

  • serde::Deserialize: first deserializes from a string,and then calls the objects' Deserialize impl.

  • serde::Serialize: first calls the objects' Deserialize impl,then serializes that as a string.

Deconstruction

DynTrait<_> can then be unwrapped into a concrete type, within the same dynamic library/executable that constructed it, using these (fallible) conversion methods:

  • into_unerased_impltype: Unwraps into a pointer to T. Where DynTrait<P<()>,Interface>'s Interface must equal <T as ImplType>::Interface

  • as_unerased_impltype: Unwraps into a &T. Where DynTrait<P<()>,Interface>'s Interface must equal <T as ImplType>::Interface

  • as_unerased_mut_impltype: Unwraps into a &mut T. Where DynTrait<P<()>,Interface>'s Interface must equal <T as ImplType>::Interface

  • into_unerased:Unwraps into a pointer to T.Requires T:'static.

  • as_unerased:Unwraps into a &T.Requires T:'static.

  • as_unerased_mut:Unwraps into a &mut T.Requires T:'static.

DynTrait cannot be converted back if it was created using DynTrait::from_borrowing_*.

Passing DynTrait between dynamic libraries

Passing DynTrait between dynamic libraries (as in between the dynamic libraries directly loaded by the same binary/dynamic library) may cause the program to panic at runtime with an error message stating that the trait is not implemented for the specific interface.

This can only happen if you are passing DynTrait between dynamic libraries, or if DynTrait was instantiated in the parent passed to a child, a DynTrait instantiated in a child dynamic library passed to the parent should not cause a panic,it would be a bug.

        binary
  _________|___________
lib0      lib1      lib2
  |         |         |
lib00    lib10      lib20

In this diagram passing a DynTrait constructed in lib00 to anything other than the binary or lib0 will cause the panic to happen if:

  • The InterfaceType requires extra traits in the version of the Interface that lib1 and lib2 know about (that the binary does not require).

  • lib1 or lib2 attempt to call methods that require the traits that were added to the InterfaceType,in versions of that interface that only they know about.

serializing/deserializing DynTraits

To be able to serialize and deserialize a DynTrait, the interface it uses must implement SerializeProxyType and DeserializeDyn, and the implementation type must implement SerializeImplType.

For a more realistic example you can look at the "examples/0_modules_and_interface_types" crates in the repository for this crate.

use abi_stable::{
    StableAbi,
    impl_get_type_info,
    sabi_extern_fn,
    erased_types::{
        InterfaceType,DeserializeDyn,SerializeProxyType,ImplType,SerializeImplType,
        TypeInfo,
    },
    external_types::{RawValueRef,RawValueBox},
    prefix_type::PrefixTypeTrait,
    type_level::bools::*,
    DynTrait,
    std_types::{RBox, RStr,RBoxError,RResult,ROk,RErr},
    traits::IntoReprC,
};

use serde::{Deserialize,Serialize};

/// An `InterfaceType` describing which traits are implemented by FooInterfaceBox.
#[repr(C)]
#[derive(StableAbi)]
#[sabi(impl_InterfaceType(Sync,Debug,Clone,Serialize,Deserialize,PartialEq))]
pub struct FooInterface;

/// The state passed to most functions in the TextOpsMod module.
pub type FooInterfaceBox = DynTrait<'static,RBox<()>,FooInterface>;


/// First <ConcreteType as DeserializeImplType>::serialize_impl returns 
/// a RawValueBox containing the serialized data,
/// then the returned RawValueBox is serialized.
impl<'s> SerializeProxyType<'s> for FooInterface{
    type Proxy=RawValueBox;
}


impl<'borr> DeserializeDyn<'borr,FooInterfaceBox> for FooInterface {
    type Proxy = RawValueRef<'borr>;

    fn deserialize_dyn(s: RawValueRef<'borr>) -> Result<FooInterfaceBox, RBoxError> {
        get_module()
            .deserialize_foo()(s.get_rstr())
            .into_result()
    }
}


#[repr(C)]
#[derive(StableAbi)] 
#[sabi(kind(Prefix(prefix_struct="Module")))]
#[sabi(missing_field(panic))]
pub struct ModuleVal{
    #[sabi(last_prefix_field)]
    pub deserialize_foo:extern "C" fn(s:RStr<'_>)->RResult<FooInterfaceBox,RBoxError>,
}




/////////////////////////////////////////////////////////////////////////////////////////
////   In implementation crate (the one that gets compiled as a dynamic library)    /////
/////////////////////////////////////////////////////////////////////////////////////////


#[derive(Debug,Clone,PartialEq,Serialize,Deserialize)]
pub struct Foo{
    name:String,
}

impl ImplType for Foo {
    type Interface = FooInterface;

    const INFO: &'static TypeInfo=impl_get_type_info! { Foo };
}

impl<'s> SerializeImplType<'s> for Foo {
    type Interface = FooInterface;

    fn serialize_impl(&'s self) -> Result<RawValueBox, RBoxError> {
        match serde_json::to_string(self) {
            Ok(v)=>{
                RawValueBox::try_from_string(v)
                    .map_err(RBoxError::new)
            }
            Err(e)=>Err(RBoxError::new(e)),
        }
    }
}

#[sabi_extern_fn]
pub fn deserialize_foo(s:RStr<'_>)->RResult<FooInterfaceBox,RBoxError>{
    match serde_json::from_str::<Foo>(s.into()) {
        Ok(x) => ROk(DynTrait::from_value(x)),
        Err(e) => RErr(RBoxError::new(e)),
    }
}


let foo=Foo{name:"nope".into()};
let object=DynTrait::from_value(foo.clone());

assert_eq!(
    serde_json::from_str::<FooInterfaceBox>(r##"
        {
            "name":"nope"
        }
    "##).unwrap(),
    object
);

assert_eq!(
    serde_json::to_string(&object).unwrap(),
    r##"{"name":"nope"}"##
);


Examples

In the Readme

The primary example using DynTrait<_> is in the readme.

Readme is in the repository for this crate, crates.io, lib.rs.

Comparing DynTraits

This is only possible if the erased types don't contain borrows, and they are not constructed using DynTrait::from_borrowing_* methods.

DynTraits wrapping different pointer types can be compared with each other, it simply uses the values' implementation of PartialEq.

use abi_stable::{
    DynTrait,
    erased_types::interfaces::PartialEqInterface,
    std_types::RArc,
};

{
    let left:DynTrait<'static,&(),PartialEqInterface>=
        DynTrait::from_any_ptr(&100,PartialEqInterface);
    
    let mut n100=100;
    let right:DynTrait<'static,&mut (),PartialEqInterface>=
        DynTrait::from_any_ptr(&mut n100,PartialEqInterface);

    assert_eq!(left,right);
}
{
    let left=
        DynTrait::from_any_value(200,PartialEqInterface);

    let right=
        DynTrait::from_any_ptr(RArc::new(200),PartialEqInterface);

    assert_eq!(left,right);
}

Writing to a DynTrait

This is an example of using the write!() macro with DynTrait.

use abi_stable::{
    DynTrait,
    erased_types::interfaces::FmtWriteInterface,
};

use std::fmt::Write;

let mut buffer=String::new();

let mut wrapped:DynTrait<'static,&mut (),FmtWriteInterface>=
    DynTrait::from_any_ptr(&mut buffer,FmtWriteInterface);

write!(wrapped,"Foo").unwrap();
write!(wrapped,"Bar").unwrap();
write!(wrapped,"Baz").unwrap();

drop(wrapped);

assert_eq!(&buffer[..],"FooBarBaz");

Iteration

Using DynTrait as an Iterator and DoubleEndedIterator.

use abi_stable::{
    DynTrait,
    erased_types::interfaces::DEIteratorInterface,
};

let mut wrapped=DynTrait::from_any_value(0..=10,DEIteratorInterface::NEW);

assert_eq!(
    wrapped.by_ref().take(5).collect::<Vec<_>>(),
    vec![0,1,2,3,4]
);

assert_eq!(
    wrapped.rev().collect::<Vec<_>>(),
    vec![10,9,8,7,6,5]
);

Making pointers compatible with DynTrait

To make pointers compatible with DynTrait,they must imlement the abi_stable::pointer_trait::{GetPointerKind,Deref,CanTransmuteElement} traits as shown in the example.

GetPointerKind should generally be implemented with type Kind=PK_SmartPointer. The exception is in the case that it is a #[repr(transparent)] wrapper around a & or a &mut, in which case it should implement GetPointerKind<Kind=PK_Reference> or GetPointerKind<Kind=PK_MutReference> respectively.

Example

This is an example of a newtype wrapping an RBox<T>.

    
use abi_stable::DynTrait;

fn main(){
    let lines="line0\nline1\nline2";
    let mut iter=NewtypeBox::new(lines.lines());

    // The type annotation here is just to show the type,it's not necessary.
    let mut wrapper:DynTrait<'_,NewtypeBox<()>,IteratorInterface>=
        DynTrait::from_borrowing_ptr(iter,IteratorInterface);

    // You can clone the DynTrait! 
    let clone=wrapper.clone();

    assert_eq!( wrapper.next(), Some("line0") );
    assert_eq!( wrapper.next(), Some("line1") );
    assert_eq!( wrapper.next(), Some("line2") );
    assert_eq!( wrapper.next(), None );

    assert_eq!(
        clone.rev().collect::<Vec<_>>(),
        vec!["line2","line1","line0"],
    )

}


/////////////////////////////////////////

use std::ops::{Deref, DerefMut};

use abi_stable::{
    StableAbi,
    InterfaceType,
    std_types::RBox,
    erased_types::IteratorItem,
    pointer_trait::{
        PK_SmartPointer,GetPointerKind,CanTransmuteElement
    },
    type_level::bools::True,
};

#[repr(transparent)]
#[derive(Default,Clone,StableAbi)]
pub struct NewtypeBox<T>{
    box_:RBox<T>,
}

impl<T> NewtypeBox<T>{
    pub fn new(value:T)->Self{
        Self{
            box_:RBox::new(value)
        }
    }
}

impl<T> Deref for NewtypeBox<T>{
    type Target=T;

    fn deref(&self)->&T{
        &*self.box_
    }
}

impl<T> DerefMut for NewtypeBox<T>{
    fn deref_mut(&mut self)->&mut T{
        &mut *self.box_
    }
}

unsafe impl<T> GetPointerKind for NewtypeBox<T>{
    type Kind=PK_SmartPointer;
}

unsafe impl<T,O> CanTransmuteElement<O> for NewtypeBox<T>
where 
    // Using this to ensure that the pointer is safe to wrap,
    // while this is not necessary for `RBox<T>`,
    // it might be for some other pointer type.
    RBox<T>:CanTransmuteElement<O,Kind=Self::Kind>
{
    type TransmutedPtr = NewtypeBox<O>;
}

/////////////////////////////////////////

#[repr(C)]
#[derive(StableAbi)]
#[sabi(impl_InterfaceType(Sync,Send,Iterator,DoubleEndedIterator,Clone,Debug))]
pub struct IteratorInterface;

impl<'a> IteratorItem<'a> for IteratorInterface{
    type Item=&'a str;
}

Methods

impl DynTrait<'static, &'static (), ()>[src]

Important traits for DynTrait<'borr, P, I, EV>
pub fn from_value<T>(object: T) -> DynTrait<'static, RBox<()>, T::Interface> where
    T: ImplType,
    T::Interface: InterfaceBound,
    T: GetVtable<'static, T, RBox<()>, RBox<T>, <T as ImplType>::Interface>, 
[src]

Constructs the DynTrait<_> from a T:ImplType.

Use this whenever possible instead of from_any_value, because it produces better error messages when unerasing the DynTrait<_>

Important traits for DynTrait<'borr, P, I, EV>
pub fn from_ptr<P, T>(
    object: P
) -> DynTrait<'static, P::TransmutedPtr, T::Interface> where
    T: ImplType,
    T::Interface: InterfaceBound,
    T: GetVtable<'static, T, P::TransmutedPtr, P, <T as ImplType>::Interface>,
    P: Deref<Target = T> + CanTransmuteElement<()> + GetPointerKind,
    P::TransmutedPtr: GetPointerKind
[src]

Constructs the DynTrait<_> from a pointer to a T:ImplType.

Use this whenever possible instead of from_any_ptr, because it produces better error messages when unerasing the DynTrait<_>

Important traits for DynTrait<'borr, P, I, EV>
pub fn from_any_value<T, I>(
    object: T,
    interface: I
) -> DynTrait<'static, RBox<()>, I> where
    T: 'static,
    I: InterfaceBound,
    InterfaceFor<T, I, TU_Unerasable>: GetVtable<'static, T, RBox<()>, RBox<T>, I>, 
[src]

Constructs the DynTrait<_> from a type that doesn't borrow anything.

Important traits for DynTrait<'borr, P, I, EV>
pub fn from_any_ptr<P, T, I>(
    object: P,
    _interface: I
) -> DynTrait<'static, P::TransmutedPtr, I> where
    I: InterfaceBound,
    T: 'static,
    InterfaceFor<T, I, TU_Unerasable>: GetVtable<'static, T, P::TransmutedPtr, P, I>,
    P: Deref<Target = T> + CanTransmuteElement<()> + GetPointerKind,
    P::TransmutedPtr: GetPointerKind
[src]

Constructs the DynTrait<_> from a pointer to a type that doesn't borrow anything.

Important traits for DynTrait<'borr, P, I, EV>
pub fn from_borrowing_value<'borr, T, I>(
    object: T,
    interface: I
) -> DynTrait<'borr, RBox<()>, I> where
    T: 'borr,
    I: InterfaceBound,
    InterfaceFor<T, I, TU_Opaque>: GetVtable<'borr, T, RBox<()>, RBox<T>, I>, 
[src]

Constructs the DynTrait<_> from a value with a 'borr borrow.

Cannot unerase the DynTrait afterwards.

Important traits for DynTrait<'borr, P, I, EV>
pub fn from_borrowing_ptr<'borr, P, T, I>(
    object: P,
    _interface: I
) -> DynTrait<'borr, P::TransmutedPtr, I> where
    T: 'borr,
    I: InterfaceBound,
    InterfaceFor<T, I, TU_Opaque>: GetVtable<'borr, T, P::TransmutedPtr, P, I>,
    P: Deref<Target = T> + CanTransmuteElement<()> + GetPointerKind,
    P::TransmutedPtr: GetPointerKind
[src]

Constructs the DynTrait<_> from a pointer to the erased type with a 'borr borrow.

Cannot unerase the DynTrait afterwards.

impl<'borr, P, I, EV> DynTrait<'borr, P, I, EV> where
    P: GetPointerKind<Target = ()>, 
[src]

Important traits for DynTrait<'borr, P, I, EV>
pub fn with_extra_value<OrigPtr, Unerasability>(
    ptr: OrigPtr,
    extra_value: EV
) -> DynTrait<'borr, P, I, EV> where
    OrigPtr::Target: Sized + 'borr,
    I: InterfaceBound,
    InterfaceFor<OrigPtr::Target, I, Unerasability>: GetVtable<'borr, OrigPtr::Target, P, OrigPtr, I>,
    OrigPtr: CanTransmuteElement<(), TransmutedPtr = P>, 
[src]

Constructs an DynTrait from an erased pointer and an extra value.

impl<'borr, 'a, I, EV> DynTrait<'borr, RRef<'a, ()>, I, EV>[src]

pub const fn from_const<T, Unerasability>(
    ptr: &'a T,
    unerasability: Unerasability,
    vtable_for: VTableDT<'borr, T, RRef<'a, ()>, RRef<'a, T>, I, Unerasability>,
    extra_value: EV
) -> Self where
    T: 'borr, 
[src]

This function allows constructing a DynTrait in a constant/static.

Parameters

ptr: This is generally constructed with RRef::new(&value) RRef is a reference-like type that can be erased inside a const fn on stable Rust (once it becomes possible to unsafely cast &T to &() inside a const fn, and the minimum Rust version is bumped,this type will be replaced with a reference)


unerasability can be either:

  • TU_Unerasable: Which allows the trait object to be unerased,requires that the value implements any.

  • TU_Opaque: Which does not allow the trait object to be unerased.


vtable_for: This is constructible with VTableDT::GET. VTableDT wraps the vtable for a DynTrait, while keeping the original type and pointer type that it was constructed for, allowing this function to be safe to call.


extra_value: This is used by #[sabi_trait] trait objects to store their vtable inside DynTrait.

Example

use abi_stable::{
    erased_types::{
        interfaces::DebugDisplayInterface,
        DynTrait,TU_Opaque,VTableDT,
    },
    sabi_types::RRef,
};

static STRING:&str="What the heck";

static DYN:DynTrait<'static,RRef<'static,()>,DebugDisplayInterface,()>=
    DynTrait::from_const(
        &STRING,
        TU_Opaque,
        VTableDT::GET,
        (),
    );

fn main(){
    assert_eq!( format!("{}",DYN), format!("{}",STRING) );
    assert_eq!( format!("{:?}",DYN), format!("{:?}",STRING) );    
}

impl<P, I, EV> DynTrait<'static, P, I, EV> where
    P: GetPointerKind
[src]

pub fn sabi_is_same_type<Other, I2, EV2>(
    &self,
    other: &DynTrait<'static, Other, I2, EV2>
) -> bool where
    I2: InterfaceBound,
    Other: GetPointerKind
[src]

Allows checking whether 2 DynTrait<_>s have a value of the same type.

Notes:

  • Types from different dynamic libraries/executables are never considered equal.

  • DynTraits constructed using DynTrait::from_borrowing_* are never considered to wrap the same type.

impl<'borr, P, I, EV> DynTrait<'borr, P, I, StaticRef<EV>> where
    P: GetPointerKind
[src]

pub fn sabi_et_vtable(&self) -> StaticRef<EV>[src]

A vtable used by #[sabi_trait] derived trait objects.

impl<'borr, P, I, EV> DynTrait<'borr, P, I, EV> where
    P: GetPointerKind
[src]

pub fn sabi_extra_value(&self) -> &EV[src]

Gets access to the extra value that was stored in this DynTrait in the with_extra_value constructor.

pub fn sabi_object_address(&self) -> usize where
    P: Deref
[src]

Returns the address of the wrapped object.

pub fn sabi_erased_ref(&self) -> &ErasedObject where
    P: Deref
[src]

pub fn sabi_erased_mut(&mut self) -> &mut ErasedObject where
    P: DerefMut
[src]

pub fn sabi_with_value<F, R>(self, f: F) -> R where
    P: OwnedPointer<Target = ()>,
    F: FnOnce(MovePtr<()>) -> R, 
[src]

impl<'borr, P, I, EV> DynTrait<'borr, P, I, EV> where
    P: GetPointerKind
[src]

pub fn into_unerased_impltype<T>(
    self
) -> Result<P::TransmutedPtr, UneraseError<Self>> where
    P: CanTransmuteElement<T>,
    P::Target: Sized,
    T: ImplType
[src]

Unwraps the DynTrait<_> into a pointer of the concrete type that it was constructed with.

T is required to implement ImplType.

Errors

This will return an error in any of these conditions:

  • It is called in a dynamic library/binary outside the one from which this DynTrait<_> was constructed.

  • The DynTrait was constructed using a from_borrowing_* method

  • T is not the concrete type this DynTrait<_> was constructed with.

pub fn as_unerased_impltype<T>(&self) -> Result<&T, UneraseError<&Self>> where
    P: Deref + CanTransmuteElement<T>,
    T: ImplType
[src]

Unwraps the DynTrait<_> into a reference of the concrete type that it was constructed with.

T is required to implement ImplType.

Errors

This will return an error in any of these conditions:

  • It is called in a dynamic library/binary outside the one from which this DynTrait<_> was constructed.

  • The DynTrait was constructed using a from_borrowing_* method

  • T is not the concrete type this DynTrait<_> was constructed with.

pub fn as_unerased_mut_impltype<T>(
    &mut self
) -> Result<&mut T, UneraseError<&mut Self>> where
    P: DerefMut + CanTransmuteElement<T>,
    T: ImplType
[src]

Unwraps the DynTrait<_> into a mutable reference of the concrete type that it was constructed with.

T is required to implement ImplType.

Errors

This will return an error in any of these conditions:

  • It is called in a dynamic library/binary outside the one from which this DynTrait<_> was constructed.

  • The DynTrait was constructed using a from_borrowing_* method

  • T is not the concrete type this DynTrait<_> was constructed with.

pub fn into_unerased<T>(self) -> Result<P::TransmutedPtr, UneraseError<Self>> where
    T: 'static,
    P: CanTransmuteElement<T>,
    P::Target: Sized,
    Self: DynTraitBound<'borr>,
    InterfaceFor<T, I, TU_Unerasable>: ImplType
[src]

Unwraps the DynTrait<_> into a pointer of the concrete type that it was constructed with.

T is required to not borrow anything.

Errors

This will return an error in any of these conditions:

  • It is called in a dynamic library/binary outside the one from which this DynTrait<_> was constructed.

  • The DynTrait was constructed using a from_borrowing_* method

  • T is not the concrete type this DynTrait<_> was constructed with.

pub fn as_unerased<T>(&self) -> Result<&T, UneraseError<&Self>> where
    T: 'static,
    P: Deref + CanTransmuteElement<T>,
    Self: DynTraitBound<'borr>,
    InterfaceFor<T, I, TU_Unerasable>: ImplType
[src]

Unwraps the DynTrait<_> into a reference of the concrete type that it was constructed with.

T is required to not borrow anything.

Errors

This will return an error in any of these conditions:

  • It is called in a dynamic library/binary outside the one from which this DynTrait<_> was constructed.

  • The DynTrait was constructed using a from_borrowing_* method

  • T is not the concrete type this DynTrait<_> was constructed with.

pub fn as_unerased_mut<T>(&mut self) -> Result<&mut T, UneraseError<&mut Self>> where
    P: DerefMut + CanTransmuteElement<T>,
    Self: DynTraitBound<'borr>,
    InterfaceFor<T, I, TU_Unerasable>: ImplType
[src]

Unwraps the DynTrait<_> into a mutable reference of the concrete type that it was constructed with.

T is required to not borrow anything.

Errors

This will return an error in any of these conditions:

  • It is called in a dynamic library/binary outside the one from which this DynTrait<_> was constructed.

  • The DynTrait was constructed using a from_borrowing_* method

  • T is not the concrete type this DynTrait<_> was constructed with.

pub unsafe fn unchecked_into_unerased<T>(self) -> P::TransmutedPtr where
    P: Deref + CanTransmuteElement<T>,
    P::Target: Sized
[src]

Unwraps the DynTrait<_> into a pointer to T, without checking whether T is the type that the DynTrait was constructed with.

Safety

You must check that T is the type that DynTrait was constructed with through other means.

pub unsafe fn unchecked_as_unerased<T>(&self) -> &T where
    P: Deref
[src]

Unwraps the DynTrait<_> into a reference to T, without checking whether T is the type that the DynTrait was constructed with.

Safety

You must check that T is the type that DynTrait was constructed with through other means.

pub unsafe fn unchecked_as_unerased_mut<T>(&mut self) -> &mut T where
    P: DerefMut
[src]

Unwraps the DynTrait<_> into a mutable reference to T, without checking whether T is the type that the DynTrait was constructed with.

Safety

You must check that T is the type that DynTrait was constructed with through other means.

impl<'borr, P, I, EV> DynTrait<'borr, P, I, EV> where
    P: GetPointerKind,
    I: InterfaceType
[src]

Important traits for DynTrait<'borr, P, I, EV>
pub fn reborrow<'re>(&'re self) -> DynTrait<'borr, &'re (), I, EV> where
    P: Deref<Target = ()>,
    PrivStruct: ReborrowBounds<I::Send, I::Sync>,
    EV: Copy
[src]

Creates a shared reborrow of this DynTrait.

The reborrowed DynTrait cannot use these methods:

  • DynTrait::default

Important traits for DynTrait<'borr, P, I, EV>
pub fn reborrow_mut<'re>(&'re mut self) -> DynTrait<'borr, &'re mut (), I, EV> where
    P: DerefMut<Target = ()>,
    PrivStruct: ReborrowBounds<I::Send, I::Sync>,
    EV: Copy
[src]

Creates a mutable reborrow of this DynTrait.

The reborrowed DynTrait cannot use these methods:

  • DynTrait::default

  • DynTrait::clone

impl<'borr, P, I, EV> DynTrait<'borr, P, I, EV> where
    I: InterfaceBound + 'borr,
    EV: 'borr,
    P: GetPointerKind
[src]

pub fn default(&self) -> Self where
    P: Deref + GetPointerKind<Kind = PK_SmartPointer>,
    I: InterfaceType<Default = Implemented<Default>>,
    EV: Copy
[src]

Constructs a DynTrait<P,I> with the default value for P.

Reborrowing

This cannot be called with a reborrowed DynTrait:

This example deliberately fails to compile
let object=DynTrait::from_any_value((),DefaultInterface);
let borrow=object.reborrow();
let _=borrow.default();
This example deliberately fails to compile
let object=DynTrait::from_any_value((),DefaultInterface);
let borrow=object.reborrow_mut();
let _=borrow.default();

pub fn serialize_into_proxy<'a>(&'a self) -> Result<I::ProxyType, RBoxError> where
    P: Deref,
    I: InterfaceType<Serialize = Implemented<Serialize>>,
    I: GetSerializeProxyType<'a>, 
[src]

It serializes a DynTrait<_> into a string by using <ConcreteType as SerializeImplType>::serialize_impl.

pub fn deserialize_from_proxy<'de>(proxy: I::Proxy) -> Result<Self, RBoxError> where
    P: 'borr + Deref,
    I: DeserializeDyn<'de, Self>, 
[src]

Deserializes a DynTrait<'borr,_> from a proxy type,by using <I as DeserializeDyn<'borr,Self>>::deserialize_dyn.

impl<'borr, P, I, Item, EV> DynTrait<'borr, P, I, EV> where
    P: DerefMut + GetPointerKind,
    I: IteratorItemOrDefault<'borr, Item = Item>,
    I: InterfaceBound<Iterator = Implemented<Iterator>>,
    Item: 'borr, 
[src]

pub fn skip_eager(&mut self, n: usize)[src]

Eagerly skips n elements from the iterator.

This method is faster than using Iterator::skip.

Example


let mut iter=0..20;
let mut wrapped=DynTrait::from_any_ptr(&mut iter,IteratorInterface::NEW);

assert_eq!(wrapped.next(),Some(0));

wrapped.skip_eager(2);

assert_eq!(wrapped.next(),Some(3));
assert_eq!(wrapped.next(),Some(4));
assert_eq!(wrapped.next(),Some(5));

wrapped.skip_eager(2);

assert_eq!(wrapped.next(),Some(8));
assert_eq!(wrapped.next(),Some(9));

wrapped.skip_eager(9);

assert_eq!(wrapped.next(),Some(19));
assert_eq!(wrapped.next(),None    );


pub fn extending_rvec(
    &mut self,
    buffer: &mut RVec<Item>,
    taking: ROption<usize>
)
[src]

Extends the RVec<Item> with the self Iterator.

Extends buffer with as many elements of the iterator as taking specifies:

  • RNone: Yields all elements.Use this with care,since Iterators can be infinite.

  • RSome(n): Yields n elements.

Example


let mut wrapped=DynTrait::from_any_value(0.. ,IteratorInterface::NEW);

let mut buffer=vec![ 101,102,103 ].into_c();
wrapped.extending_rvec(&mut buffer,RSome(5));
assert_eq!(
    &buffer[..],
    &*vec![101,102,103,0,1,2,3,4]
);

assert_eq!( wrapped.next(),Some(5));
assert_eq!( wrapped.next(),Some(6));
assert_eq!( wrapped.next(),Some(7));

impl<'borr, P, I, Item, EV> DynTrait<'borr, P, I, EV> where
    Self: Iterator<Item = Item>,
    P: DerefMut + GetPointerKind,
    I: IteratorItemOrDefault<'borr, Item = Item>,
    I: InterfaceBound<DoubleEndedIterator = Implemented<DoubleEndedIterator>>,
    Item: 'borr, 
[src]

pub fn nth_back_(&mut self, nth: usize) -> Option<Item>[src]

pub fn extending_rvec_back(
    &mut self,
    buffer: &mut RVec<Item>,
    taking: ROption<usize>
)
[src]

Extends the RVec<Item> with the back of the self DoubleEndedIterator.

Extends buffer with as many elements of the iterator as taking specifies:

  • RNone: Yields all elements.Use this with care,since Iterators can be infinite.

  • RSome(n): Yields n elements.

Example


let mut wrapped=DynTrait::from_any_value(0..=3 ,DEIteratorInterface::NEW);

let mut buffer=vec![ 101,102,103 ].into_c();
wrapped.extending_rvec_back(&mut buffer,RNone);
assert_eq!(
    &buffer[..],
    &*vec![101,102,103,3,2,1,0]
)

Trait Implementations

impl<'borr, P, I, EV> GetStaticEquivalent_ for DynTrait<'borr, P, I, EV> where
    P: GetPointerKind,
    P: __StableAbi,
    I: __StableAbi,
    EV: __StableAbi,
    I: InterfaceBound,
    VTable<'borr, P, I>: SharedStableAbi
[src]

impl<'borr, P, I, EV> SharedStableAbi for DynTrait<'borr, P, I, EV> where
    P: GetPointerKind,
    P: __StableAbi,
    I: __StableAbi,
    EV: __StableAbi,
    I: InterfaceBound,
    VTable<'borr, P, I>: SharedStableAbi
[src]

type IsNonZeroType = False

Whether this type has a single invalid bit-pattern. Read more

type Kind = __ValueKind

The kind of abi stability of this type,there are 2: Read more

impl<'borr, P, I, EV> DynTraitBound<'borr> for DynTrait<'borr, P, I, EV> where
    P: Deref + GetPointerKind,
    I: InterfaceBound
[src]

type Interface = I

impl<'borr, P, I, EV> Send for DynTrait<'borr, P, I, EV> where
    P: Send + GetPointerKind,
    I: InterfaceBound<Send = Implemented<Send>>, 
[src]

impl<'borr, P, I, EV> Drop for DynTrait<'borr, P, I, EV> where
    P: GetPointerKind
[src]

impl<P, I, EV> Eq for DynTrait<'static, P, I, EV> where
    Self: PartialEq,
    P: Deref + GetPointerKind,
    I: InterfaceBound<Eq = Implemented<Eq>>, 
[src]

impl<'borr, P, I, EV> Sync for DynTrait<'borr, P, I, EV> where
    P: Sync + GetPointerKind,
    I: InterfaceBound<Sync = Implemented<Sync>>, 
[src]

impl<'borr, P, I, EV> Clone for DynTrait<'borr, P, I, EV> where
    P: Deref + GetPointerKind,
    I: InterfaceBound,
    Self: CloneImpl<<P as GetPointerKind>::Kind>, 
[src]

Clone is implemented for references and smart pointers, using GetPointerKind to decide whether P is a smart pointer or a reference.

DynTrait does not implement Clone if P==&mut () :

This example deliberately fails to compile

let mut object=DynTrait::from_any_value((),());
let borrow=object.reborrow_mut();
let _=borrow.clone();

impl<'borr, P, I, Item, EV> DoubleEndedIterator for DynTrait<'borr, P, I, EV> where
    Self: Iterator<Item = Item>,
    P: DerefMut + GetPointerKind,
    I: IteratorItemOrDefault<'borr, Item = Item>,
    I: InterfaceBound<DoubleEndedIterator = Implemented<DoubleEndedIterator>>,
    Item: 'borr, 
[src]

impl<P, P2, I, EV, EV2> PartialOrd<DynTrait<'static, P2, I, EV2>> for DynTrait<'static, P, I, EV> where
    P: Deref + GetPointerKind,
    P2: Deref + GetPointerKind,
    I: InterfaceBound<PartialOrd = Implemented<PartialOrd>>,
    Self: PartialEq<DynTrait<'static, P2, I, EV2>>, 
[src]

impl<P, P2, I, EV, EV2> PartialEq<DynTrait<'static, P2, I, EV2>> for DynTrait<'static, P, I, EV> where
    P: Deref + GetPointerKind,
    P2: Deref + GetPointerKind,
    I: InterfaceBound<PartialEq = Implemented<PartialEq>>, 
[src]

impl<P, I, EV> Ord for DynTrait<'static, P, I, EV> where
    P: Deref + GetPointerKind,
    I: InterfaceBound<Ord = Implemented<Ord>>,
    Self: PartialOrd + Eq
[src]

impl<'borr, P, I, Item, EV> Iterator for DynTrait<'borr, P, I, EV> where
    P: DerefMut + GetPointerKind,
    I: IteratorItemOrDefault<'borr, Item = Item>,
    I: InterfaceBound<Iterator = Implemented<Iterator>>,
    Item: 'borr, 
[src]

type Item = Item

The type of the elements being iterated over.

impl<'borr, P, I, EV> Hash for DynTrait<'borr, P, I, EV> where
    P: Deref + GetPointerKind,
    I: InterfaceBound<Hash = Implemented<Hash>>, 
[src]

impl<'borr, P, I, EV> Debug for DynTrait<'borr, P, I, EV> where
    P: Deref + GetPointerKind,
    I: InterfaceBound<Debug = Implemented<Debug>>, 
[src]

impl<'borr, P, I, EV> Display for DynTrait<'borr, P, I, EV> where
    P: Deref + GetPointerKind,
    I: InterfaceBound<Display = Implemented<Display>>, 
[src]

impl<'borr, P, I, EV> Write for DynTrait<'borr, P, I, EV> where
    P: DerefMut + GetPointerKind,
    I: InterfaceBound<FmtWrite = Implemented<FmtWrite>>, 
[src]

impl<'borr, P, I, EV> Error for DynTrait<'borr, P, I, EV> where
    P: Deref + GetPointerKind,
    I: InterfaceBound<Display = Implemented<Display>, Debug = Implemented<Debug>, Error = Implemented<Error>>, 
[src]

impl<'borr, P, I, EV> Read for DynTrait<'borr, P, I, EV> where
    P: DerefMut + GetPointerKind,
    I: InterfaceBound<IoRead = Implemented<IoRead>>, 
[src]

impl<'borr, P, I, EV> BufRead for DynTrait<'borr, P, I, EV> where
    P: DerefMut + GetPointerKind,
    I: InterfaceBound<IoRead = Implemented<IoRead>, IoBufRead = Implemented<IoBufRead>>, 
[src]

impl<'borr, P, I, EV> Seek for DynTrait<'borr, P, I, EV> where
    P: DerefMut + GetPointerKind,
    I: InterfaceBound<IoSeek = Implemented<IoSeek>>, 
[src]

impl<'borr, P, I, EV> Write for DynTrait<'borr, P, I, EV> where
    P: DerefMut + GetPointerKind,
    I: InterfaceBound<IoWrite = Implemented<IoWrite>>, 
[src]

impl<'borr, P, I, EV> Serialize for DynTrait<'borr, P, I, EV> where
    P: Deref + GetPointerKind,
    I: InterfaceBound<Serialize = Implemented<Serialize>>,
    I: GetSerializeProxyType<'borr>,
    I::ProxyType: Serialize
[src]

First it serializes a DynTrait<_> into a string by using ::serialize_impl, then it serializes the string.

impl<'de, 'borr: 'de, P, I, EV> Deserialize<'de> for DynTrait<'borr, P, I, EV> where
    EV: 'borr,
    P: Deref + GetPointerKind + 'borr,
    I: InterfaceBound + 'borr,
    I: DeserializeDyn<'de, Self>,
    <I as DeserializeDyn<'de, Self>>::Proxy: Deserialize<'de>, 
[src]

First it Deserializes a string,then it deserializes into a DynTrait<_>,by using <I as DeserializeOwnedInterface>::deserialize_impl.

Auto Trait Implementations

impl<'borr, P, I, EV> Unpin for DynTrait<'borr, P, I, EV> where
    EV: Unpin,
    P: Unpin

impl<'borr, P, I, EV> UnwindSafe for DynTrait<'borr, P, I, EV> where
    EV: UnwindSafe,
    P: UnwindSafe

impl<'borr, P, I, EV = ()> !RefUnwindSafe for DynTrait<'borr, P, I, EV>

Blanket Implementations

impl<This> GetConstGenericVTable for This where
    This: StableAbi + Eq + PartialEq<This> + Debug + Send + Sync
[src]

impl<This> StableAbi for This where
    This: SharedStableAbi<Kind = ValueKind>, 
[src]

impl<This> TransmuteElement for This where
    This: ?Sized
[src]

impl<'a, T> BorrowOwned<'a> for T where
    T: 'a + Clone
[src]

type ROwned = T

type RBorrowed = &'a T

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> From<T> for T[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> TypeIdentity for T where
    T: ?Sized
[src]

type Type = T

The same type as Self. Read more

impl<I> IteratorExt for I where
    I: Iterator
[src]

impl<T> SelfOps for T where
    T: ?Sized
[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The error type returned when the conversion fails.

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]