AnyHandle

Struct AnyHandle 

Source
pub struct AnyHandle<T>(/* private fields */)
where
    T: ?Sized;
Expand description

A thread-safe shared pointer to a value of any Any type, allowing for downcasting.

Internally, this uses RwLock, allowing for multiple concurrent readers or a single writer.

§Example

use any_handle::{AnyHandle, Any};

struct SomeStruct (i32);

impl SomeStruct {
    fn do_things_with(&self) {}
    fn do_mut_things_with(&mut self) {}
}

fn demo() -> Option<()> {
    // Initialize a handle with an unknown type.
    // If you want to pass in a Box<dyn SomeOtherTrait>, instead of a concrete
    // type, you will have to use `#![feature(trait_upcasting)]`, unfortunately.
    let handle : AnyHandle<dyn Any> = AnyHandle::new(Box::new(SomeStruct(12)));
    // Now we can put it in some sort of generic container...

    // ...and when we retrieve it later:
    let mut handle : AnyHandle<SomeStruct> = handle.downcast().ok()?;
    handle.write().do_mut_things_with();
    handle.read().do_things_with();
    Some(())
}

fn main() { demo().unwrap() }

Implementations§

Source§

impl AnyHandle<dyn Any>

Source

pub fn new(inner: Box<dyn Any>) -> AnyHandle<dyn Any>

Initialize an AnyHandle from a Box<dyn Any>.

Source

pub fn downcast<Y>(self) -> Result<AnyHandle<Y>, AnyHandle<dyn Any>>
where Y: 'static,

Downcast this handle from dyn Any to a specific type. If the stored data can be downcast to type Y, succeeds and returns Ok(the cast AnyHandle). If the data cannot be downcast, errors and returns Error(self).

You may also downcast using Option<AnyHandle<T>>::from.

Source§

impl<T> AnyHandle<T>
where T: ?Sized,

Source

pub fn read(&self) -> AnyHandleReadGuard<'_, T>

Get a ‘read guard’ that allows for reading from the object. Any number of read guards can exist at a given time, but not at the same time as any write guards, so this may block or result in deadlocks if used improperly.

Source

pub fn write(&mut self) -> AnyHandleWriteGuard<'_, T>

Get a ‘write guard’ that allows for writing to the object. Only one write guard can exist at a given time for an object, and not at the same time as any read guards, so this may block or result in deadlocks if used improperly.

Source

pub fn reference_count(&self) -> usize

Get a count of the number of living references to this object.

Trait Implementations§

Source§

impl<T> Clone for AnyHandle<T>
where T: ?Sized,

Source§

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

Make a new copy of this handle. This will not copy the object within, and will increase the reference count.

1.0.0 · Source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

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

§

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

§

impl<T> !Send for AnyHandle<T>

§

impl<T> !Sync for AnyHandle<T>

§

impl<T> Unpin for AnyHandle<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for AnyHandle<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> 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 = 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.