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>
impl<T: SrcTarget + ?Sized> Src<T>
Sourcepub fn ptr_eq<U: SrcTarget<Item = T::Item> + ?Sized>(
this: &Src<T>,
other: &Src<U>,
) -> bool
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.
Sourcepub fn same_root<U: SrcTarget<Item = T::Item> + ?Sized>(
this: &Src<T>,
other: &Src<U>,
) -> bool
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.
Sourcepub fn is_root(this: &Src<T>) -> bool
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.
Sourcepub fn strong_count(this: &Src<T>) -> usize
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));Sourcepub fn weak_count(this: &Src<T>) -> usize
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));Sourcepub fn into_unique(this: Src<T>) -> Result<UniqueSrc<T>, Src<T>>
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.
Sourcepub fn make_unique(this: Src<T>) -> UniqueSrc<T>
pub fn make_unique(this: Src<T>) -> UniqueSrc<T>
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.
Sourcepub fn root(this: &Src<T>) -> Src<[T::Item]>
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 aSrc<T>if its total length is is1, 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 ofstr:
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>
impl<T: SrcSlice + ?Sized> Src<T>
Sourcepub fn len(&self) -> usize
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);Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn slice<I: SrcIndex<T>>(&self, index: I) -> Src<I::Output>
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 anSrc<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>
impl<T: Sized> Src<T>
Sourcepub fn single(value: T) -> Src<T>
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);Sourcepub fn single_cyclic<F: FnOnce(&WeakSrc<T>) -> T>(f: F) -> Src<T>
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()));Sourcepub fn single_uninit() -> Src<MaybeUninit<T>>
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);Sourcepub fn single_zeroed() -> Src<MaybeUninit<T>>
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);Sourcepub fn as_slice(this: &Src<T>) -> Src<[T]>
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]>
impl<T> Src<[T]>
Sourcepub fn new_uninit(len: usize) -> Src<[MaybeUninit<T>]>
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]);Sourcepub fn new_zeroed(len: usize) -> Src<[MaybeUninit<T>]>
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]);Sourcepub fn from_fn<F: FnMut(usize) -> T>(len: usize, f: F) -> Src<[T]>
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);Sourcepub fn cyclic_from_fn<F: FnMut(&WeakSrc<[T]>, usize) -> T>(
len: usize,
f: F,
) -> Src<[T]>
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())));Sourcepub fn from_iter<I: IntoIterator<Item = T, IntoIter: ExactSizeIterator>>(
iter: I,
) -> Src<[T]>
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<_>>().
Sourcepub fn from_array<const N: usize>(values: [T; N]) -> Src<[T]>
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.
Sourcepub fn from_default(len: usize) -> Src<[T]>where
T: Default,
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()).
Sourcepub fn filled_cyclic<F: FnOnce(&WeakSrc<[T]>) -> T>(
len: usize,
f: F,
) -> Src<[T]>where
T: Clone,
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())));Sourcepub fn copied(values: &[T]) -> Src<[T]>where
T: Copy,
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>>
impl<T> Src<MaybeUninit<T>>
Sourcepub unsafe fn assume_init(self) -> Src<T>
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>]>
impl<T> Src<[MaybeUninit<T>]>
Sourcepub unsafe fn assume_init(self) -> Src<[T]>
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>
impl Src<str>
Sourcepub fn new(s: impl AsRef<str>) -> Src<str>
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!");Sourcepub fn from_utf8(v: Src<[u8]>) -> Result<Src<str>, Utf8Error>
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());Sourcepub unsafe fn from_utf8_unchecked(v: Src<[u8]>) -> Src<str>
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);