Src

Struct Src 

Source
pub struct Src<T: SrcTarget + ?Sized> { /* private fields */ }
Expand description

A single-threaded sliceable reference-counting pointer. ‘Src’ stands for ‘Slice Reference Counted’.

See std::rc for details about general reference-counting pointers.

Src is a variation on Rc; the functionality that differentiates them can be accessed with the method Src::slice.

Many of the inherent methods of Src are associated functions, which means that you have to call them as e.g., Src::downgrade(&value) instead of value.downgrade(); this avoids conflicts with methods of the inner type T. However, some methods, e.g. Src::slice, are permitted to remain as methods because the inner type is known not to have a conflicting method, and some, e.g. Src::len, intentionally shadow a known method of the inner type because they use a more efficient computation for the same result.

Implementations§

Source§

impl<T: SrcTarget + ?Sized> Src<T>

Source

pub fn ptr_eq<U: SrcTarget<Item = T::Item> + ?Sized>( this: &Src<T>, other: &Src<U>, ) -> bool

Returns true if the two Srcs point to slices starting at the same location in memory, akin to ptr::eq.

use slice_rc::Src;
 
let slice = Src::from_array([1, 2, 3]);
let same_slice = Src::clone(&slice);
let sub_slice = slice.slice(1..);
let other_slice = Src::from_array([1, 2, 3]);
 
assert!(Src::ptr_eq(&slice, &same_slice));
assert!(!Src::ptr_eq(&slice, &sub_slice));
assert!(!Src::ptr_eq(&slice, &other_slice));

If Src::ptr_eq(&a, &b) returns true, then Src::same_root(&a, &b) will also be true.

The type parameter, U, is to allow Srcs of different types that could be of the same allocation, and therefore, could be equal by pointer, to be compared, e.g.:

let single: Src<i32> = Src::single(4);
let slice: Src<[i32]> = Src::as_slice(&single);
 
assert!(Src::ptr_eq(&single, &slice));

Note that this method currently ignores the length of the slice:

let root = Src::from_array([1, 2, 3]);
let first = root.slice(0);
 
assert!(Src::ptr_eq(&root, &first));
 
let mid_to_end_slice = root.slice(1..);
let mid_slice = root.slice(1..=1);
 
assert!(Src::ptr_eq(&mid_to_end_slice, &mid_slice));

It is undecided whether this behavior is desireable, and as such, it may change; notably, Rc::ptr_eq does ignore metadata for ?Sized types (though that’s irrelevant for slices because Rcs can only point to the whole slice, and therefore the length will always be the same for Rcs that point to the same allocation), while ptr::eq does consider the metadata (which causes inconsistent results for trait objects, but that is irrelevant here because Srcs don’t support trait objects).

See also Src::same_root.

Source

pub fn same_root<U: SrcTarget<Item = T::Item> + ?Sized>( this: &Src<T>, other: &Src<U>, ) -> bool

Returns true if the two Srcs share the same root (i.e., they point to parts of the same allocation).

use slice_rc::Src;
 
let slice = Src::from_array([1, 2, 3]);
let same_slice = Src::clone(&slice);
let other_slice = Src::from_array([1, 2, 3]);
 
assert!(Src::same_root(&slice, &same_slice));
assert!(!Src::same_root(&slice, &other_slice));

Notably, neither slice has to be the root, nor do they need to overlap at all:

let root = Src::from_array([1, 2, 3]);
let a = root.slice(..1);
let b = root.slice(2..);
 
assert!(Src::same_root(&a, &b));

The type parameter, U, is to allow Srcs of different types that could share the same root, to be compared, e.g.:

let single: Src<i32> = Src::single(4);
let slice: Src<[i32]> = Src::as_slice(&single);
 
assert!(Src::same_root(&single, &slice));

This method ignores the length of the slices in question, but unlike Src::ptr_eq, this will not change, as the roots remains the same regardless of which parts of it are included in these slices.

See also Src::ptr_eq, Src::is_root, and Src::root.

Source

pub fn is_root(this: &Src<T>) -> bool

