Struct shared_slice::rc::RcSlice
[-]
[+]
[src]
pub struct RcSlice<T> { // some fields omitted }
A reference-counted slice type.
This is exactly like &[T]
except without lifetimes, so the
allocation only disappears once all RcSlice
s have disappeared.
NB. this can lead to applications effectively leaking memory if a
short subslice of a long RcSlice
is held.
Examples
use shared_slice::rc::RcSlice; let x = RcSlice::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::rc::RcSlice; let n = 5; let v: Vec<u8> = (0u8..n).collect(); // 0, ..., 4 let x = RcSlice::new(v.into_boxed_slice()); assert_eq!(&*x, [0, 1, 2, 3, 4]);
Methods
impl<T> RcSlice<T>
fn new(slice: Box<[T]>) -> RcSlice<T>
Construct a new RcSlice
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) -> RcSlice<T>
Construct a new RcSlice
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) -> RcSlice<T>
Construct a new RcSlice
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) -> RcSlice<T>
Construct a new RcSlice
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()
.