Skip to main content

Irc

Struct Irc 

Source
pub struct Irc<T: IrcItem<Tag>, Tag> { /* private fields */ }
Available on crate feature irc only.
Expand description

Intrusive reference counter, which support conversion bwteween Box<T>.

It does not support weak reference.

Implementations§

Source§

impl<T: IrcItem<Tag>, Tag> Irc<T, Tag>

Source

pub fn new(inner: T) -> Self

Wrap a stack value T into Irc.

The counter will be reset to 1 on initialization.

Source

pub fn ptr_eq(this: &Self, other: &Self) -> bool

Source

pub fn is_unique(&self) -> bool

If is_unique returns true, then this thread is the only owner

§False negative

it’s possible to return false when counter drop to 1, Because of using Acquire load and Release on drop.

§Example
use embed_collections::irc::{Irc, IrcItem};
use core::sync::atomic::AtomicUsize;

struct Tag;

struct MyItem {
    value: i32,
    counter: AtomicUsize,
}

unsafe impl IrcItem<Tag> for MyItem {
    type Counter = AtomicUsize;
    fn counter(&self) -> &Self::Counter {
        &self.counter
    }
}

// Create a new Irc
let irc1 = Irc::<_, Tag>::new(MyItem { value: 10, counter: AtomicUsize::new(0) });
assert_eq!(irc1.value, 10);
assert!(irc1.is_unique());

// Clone the Irc
let irc2 = irc1.clone();
assert_eq!(irc1.strong_count(), 2);
assert!(!irc1.is_unique());
Source

pub fn get_mut(this: &mut Self) -> Option<&mut T>

return mutable reference if we are the only owner

§False negative

It can return None even when only one reference left

Source§

impl<T: IrcItem<Tag> + Clone, Tag> Irc<T, Tag>

Source

pub fn make_mut(this: &mut Self) -> &mut T

The Cow function, the same as Arc::make_mut()

§Example
use embed_collections::irc::{Irc, IrcItem};
use core::sync::atomic::AtomicUsize;

struct Tag;
struct MyItem {
    value: i32,
    counter: AtomicUsize,
}

impl Clone for MyItem {
    fn clone(&self) -> Self {
        Self { value: self.value, counter: AtomicUsize::new(0) }
    }
}

unsafe impl IrcItem<Tag> for MyItem {
    type Counter = AtomicUsize;
    fn counter(&self) -> &Self::Counter {
        &self.counter
    }
}

let mut irc1 = Irc::<_, Tag>::new(MyItem { value: 10, counter: AtomicUsize::new(0) });
let irc2 = irc1.clone();

// This will clone the inner item because it's shared
let m = Irc::make_mut(&mut irc1);
m.value = 20;

assert_eq!(irc1.value, 20);
assert_eq!(irc2.value, 10);

Trait Implementations§

Source§

impl<T: IrcItem<Tag>, Tag> AsRef<T> for Irc<T, Tag>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: IrcItem<Tag>, Tag> Clone for Irc<T, Tag>

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<T: IrcItem<Tag> + Debug, Tag> Debug for Irc<T, Tag>

Source§

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

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

impl<T: IrcItem<Tag>, Tag> Deref for Irc<T, Tag>

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T: IrcItem<Tag> + Display, Tag> Display for Irc<T, Tag>

Source§

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

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

impl<T: IrcItem<Tag>, Tag> Drop for Irc<T, Tag>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: IrcItem<Tag>, Tag> From<Box<T>> for Irc<T, Tag>

Source§

fn from(inner: Box<T>) -> Self

Convert a boxed T into Irc.

The counter will be reset to 1 on initialization.

Source§

impl<T: IrcItem<Tag>, Tag> Pointer for Irc<T, Tag>

Source§

unsafe fn from_raw(p: *const Self::Target) -> Self

§Safety

must be pointer acquire from Irc::into_raw()

Source§

type Target = T

Source§

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

Source§

fn into_raw(self) -> *const Self::Target

Source§

fn as_ptr(&self) -> *const Self::Target

Source§

impl<T: IrcItem<Tag>, Tag> SmartPointer for Irc<T, Tag>

Source§

fn new(inner: T) -> Self

Source§

impl<T: IrcItem<Tag>, Tag> Send for Irc<T, Tag>

Source§

impl<T: IrcItem<Tag>, Tag> Sync for Irc<T, Tag>

Auto Trait Implementations§

§

impl<T, Tag> Freeze for Irc<T, Tag>

§

impl<T, Tag> RefUnwindSafe for Irc<T, Tag>
where T: RefUnwindSafe,

§

impl<T, Tag> Unpin for Irc<T, Tag>

§

impl<T, Tag> UnsafeUnpin for Irc<T, Tag>

§

impl<T, Tag> UnwindSafe for Irc<T, Tag>
where T: RefUnwindSafe,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.