Returns true if this Src contains its root (i.e., it references its entire allocation). Notably, this Src does not have to be the first one that was initialized, it just has to cover the entire allocation.

use slice_rc::Src;
 
let root = Src::from_array([1, 2, 3]);
let also_root = root.slice(..);
let slice = root.slice(1..);
 
assert!(Src::is_root(&root));
assert!(Src::is_root(&also_root));
assert!(!Src::is_root(&slice));

See also Src::same_root and Src::root.

Source

pub fn downgrade(this: &Src<T>) -> WeakSrc<T>

Creates a WeakSrc pointer to this slice. The WeakSrc refers to the same slice as this Src, and therefore, refers to the root if and only if this Src does.

use slice_rc::Src;
 
let root = Src::from_array([1, 2, 3]);
 
let weak_root = Src::downgrade(&root);
Source

pub fn strong_count(this: &Src<T>) -> usize

Gets the number of strong (Src) pointers to any part of this allocation.

use slice_rc::Src;
 
let root = Src::from_array([1, 2, 3]);
let _slice = root.slice(1..);
 
assert_eq!(2, Src::strong_count(&root));
Source

pub fn weak_count(this: &Src<T>) -> usize

Gets the number of WeakSrc pointers to any part of this allocation.

use slice_rc::Src;
 
let root = Src::from_array([1, 2, 3]);
let slice = root.slice(1..);
let _weak_slice = Src::downgrade(&slice);
 
assert_eq!(1, Src::weak_count(&root));
Source

pub fn into_unique(this: Src<T>) -> Result<UniqueSrc<T>, Src<T>>

Turns this Src into a UniqueSrc, if it has only one strong reference.

Otherwise, an Err is returned withe the same Src that was passed in.

This will succeed even if there are outstanding weak references, though those references will not be able to upgrade while this allocation is managed by a UniqueSrc.

use slice_rc::Src;
 
let x = Src::single(3);
assert_eq!(*Src::into_unique(x).unwrap(), 3);
 
let x = Src::single(4);
let _y = Src::clone(&x);
assert_eq!(*Src::into_unique(x).unwrap_err(), 4);

See also Src::make_unique.

Note that this method (and Src::make_unique) can currently be used to construct non-root UniqueSrcs; this behavior has not been thouroghly considered and may be changed or removed in the future. As it is, the UniqueSrc will retain unique ownership of the whole allocation, even the parts it doesn’t contain. This means that a WeakSrc to any part of the allocation will not be able to upgrade, even if that WeakSrc does not overlap with the UniqueSrc.

Source

pub fn make_unique(this: Src<T>) -> UniqueSrc<T>
where T::Item: Clone,

If this is the only strong reference to this allocation, then it is turned into a UniqueSrc. Otherwise, the contents are cloned and return in a new UniqueSrc.

This is somewhat like Rc::make_mut, but with some subtle differences.

use slice_rc::{Src, UniqueSrc};
 
let data = Src::single(5);
let mut data = Src::make_unique(data);             // Won't clone anything
*data += 1;
let data = UniqueSrc::into_shared(data);
let other_data = Src::clone(&data);                // Won't clone inner data
let mut data = Src::make_unique(data);             // Clones inner data
*data += 1;
let data = UniqueSrc::into_shared(data);
let mut data = Src::make_unique(data);             // Won't clone anything
*data += 1;
let data = UniqueSrc::into_shared(data);
let mut other_data = Src::make_unique(other_data); // Won't clone anything
*other_data *= 2;
let other_data = UniqueSrc::into_shared(other_data);
 
// Now `data` and `other_data` point to different allocations.
assert_eq!(*data, 8);
assert_eq!(*other_data, 12);

If this is the only Src pointer to this allocation, but there are some WeakSrc pointers, then the WeakSrc pointers will be carried over to the UniqueSrc.

let data = Src::single(75);
let weak = Src::downgrade(&data);
 
assert_eq!(75, *data);
assert_eq!(75, *weak.upgrade().unwrap());
 
let mut data = Src::make_unique(data);
*data += 1;
 
assert_eq!(76, *data);
assert!(weak.upgrade().is_none());
 
