Struct Sc

Source
pub struct Sc<T: ?Sized, A = System>
where A: GlobalAlloc,
{ /* private fields */ }
Expand description

A single-threaded strong reference-counting pointer. ‘Sc’ stands for ‘Strong Counted.’

It behaves like std::rc::Rc except for that this treats only strong pointer; i.e. Sc gives up weak pointer for the performance.

The inherent methods of Sc are all associated funcitons, which means that you have to call them as e.g., Sc::get_mut(&mut value) instead of value.get_mut() . This avoids conflicts with methods of the inner type T .

Implementations§

Source§

impl<T, A> Sc<T, A>
where A: GlobalAlloc,

Source

pub fn new(val: T, alloc: A) -> Self

Creates a new instance.

§Examples
use std::alloc::System;
use counting_pointer::Sc;

let _five = Sc::new(5, System);
Source

pub fn from_slice_alloc(vals: &[T], alloc: A) -> Sc<[T], A>
where T: Clone,

Creates a new instance of Sc<[T], A> .

§Examples
use std::alloc::System;
use counting_pointer::Sc;

let vals: [i32; 4] = [0, 1, 2, 3];
let sc = Sc::from_slice_alloc(&vals, System);
assert_eq!(&vals, &*sc);
Source§

impl<A> Sc<dyn Any, A>
where A: GlobalAlloc,

Source

pub fn new_any<T>(val: T, alloc: A) -> Self
where T: Any,

Creates Sc<dyn Any> instance.

§Examples
use std::alloc::System;
use counting_pointer::Sc;

let _five = Sc::new_any(5, System);
Source§

impl<T, A> Sc<T, A>
where A: GlobalAlloc,

Source

pub fn to_any(this: Self) -> Sc<dyn Any, A>
where T: Any,

Consumes this, returning Sc<dyn Any, A>

§Examples
use counting_pointer::Sc;

let sc: Sc<i32> = Sc::from(6);
let any = Sc::to_any(sc);
Source§

impl<T: ?Sized, A> Sc<T, A>
where A: GlobalAlloc,

Source

pub fn as_ptr(this: &Self) -> *const T

Provides a raw pointer to the data.

The counts are not affected in any way and the Sc is not consumed. The pointer is valid for as long as another Sc instance is pointing to the address.

§Examples
use counting_pointer::Sc;

let x: Sc<String> = Sc::from(String::from("Hello"));
let x_ptr = Sc::as_ptr(&x);
assert_eq!("Hello", unsafe { &*x_ptr });
Source

pub fn count(this: &Self) -> usize

Returns the number of Sc pointers pointing to the same address.

§Examples
use counting_pointer::Sc;

let five: Sc<i32> = Sc::from(5);
assert_eq!(1, Sc::count(&five));

let _also_five = five.clone();
assert_eq!(2, Sc::count(&five));
assert_eq!(2, Sc::count(&_also_five));

drop(five);
assert_eq!(1, Sc::count(&_also_five));
Source

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

Returns a mutable reference into the given Sc , if no other Sc instance is pointing to the same address; otherwise returns None .

See also make_mut , which will clone the inner value when some other Sc instances are.

§Examples
use counting_pointer::Sc;

let mut x: Sc<i32> = Sc::from(3);
assert_eq!(3, *x);

*Sc::get_mut(&mut x).unwrap() = 4;
assert_eq!(4, *x);

let _y = x.clone();
let n = Sc::get_mut(&mut x);
assert!(n.is_none());
Source

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

Returns true if the two Sc instances point to the same address, or false .

§Examples
use counting_pointer::Sc;

let five: Sc<i32> = Sc::from(5);
let same_five = five.clone();
let other_five: Sc<i32> = Sc::from(5);

assert_eq!(true, Sc::ptr_eq(&five, &same_five));
assert_eq!(false, Sc::ptr_eq(&five, &other_five));
Source

pub fn into_raw_alloc(this: Self) -> (*const T, A)

Consumes this , returning the wrapped pointer and the allocator.

To avoid memory leak, the returned pointer must be converted back to an Sc using from_raw_alloc .

Using this function and from_raw_alloc , user can create an Sc<T: ?Sized> instance.

§Examples
use counting_pointer::Sc;

let sc: Sc<String> = Sc::from("Foo".to_string());
let (ptr, alloc) = Sc::into_raw_alloc(sc);
let _sc: Sc<dyn AsRef<str>> = unsafe { Sc::from_raw_alloc(ptr, alloc) };
Source

pub unsafe fn from_raw_alloc(ptr: *const T, alloc: A) -> Self

