Struct Box

Source
pub struct Box<T: ?Sized + 'static> { /* private fields */ }
Expand description

A linked object that acts like a std::boxed::Box<dyn MyTrait>.

Intended to represent linked instances of T where T: MyTrait. This is for use with types that are always exposed to user code as trait objects via linked::Box<dyn MyTrait>.

The Box itself implements the linked object mechanics from #[linked::object]. The type T does not need to implement the mechanics of the linked object pattern itself and must not be decorated with the #[linked::object] attribute.

§Usage

Use it like a regular std::boxed::Box<T> that also happens to support the linked object mechanisms via the linked::instance_per_access! or linked::instance_per_thread! macros or PerThread<T> and offers the API surface for handle-based transfer across threads via .handle().

§Implementation

Instead of a typical constructor, create one that returns linked::Box<dyn MyTrait>. Inside this constructor, create a linked::Box instance using the linked::new_box! macro. The first macro parameter is the type inside the box, and the second is a Self struct-expression to create one instance of the implementation type.

// If using linked::Box, do not put `#[linked::object]` on the struct.
// The linked::Box itself is the linked object and our struct is only its contents.
struct XmlConfig {
    config: String
}

impl XmlConfig {
    pub fn new_as_config_source() -> linked::Box<dyn ConfigSource> {
        // Constructing instances works logically the same as for regular linked objects.
        //
        // The only differences are:
        // 1. We use `linked::new_box!` instead of `linked::new!`
        // 2. There is an additional parameter to the macro to name the trait object type
        linked::new_box!(
            dyn ConfigSource,
            Self {
                config: "xml".to_string(),
            }
        )
    }
}

Any connections between the instances should be established via the captured state of this closure (e.g. sharing an Arc or setting up messaging channels).

§Example

Using the linked objects as linked::Box<dyn ConfigSource>, without the user code knowing the exact type of the object in the box:

trait ConfigSource {
    fn config(&self) -> String;
}

struct XmlConfig {}
struct IniConfig {}

impl ConfigSource for XmlConfig {
    fn config(&self) -> String {
        "xml".to_string()
    }
}

impl ConfigSource for IniConfig {
    fn config(&self) -> String {
        "ini".to_string()
    }
}

impl XmlConfig {
    pub fn new_as_config_source() -> linked::Box<dyn ConfigSource> {
        linked::new_box!(
            dyn ConfigSource,
            XmlConfig {}
        )
    }
}

impl IniConfig {
    pub fn new_as_config_source() -> linked::Box<dyn ConfigSource> {
        linked::new_box!(
            dyn ConfigSource,
            IniConfig {}
        )
    }
}

let xml_config = XmlConfig::new_as_config_source();
let ini_config = IniConfig::new_as_config_source();

let configs = [xml_config, ini_config];

assert_eq!(configs[0].config(), "xml".to_string());
assert_eq!(configs[1].config(), "ini".to_string());

Trait Implementations§

Source§

impl<T: ?Sized + 'static> Clone for Box<T>

Source§

fn clone(&self) -> Self

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: Debug + ?Sized + 'static> Debug for Box<T>

Source§

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

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

impl<T: ?Sized + 'static> Deref for Box<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T: ?Sized + 'static> DerefMut for Box<T>

Source§

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

Mutably dereferences the value.
Source§

impl<T: ?Sized + 'static> From<Handle<Box<T>>> for Box<T>

Source§

fn from(handle: Handle<Box<T>>) -> Self

Converts to this type from the input type.
Source§

impl<T: ?Sized + 'static> Object for Box<T>

Source§

fn handle(&self) -> Handle<Self>

Gets a thread-safe handle that can be used to create linked instances on other threads. Read more

Auto Trait Implementations§

§

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

§

impl<T> !RefUnwindSafe for Box<T>

§

impl<T> !Send for Box<T>

§

impl<T> !Sync for Box<T>

§

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

§

impl<T> !UnwindSafe for Box<T>

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