let data = UniqueSrc::into_shared(data);
 
assert_eq!(76, *data);
assert_eq!(76, *weak.upgrade().unwrap());

However, if there are other Src pointers to this allocation, any WeakSrc pointers will remain with the old allocation.

let data = Src::single(75);
let other_data = Src::clone(&data);
let weak = Src::downgrade(&data);
 
assert_eq!(75, *data);
assert_eq!(75, *other_data);
assert_eq!(75, *weak.upgrade().unwrap());
 
let mut data = Src::make_unique(data);
*data += 1;
 
assert_eq!(76, *data);
assert_eq!(75, *other_data);
assert_eq!(75, *weak.upgrade().unwrap());
 
let data = UniqueSrc::into_shared(data);
 
assert_eq!(76, *data);
assert_eq!(75, *other_data);
assert_eq!(75, *weak.upgrade().unwrap());

See also Src::into_unique.

Note that this method (and Src::into_unique) can currently be used to construct non-root UniqueSrcs; this behavior has not been thouroghly considered and may be changed or removed in the future. As it is, the UniqueSrc will retain unique ownership of the whole allocation, even the parts it doesn’t contain. This means that a WeakSrc to any part of the allocation will not be able to upgrade, even if that WeakSrc does not overlap with the UniqueSrc.

Source

pub fn root(this: &Src<T>) -> Src<[T::Item]>

Returns an Src pointer that refers to this Src’s root (i.e., the entire allocation).

use slice_rc::Src;
 
let root = Src::from_array([1, 2, 3]);
let slice = root.slice(1..);
drop(root);
 
assert_eq!(*slice, [2, 3]);
 
let new_root = Src::root(&slice);
 
assert_eq!(*new_root, [1, 2, 3]);

This method returns an Src<[T::Item]> rather than an Src<T> for two reasons:

  • If T: Sized, then the root can only be a Src<T> if its total length is is 1, which would prevent situations like this:
let root: Src<[i32]> = Src::from_array([1, 2, 3]);
let slice: Src<i32> = root.slice(1);
let new_root: Src<[i32]> = Src::root(&slice);
 
assert_eq!(*new_root, [1, 2, 3]);
  • If T = str, it could be a UTF-8 slice of a larger allocation that is not entirely UTF-8, which would violate the safety invariant of str:
let root: Src<[u8]> = Src::copied(b"\xFFhello");
let s: Src<str> = Src::from_utf8(root.slice(1..)).unwrap();
let new_root: Src<[u8]> = Src::root(&s);
 
assert_eq!(&*s, "hello");
assert!(Src::from_utf8(new_root).is_err());
Source§

impl<T: SrcSlice + ?Sized> Src<T>

Source

pub fn empty() -> Src<T>

Constructs a new root Src of length 0. Note that, because Srcs are not growable like Vecs are, this allocation will never become larger. Every reference to this allocation is a root.

use slice_rc::Src;
 
let s = Src::<[i32]>::empty();
 
assert_eq!(s.len(), 0);
Source

pub fn len(&self) -> usize

Returns the number of elements in this Src. This method deliberately shadows <[T]>::len and str::len because this method provides a (slightly) simpler and more efficient implementation.

This method only returns the length of the whole allocation if self is a root Src.

use slice_rc::Src;
 
let s = Src::from_array([1, 2, 3]);
assert_eq!(s.len(), 3);
Source

pub fn is_empty(&self) -> bool

Returns true if this Src has a length of 0. This method deliberately shadows <[T]>::is_empty and str::is_empty because this method provides a (slightly) simpler and more efficient implementation.

Note that this method does not imply that this Src was constructed via Src::empty. Similarly, it does not imply that the entire allocation is empty, unless self is a root Src.

use slice_rc::Src;
 
let a = Src::from_array([1, 2, 3]);
assert!(!a.is_empty());
 
let b = Src::<[i32]>::from_array([]);
assert!(b.is_empty());
Source

pub fn slice<I: SrcIndex<T>>(&self, index: I) -> Src<I::Output>