Constructs a new instance from a raw pointer and allocator.

The raw pointer must have been previously returned by a call to into_raw_alloc .

Using this function and into_raw_alloc , user can create an Sc<T: ?Sized> instance.

§Safety

It may lead to memory unsafety to use improperly, even if the returned value will never be accessed.

§Examples
use counting_pointer::Sc;

let sc: Sc<String> = Sc::from("Foo".to_string());
let (ptr, alloc) = Sc::into_raw_alloc(sc);
let _sc: Sc<dyn AsRef<str>> = unsafe { Sc::from_raw_alloc(ptr, alloc) };
Source§

impl<T: Clone, A> Sc<T, A>
where A: GlobalAlloc + Clone,

Source

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

Makes a mutable reference into the given Sc .

If another Sc instance is pointing to the same address, make_mut will clone the inner value to a new allocation to ensure unique ownership.

See also get_mut , which will fail rather than cloning.

§Examples
use counting_pointer::Sc;

let mut data: Sc<i32> = Sc::from(5);
assert_eq!(5, *data);

*Sc::make_mut(&mut data) += 1; // Won't clone anything.
assert_eq!(6, *data);

let mut data2 = data.clone();  // Won't clone the inner data.
*Sc::make_mut(&mut data) += 1; // Clones inner data.
assert_eq!(7, *data);
assert_eq!(6, *data2);
Source§

impl<A> Sc<dyn Any, A>
where A: GlobalAlloc,

Source

pub fn downcast<T: Any>(self) -> Result<Sc<T, A>, Self>

Attempts to downcast the Sc<dyn Any, A> to a concrete type.

§Examples
use std::alloc::System;
use std::any::Any;
use counting_pointer::Sc;

let sc = Sc::new_any(8 as i32, System);

let success = Sc::downcast::<i32>(sc.clone()).unwrap();
assert_eq!(8, *success);

let fail = Sc::downcast::<String>(sc.clone());
assert_eq!(true, fail.is_err());

Trait Implementations§

Source§

impl<T: ?Sized, A> AsRef<T> for Sc<T, A>
where A: GlobalAlloc,

Source§

fn as_ref(&self) -> &T

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

impl<T: ?Sized, A> Borrow<T> for Sc<T, A>
where A: GlobalAlloc,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T: ?Sized, A> Clone for Sc<T, A>
where A: Clone + GlobalAlloc,

Source§

fn clone(&self) -> Self

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: ?Sized, A> Debug for Sc<T, A>
where A: Debug + GlobalAlloc,

Source§

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

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

impl<T, A> Default for Sc<T, A>
where T: Default, A: Default + GlobalAlloc,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: ?Sized, A> Deref for Sc<T, A>
where A: GlobalAlloc,

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T, A> Display for Sc<T, A>
where T: Display + ?Sized, A: GlobalAlloc,

Source§

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

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

impl<T: ?Sized, A> Drop for Sc<T, A>
where A: GlobalAlloc,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T, A> From<&[T]> for Sc<[T], A>
where T: Clone, A: Default + GlobalAlloc,

Source§

fn from(vals: &[T]) -> Self

Converts to this type from the input type.
Source§

impl<T, A> From<T> for Sc<T, A>
where A: Default + GlobalAlloc,

Source§

fn from(val: T) -> Self

Converts to this type from the input type.
Source§

impl<T, A> Hash for Sc<T, A>
where T: Hash + ?Sized, A: GlobalAlloc,

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, A> Ord for Sc<T, A>
where T: Ord + ?Sized, A: GlobalAlloc,

Source§

fn cmp(&self, other: &Self) -> 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, A> PartialEq for Sc<T, A>
where T: PartialEq + ?Sized, A: GlobalAlloc,

Source§

fn eq(&self, other: &Self) -> 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, A> PartialOrd for Sc<T, A>
where T: PartialOrd + ?Sized, A: GlobalAlloc,

Source§

fn partial_cmp(&self, other: &Self) -> 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, A> Eq for Sc<T, A>
where T: Eq + ?Sized, A: GlobalAlloc,

Auto Trait Implementations§

§

impl<T, A> Freeze for Sc<T, A>
where A: Freeze, T: ?Sized,

§

impl<T, A> RefUnwindSafe for Sc<T, A>

§

impl<T, A = System> !Send for Sc<T, A>

§

impl<T, A = System> !Sync for Sc<T, A>

§

impl<T, A> Unpin for Sc<T, A>
where A: Unpin, T: ?Sized,

§

impl<T, A> UnwindSafe for Sc<T, A>
where A: UnwindSafe, T: RefUnwindSafe + ?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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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.