Struct shared_slice::arc::ArcSlice
[-]
[+]
[src]
pub struct ArcSlice<T> { // some fields omitted }
A reference-counted slice type.
This is exactly like &[T]
except without lifetimes, so the
allocation only disappears once all ArcSlice
s have disappeared.
NB. this can lead to applications effectively leaking memory if a
short subslice of a long ArcSlice
is held.
Examples
use shared_slice::arc::ArcSlice; let x = ArcSlice::new(Box::new(["foo", "bar", "baz"])); println!("{:?}", x); // ["foo", "bar", "baz"] println!("{:?}", x.slice(1, 3)); // ["bar", "baz"]
Constructing with a dynamic number of elements:
use shared_slice::arc::ArcSlice; let n = 5; let v: Vec<u8> = (0u8..n).collect(); // 0, ..., 4 let x = ArcSlice::new(v.into_boxed_slice()); assert_eq!(&*x, [0, 1, 2, 3, 4]);
Methods
impl<T> ArcSlice<T>
fn new(slice: Box<[T]>) -> ArcSlice<T>
Construct a new ArcSlice
containing the elements of slice
.
This reuses the allocation of slice
.
fn downgrade(&self) -> WeakSlice<T>
Downgrade self into a weak slice.
fn slice(self, lo: usize, hi: usize) -> ArcSlice<T>
Construct a new ArcSlice
that only points to elements at
indices lo
(inclusive) through hi
(exclusive).
This consumes self
to avoid unnecessary reference-count
modifications. Use .clone()
if it is necessary to refer to
self
after calling this.
Panics
Panics if lo > hi
or if either are strictly greater than
self.len()
.
fn slice_to(self, hi: usize) -> ArcSlice<T>
Construct a new ArcSlice
that only points to elements at
indices up to hi
(exclusive).
This consumes self
to avoid unnecessary reference-count
modifications. Use .clone()
if it is necessary to refer to
self
after calling this.
Panics
Panics if hi > self.len()
.
fn slice_from(self, lo: usize) -> ArcSlice<T>
Construct a new ArcSlice
that only points to elements at
indices starting at lo
(inclusive).
This consumes self
to avoid unnecessary reference-count
modifications. Use .clone()
if it is necessary to refer to
self
after calling this.
Panics
Panics if lo > self.len()
.