Returns an Src pointer to an element or subslice depending on the type of index.

  • If given a position (only applicable where Self = Src<[U]>), returns an Src<U> to the element at that position.
  • If given a range, returns the subslice corresponding to that range.
§Panics

If the index is in some way out of bounds, or if Self = Src<str> and the indices are not at char boundaries.

§Examples
use slice_rc::Src;
 
let v = Src::from_array([10, 40, 30]);
assert_eq!(Src::single(40), v.slice(1));
assert_eq!(Src::from_array([10, 40]), v.slice(0..2));

Panics:

let v = Src::from_array([10, 40, 30]);
let _ = v.slice(3);
let _ = v.slice(0..4);
Source§

impl<T: Sized> Src<T>

Source

pub fn single(value: T) -> Src<T>

Constructs a new root Src that contains only the given value.

use slice_rc::Src;
 
let s = Src::single(42);
assert_eq!(*s, 42);
assert_eq!(Src::root(&s).len(), 1);
Source

pub fn single_cyclic<F: FnOnce(&WeakSrc<T>) -> T>(f: F) -> Src<T>

Constructs a new root Src that contains only the value returned from the given function f. The WeakSrc that f is given will be a weak reference to this allocation, which allows constructing a self-referential value; it will return None from WeakSrc::upgrade until after single_cyclic has returned.

This is a convienience method for a specific subset of behavior that can be obtained via UninitSrc.

use slice_rc::{Src, WeakSrc};
 
struct S {
  me: WeakSrc<S>,
}
 
let s = Src::single_cyclic(|me| S { me: me.clone() });
 
assert!(Src::ptr_eq(&s, &s.me.upgrade().unwrap()));
Source

pub fn single_uninit() -> Src<MaybeUninit<T>>

Constructs a new root Src of length 1 with uninitialized contents.

use slice_rc::{Src, UniqueSrc};
 
let five = Src::<i32>::single_uninit();
 
let mut five = Src::into_unique(five).unwrap();
five.write(5);
let five = UniqueSrc::into_shared(five);
 
let five = unsafe { five.assume_init() };
 
assert_eq!(*five, 5);
Source

pub fn single_zeroed() -> Src<MaybeUninit<T>>

Constructs a new root Src of length 1 with uninitialized contents, with the memory being filled with 0 bytes.

See MaybeUninit::zeroed for examples of correct and incorrect usage of this method.

use slice_rc::Src;
 
let zero = Src::<i32>::single_zeroed();
let zero = unsafe { zero.assume_init() };
 
assert_eq!(*zero, 0);
Source

pub fn as_slice(this: &Src<T>) -> Src<[T]>

Returns an Src equivalent to this one, but typed as a slice rather than a single element. The returned slice will have a length of 1, and its element 0 will be at the same location in memory as self’s value.

use slice_rc::Src;
use std::ptr;
 
let single = Src::single(42);
let slice = Src::as_slice(&single);
 
assert!(Src::ptr_eq(&single, &slice));
assert!(ptr::eq(&*single, &slice[0]));
Source§

impl<T> Src<[T]>

Source

pub fn new_uninit(len: usize) -> Src<[MaybeUninit<T>]>

Constructs a new root Src of the given length with uninitialized contents.

use slice_rc::{Src, UniqueSrc};
 
let fives = Src::<[i32]>::new_uninit(3);
 
let mut fives = Src::into_unique(fives).unwrap();
fives[0].write(5);
fives[1].write(5);
fives[2].write(5);
let fives = UniqueSrc::into_shared(fives);
 
let fives = unsafe { fives.assume_init() };
 
assert_eq!(*fives, [5, 5, 5]);
Source

pub fn new_zeroed(len: usize) -> Src<[MaybeUninit<T>]>

Constructs a new root Src of the given length with uninitialized contents, with the memory being filled with 0 bytes.

See MaybeUninit::zeroed for examples of correct and incorrect usage of this method.

use slice_rc::Src;
 
let zeroes = Src::<[i32]>::new_zeroed(3);
let zeroes = unsafe { zeroes.assume_init() };
 
assert_eq!(*zeroes, [0, 0, 0]);
Source

