SharedPtr

Struct SharedPtr 

Source
#[repr(C)]
pub struct SharedPtr<T>
where T: SharedPtrTarget,
{ /* private fields */ }
Expand description

Binding to C++ std::shared_ptr<T>.

WARNING: Unlike Rust’s Arc<T>, a C++ shared pointer manipulates pointers to 2 separate objects in general.

  1. One is the managed pointer, and its identity is associated with shared ownership of a strong and weak count shared by other SharedPtr and WeakPtr instances having the same managed pointer.

  2. The other is the stored pointer, which is commonly either the same as the managed pointer, or is a pointer into some member of the managed object, but can be any unrelated pointer in general.

The managed pointer is the one passed to a deleter upon the strong count reaching zero, but the stored pointer is the one accessed by deref operations and methods such as is_null.

A shared pointer is considered empty if the strong count is zero, meaning the managed pointer has been deleted or is about to be deleted. A shared pointer is considered null if the stored pointer is the null pointer. All combinations are possible. To be explicit, a shared pointer can be nonempty and nonnull, or nonempty and null, or empty and nonnull, or empty and null. In general all of these cases need to be considered when handling a SharedPtr.

Implementations§

Source§

impl<T> SharedPtr<T>
where T: SharedPtrTarget,

Source

pub fn null() -> SharedPtr<T>

Makes a new SharedPtr that is both empty and null.

Matches the behavior of default-constructing a std::shared_ptr.

Source

pub fn new(value: T) -> SharedPtr<T>
where T: ExternType<Kind = Trivial>,

Allocates memory on the heap and makes a SharedPtr owner for it.

The shared pointer will be nonempty and nonnull.

Source

pub unsafe fn from_raw(raw: *mut T) -> SharedPtr<T>

Creates a shared pointer from a C++ heap-allocated pointer.

Matches the behavior of std::shared_ptr’s constructor explicit shared_ptr(T*).

The SharedPtr gains ownership of the pointer and will call std::default_delete on it when the refcount goes to zero.

The object pointed to by the input pointer is not relocated by this operation, so any pointers into this data structure elsewhere in the program continue to be valid.

The resulting shared pointer is nonempty regardless of whether the input pointer is null, but may be either null or nonnull.

§Panics

Panics if T is an incomplete type (including void) or is not destructible.

§Safety

Pointer must either be null or point to a valid instance of T heap-allocated in C++ by new.

Source

pub fn is_null(&self) -> bool

Checks whether the SharedPtr holds a null stored pointer.

This is the opposite of std::shared_ptr<T>::operator bool.

This method is unrelated to the state of the reference count. It is possible to have a SharedPtr that is nonnull but empty (has a refcount of 0), typically from having been constructed using the alias constructors in C++. Inversely, it is also possible to be null and nonempty.

Source

pub fn as_ref(&self) -> Option<&T>

Returns a reference to the object pointed to by the stored pointer if nonnull, otherwise None.

The shared pointer’s managed object may or may not already have been destroyed.

Source

pub unsafe fn pin_mut_unchecked(&mut self) -> Pin<&mut T>

Returns a mutable pinned reference to the object pointed to by the stored pointer.

The shared pointer’s managed object may or may not already have been destroyed.

§Panics

Panics if the SharedPtr holds a null stored pointer.

§Safety

This method makes no attempt to ascertain the state of the reference count. In particular, unlike Arc::get_mut, we do not enforce absence of other SharedPtr and WeakPtr referring to the same data as this one. As always, it is Undefined Behavior to have simultaneous references to the same value while a Rust exclusive reference to it exists anywhere in the program.

For the special case of CXX opaque C++ types, this method can be used to safely call thread-safe non-const member functions on a C++ object without regard for whether the reference is exclusive. This capability applies only to opaque types extern "C++" { type T; }. It does not apply to extern types defined with a non-opaque Rust representation extern "C++" { type T = ...; }.

Source

pub fn as_ptr(&self) -> *const T

Returns the SharedPtr’s stored pointer as a raw const pointer.

Source

pub fn as_mut_ptr(&self) -> *mut T

Returns the SharedPtr’s stored pointer as a raw mutable pointer.

As with std::shared_ptr<T>::get, this doesn’t require that you hold an exclusive reference to the SharedPtr. This differs from Rust norms, so extra care should be taken in the way the pointer is used.

Source

pub fn downgrade(&self) -> WeakPtr<T>
where T: WeakPtrTarget,

Constructs new WeakPtr as a non-owning reference to the object managed by self. If self manages no object, the WeakPtr manages no object too.

Matches the behavior of std::weak_ptr<T>::weak_ptr(const std::shared_ptr<T> &).

Trait Implementations§

Source§

impl<T> Clone for SharedPtr<T>
where T: SharedPtrTarget,

Source§

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

Returns a duplicate 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 for SharedPtr<T>

Source§

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

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

impl<T> Deref for SharedPtr<T>
where T: SharedPtrTarget,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<SharedPtr<T> as Deref>::Target

Dereferences the value.
Source§

impl<T> Display for SharedPtr<T>

Source§

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

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

impl<T> Drop for SharedPtr<T>
where T: SharedPtrTarget,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> From<UniquePtr<T>> for SharedPtr<T>

Source§

fn from(unique: UniquePtr<T>) -> SharedPtr<T>

Converts to this type from the input type.
Source§

impl<T> Hash for SharedPtr<T>
where T: Hash + SharedPtrTarget,

Source§

fn hash<H>(&self, hasher: &mut H)
where H: Hasher,

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 for SharedPtr<T>
where T: Ord + SharedPtrTarget,

Source§

fn cmp(&self, other: &SharedPtr<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 for SharedPtr<T>

Source§

fn eq(&self, other: &SharedPtr<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 for SharedPtr<T>

Source§

fn partial_cmp(&self, other: &SharedPtr<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 for SharedPtr<T>
where T: Eq + SharedPtrTarget,

Source§

impl<T> Send for SharedPtr<T>
where T: Send + Sync + SharedPtrTarget,

Source§

impl<T> Sync for SharedPtr<T>
where T: Send + Sync + SharedPtrTarget,

Source§

impl<T> Unpin for SharedPtr<T>
where T: SharedPtrTarget,

Auto Trait Implementations§

§

impl<T> Freeze for SharedPtr<T>

§

impl<T> RefUnwindSafe for SharedPtr<T>
where T: RefUnwindSafe,

§

impl<T> UnwindSafe for SharedPtr<T>
where T: UnwindSafe,

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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> QuickClone<T> for T
where T: Clone,

Source§

fn C(&self) -> T

Source§

impl<T> QuickToOwned for T
where T: ToOwned,

Source§

type Owned = <T as ToOwned>::Owned

Source§

fn O(&self) -> <T as QuickToOwned>::Owned

Source§

impl<T> QuickToString for T
where T: ToString + ?Sized,

Source§

fn S(&self) -> String

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> Same for T

Source§

type Output = T

Should always be Self
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> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

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