pub fn from_fn<F: FnMut(usize) -> T>(len: usize, f: F) -> Src<[T]>

Constructs a new root Src of the given length where each element is produced by calling f with that element’s index while walking forward through the slice.

This essentially the same as writing

Src::from_array([f(0), f(1), f(2), ..., f(len - 2), f(len - 1)])

and is similar to (0..len).map(f), just for Srcs rather than iterators.

If len == 0, this produces an empty Src without ever calling f.

use slice_rc::Src;
 
let slice = Src::from_fn(5, |i| i);
assert_eq!(*slice, [0, 1, 2, 3, 4]);
 
let slice2 = Src::from_fn(8, |i| i * 2);
assert_eq!(*slice2, [0, 2, 4, 6, 8, 10, 12, 14]);
 
let bool_slice = Src::from_fn(5, |i| i % 2 == 0);
assert_eq!(*bool_slice, [true, false, true, false, true]);

You can also capture things, so you can use closures with mutable state. The slice is generated in ascending index order, starting from the front and going towards the back.

let mut state = 1;
let s = Src::from_fn(6, |_| { let x = state; state *= 2; x });
assert_eq!(*s, [1, 2, 4, 8, 16, 32]);
§Panics

Panics if f panics; in this event, any elements that have been initialized will be properly dropped.

thread_local! {
  static DROPPED: Cell<usize> = Cell::new(0);
}
 
struct Droppable;
 
impl Drop for Droppable {
  fn drop(&mut self) {
    DROPPED.with(|dropped| dropped.update(|x| x + 1));
  }
}
 
let _ = std::panic::catch_unwind(|| {
  Src::from_fn(10, |i| {
    if i >= 5 { panic!() }
    Droppable
  })
});
 
assert_eq!(DROPPED.get(), 5);
Source

pub fn cyclic_from_fn<F: FnMut(&WeakSrc<[T]>, usize) -> T>( len: usize, f: F, ) -> Src<[T]>

Constructs a new root Src of the given length where each element is produced by calling f with a root WeakSrc pointer to the new allocation and that element’s index while walking forward through the slice.

This method is like Src::from_fn, but in this the function f is passed a root WeakSrc pointer to the allocation to allow constructing self-referential elements.

This is a convienience method for a specific subset of behavior that can be obtained via UninitSrc.

use slice_rc::{Src, WeakSrc};
 
struct S {
  val: usize,
  root: WeakSrc<[S]>,
}
 
let root = Src::cyclic_from_fn(5, |root, i| S {
  val: i * 2,
  root: root.clone(),
});
 
assert_eq!(root.iter().map(|s| s.val).collect::<Vec<_>>(), vec![0, 2, 4, 6, 8]);
assert!(root.iter().all(|s| Src::ptr_eq(&root, &s.root.upgrade().unwrap())));

It is possible to obtain a WeakSrc to the individual element that is being initialized via WeakSrc::slice:

struct S {
  val: usize,
  me: WeakSrc<S>,
}
 
let root = Src::cyclic_from_fn(5, |root, i| S {
  val: i * 2,
  me: root.slice(i),
});
 
assert!(root.iter().enumerate().all(|(i, s)| Src::ptr_eq(&root.slice(i), &s.me.upgrade().unwrap())));
Source

pub fn from_iter<I: IntoIterator<Item = T, IntoIter: ExactSizeIterator>>( iter: I, ) -> Src<[T]>

Constructs a new root Src from the given iterator.

This method is essentially shorthand for

use slice_rc::Src;
 
let mut iter = iter.into_iter();
Src::from_fn(iter.len(), |_| iter.next().unwrap())

The iterator must be ExactSizeIterator because Srcs cannot be resized, so the number of elements must be known at allocation-time, i.e., before any of the elements are initialized. If you want to use a non-ExactSizeIterator, use iter.collect::<Vec<_>>().

Source

pub fn from_array<const N: usize>(values: [T; N]) -> Src<[T]>

Constructs a new root Src from the given array.

This method is effectively equivalent to passing an array to Src::from_iter, but it is more efficient. As such, it is effectively shorthand for Src::from_fn(N, |i| values[i]), but again, more efficient (though not by enough to make Src::from_array(array::from_fn::<_, N, _>(f)) any better than Src::from_fn(N, f)).

Note that the my assertions about efficiency are not based any kind of benchmarking, just the fact that this method uses a single ptr::write where Src::from_fn and Src::from_iter use N arbitrary function calls and N ptr::writes. As array::from_fn re-introduces at least the N arbitrary function calls, its difference (again, without benchmarking) is negligible.

Source

pub fn from_default(len: usize) -> Src<[T]>
where T: Default,

Constructs a new root Src of the given length where each element is the type’s default.

This method is essentially equivalent to Src::from_fn(len, |_| Default::default()).

Source

pub fn filled(len: usize, value: &T) -> Src<[T]>
where T: Clone,

Constructs a new root Src of the given length where each element is a clone of value.

This method is essentially equivalent to Src::from_fn(len, |_| value.clone()).

Source

pub fn filled_cyclic<F: FnOnce(&WeakSrc<[T]>) -> T>( len: usize, f: F, ) -> Src<[T]>
where T: Clone,

Constructs a new root Src of the given length where each element is a clone of the value returned from f. f is passed a root WeakSrc pointer to the allocation; this can be used to make self-referential structures.

use slice_rc::{Src, WeakSrc};
 
#[derive(Clone)]
struct S {
  val: i32,
  root: WeakSrc<[S]>,
}
 
let root = Src::filled_cyclic(5, |root| S { val: 42, root: root.clone() });
assert!(root.iter().all(|s| s.val == 42));
assert!(root.iter().all(|s| Src::ptr_eq(&root, &s.root.upgrade().unwrap())));
Source

pub fn cloned(values: &[T]) -> Src<[T]>
where T: Clone,

Constructs a new root Src as a clone of the given slice.

This method is essentially shorthand for Src::from_fn(values.len(), |i| values[i].clone()), but without the implicit bounds checking for slice indexing.

Source

pub fn copied(values: &[T]) -> Src<[T]>
where T: Copy,

Constructs a new root Src as a copy of the given slice.

This method is functionally shorthand for Src::from_fn(values.len(), |i| values[i]), but without the implicit bounds checking for slice indexing.

This method is fairly efficient, as it is basically just an allocation (requisite for any Src constructor) and a memcpy.

Source§

impl<T> Src<MaybeUninit<T>>

Source

pub unsafe fn assume_init(self) -> Src<T>

Converts to Src<T>.

§Safety

As with MaybeUninit::assume_init, it is up to the caller to guarantee that the inner value really is in an initialized state. Calling this when the content is not yet fully initialized causes immediate undefined behavior.

§Examples
use slice_rc::Src;
 
let zero = Src::<i32>::single_zeroed();
let zero = unsafe { zero.assume_init() };
 
assert_eq!(*zero, 0);
Source§

impl<T> Src<[MaybeUninit<T>]>

Source

pub unsafe fn assume_init(self) -> Src<[T]>

Converts to Src<[T]>.

§Safety

As with MaybeUninit::assume_init, it is up to the caller to guarantee that the inner value really is in an initialized state. Calling this when the content is not yet fully initialized causes immediate undefined behavior.

§Examples
use slice_rc::Src;
 
let zeroes = Src::<[i32]>::new_zeroed(3);
let zeroes = unsafe { zeroes.assume_init() };
 
assert_eq!(*zeroes, [0, 0, 0]);
Source§

impl Src<str>

Source

pub fn new(s: impl AsRef<str>) -> Src<str>

Constructs a new root Src as a copy of the given string.

use slice_rc::Src;
 
let hello = Src::new("Hello World!");
 
assert_eq!(&*hello, "Hello World!");
Source

pub fn from_utf8(v: Src<[u8]>) -> Result<Src<str>, Utf8Error>

Converts an Src of bytes to a string Src.

str and [u8] are both slices of bytes, so this function converts between the two. Not all byte slices are valid string slices, however: str must be valid UTF-8. This method checks to ensure that the bytes are valid UTF-8, and then does the conversion.

If you are sure that the byte slice is valid UTF-8, and you don’t want to incur the overhead of the validity check, there is an unsafe version of this method, from_utf8_unchecked, which has the same behavior but skips the check.

§Errors

Returns Err if the slice is not UTF-8 with a description as to why the provided slice is not UTF-8.

§Examples

Basic usage:

use slice_rc::Src;
 
let sparkle_heart = Src::from_array([240, 159, 146, 150]);
 
let sparkle_heart = Src::from_utf8(sparkle_heart)?;
 
assert_eq!("💖", &*sparkle_heart);

Incorrect bytes:

let sparkle_heart = Src::from_array([0, 159, 146, 150]);
 
assert!(Src::from_utf8(sparkle_heart).is_err());
Source

pub unsafe fn from_utf8_unchecked(v: Src<[u8]>) -> Src<str>

Converts an Src of bytes to a string Src without checking that the string contains valid UTF-8.

See the safe version, from_utf8, for more information.

§Safety

The bytes passed in must be valid UTF-8.

§Examples
use slice_rc::Src;
 
let sparkle_heart = Src::from_array([240, 159, 146, 150]);
 
let sparkle_heart = unsafe { Src::from_utf8_unchecked(sparkle_heart) };
 
assert_eq!("💖", &*sparkle_heart);
Source

pub fn as_bytes(this: &Src<str>) -> Src<[u8]>

Converts a string Src to a Src of bytes. To convert the the bytes back to a string, use the from_utf8 method.

§Examples
use slice_rc::Src;
 
let bytes = Src::as_bytes(&Src::new("bors"));
assert_eq!(b"bors", &*bytes);

Trait Implementations§

Source§

impl<T: SrcTarget + ?Sized> AsRef<T> for Src<T>

Source§

fn as_ref(&self) -> &T

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

impl<T: SrcTarget + ?Sized> Borrow<T> for Src<T>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T: SrcTarget + ?Sized> Clone for Src<T>

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: Debug + SrcTarget + ?Sized> Debug for Src<T>

Source§

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

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

impl<T: Default> Default for Src<T>

Source§

fn default() -> Self

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

impl<T: SrcTarget + ?Sized> Deref for Src<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T: SrcTarget + ?Sized> Drop for Src<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Hash + SrcTarget + ?Sized> Hash for Src<T>

Source§

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

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: SrcTarget + Index<I> + ?Sized, I> Index<I> for Src<T>

Source§

type Output = <T as Index<I>>::Output

The returned type after indexing.
Source§

fn index(&self, index: I) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T: Ord + SrcTarget + ?Sized> Ord for Src<T>

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: PartialEq<U> + SrcTarget + ?Sized, U: SrcTarget + ?Sized> PartialEq<Src<U>> for Src<T>

Source§

fn eq(&self, other: &Src<U>) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &Src<U>) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd<U> + SrcTarget + ?Sized, U: SrcTarget + ?Sized> PartialOrd<Src<U>> for Src<T>

Source§

fn ge(&self, other: &Src<U>) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

fn gt(&self, other: &Src<U>) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
Source§

fn le(&self, other: &Src<U>) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Source§

fn lt(&self, other: &Src<U>) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
Source§

fn partial_cmp(&self, other: &Src<U>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
Source§

impl<T: SrcTarget + ?Sized> Pointer for Src<T>

Source§

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

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

impl<T: Eq + SrcTarget + ?Sized> Eq for Src<T>

Auto Trait Implementations§

§

impl<T> Freeze for Src<T>
where <T as SealedSrcTarget>::Len: Freeze, T: ?Sized,

§

impl<T> !RefUnwindSafe for Src<T>

§

impl<T> !Send for Src<T>

§

impl<T> !Sync for Src<T>

§

impl<T> Unpin for Src<T>
where <T as SealedSrcTarget>::Len: Unpin, T: ?Sized,

§

impl<T> !UnwindSafe for Src<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> SrcTarget for T

Source§

type Item = T

The type of each element of a Src<Self>. Read more
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.