[][src]Trait core_extensions::SliceExt

pub trait SliceExt<T> {
    pub fn contains_slice(&self, other: &Self) -> bool;
pub fn is_slice(&self, other: &Self) -> bool;
pub fn offset_of_slice(&self, other: &Self) -> usize;
pub fn get_offset_of_slice(&self, other: &Self) -> Option<usize>;
pub fn index_of(&self, other: *const T) -> usize;
pub fn get_index_of(&self, other: *const T) -> Option<usize>;
pub fn slice_lossy<SB>(&self, range: Range<usize>, bias: SB) -> &Self
    where
        SB: Into<SliceBias>
;
pub fn slice_lossy_mut<SB>(
        &mut self,
        range: Range<usize>,
        bias: SB
    ) -> &mut Self
    where
        SB: Into<SliceBias>
; }

Extension trait for [T] and str.

Required methods

pub fn contains_slice(&self, other: &Self) -> bool[src]

Checks whether self fully contains another slice in memory.

If other is a zero-sized slice it is not contained inside self.

Example

use core_extensions::SliceExt;
let list=vec![0,1,2,3,4,5];
 
// Slices never contain empty slices
assert!(!list.contains_slice(&list[..0]));
assert!( list.contains_slice(&list[..1]));
assert!( list.contains_slice(&list[3..]));

pub fn is_slice(&self, other: &Self) -> bool[src]

Checks whether self is exactly the other slice in memory.

Example

use core_extensions::SliceExt;
let list = [0,1,2,3,4,5];
let slice_0=&list[..0];
let slice_1=&list[..];

assert!( slice_0.is_slice(slice_0));
assert!( slice_0.is_slice(&list[..0]));
assert!(!slice_0.is_slice(slice_1));
assert!(!slice_0.is_slice(&[]));

pub fn offset_of_slice(&self, other: &Self) -> usize[src]

Returns the index at which other starts.

If other is not inside self returns self.len()

Example

use core_extensions::SliceExt;
let list=vec![0,1,2,3,4,5];
let slice_0=&list[..0];
let slice_1=&list[3..];

assert_eq!(list.offset_of_slice(slice_0),0);
assert_eq!(list.offset_of_slice(slice_1),3);
assert_eq!(list.offset_of_slice(&[]),list.len());

pub fn get_offset_of_slice(&self, other: &Self) -> Option<usize>[src]

Returns the index at which other starts.

If other is a zero-sized slice, this returns None.

Example

use core_extensions::SliceExt;
let list = [0,1,2,3,4,5];

assert_eq!(list.get_offset_of_slice(&list[..0]), None);
assert_eq!(list.get_offset_of_slice(&list[1..]), Some(1));
assert_eq!(list.get_offset_of_slice(&list[3..]), Some(3));

pub fn index_of(&self, other: *const T) -> usize[src]

Returns the index of other if it's stored in the slice (if it points within the slice).

If other is not inside self returns self.len()

Example

use core_extensions::SliceExt;
let list=vec![0,1,2,3,4,5];
let elem_0=&list[0];
let elem_3=&list[3];
let outside=&0;

assert_eq!(list.index_of(elem_0),0);
assert_eq!(list.index_of(elem_3),3);
assert_eq!(list.index_of(outside),list.len());

pub fn get_index_of(&self, other: *const T) -> Option<usize>[src]

Returns the index of other if it's stored in the slice (if it points within the slice).

Example

use core_extensions::SliceExt;
let list=vec![0,1,2,3,4,5];
let elem_0=&list[0];
let elem_3=&list[3];
let outside=&0;

assert_eq!(list.get_index_of(elem_0),Some(0));
assert_eq!(list.get_index_of(elem_3),Some(3));
assert_eq!(list.get_index_of(outside),None);

pub fn slice_lossy<SB>(&self, range: Range<usize>, bias: SB) -> &Self where
    SB: Into<SliceBias>, 
[src]

Used for non-panicking slicing.

For [T] it simply saturates ranges at self.len(). It is recommended to pass () as the bias parameter as it has no effect.

For str,it grows the range in the directions determined by bias.

For more examples on str look at SliceBias.

Example ,[u8]

use core_extensions::SliceExt;

let arr=[1,2,3,4,5,6];
assert_eq!(arr.slice_lossy(0   ..3   ,()),&arr[..3]);
assert_eq!(arr.slice_lossy(3   ..1000,()),&arr[3..]);
assert_eq!(arr.slice_lossy(1000..1000,()),&[]);

Example ,str

use core_extensions::SliceExt;
use core_extensions::slices::SliceBias;

let word="niño"; // 'ñ' is 2 bytes long , spanning the range 2..4

assert_eq!(word.slice_lossy(0..3,SliceBias::LEFT ),"ni");
assert_eq!(word.slice_lossy(0..3,SliceBias::RIGHT),"niñ");
assert_eq!(word.slice_lossy(0..3,SliceBias::IN ),"ni");
assert_eq!(word.slice_lossy(0..3,SliceBias::OUT),"niñ");

assert_eq!(word.slice_lossy(3..10000,()            ),"ño");
assert_eq!(word.slice_lossy(3..10000,SliceBias::OUT),"ño");

assert_eq!(word.slice_lossy(1000..1000,()            ),"");
assert_eq!(word.slice_lossy(1000..1000,SliceBias::OUT),"");

pub fn slice_lossy_mut<SB>(
    &mut self,
    range: Range<usize>,
    bias: SB
) -> &mut Self where
    SB: Into<SliceBias>, 
[src]

Used for non-panicking mutable slicing.

Identical behavior to slice_lossy with respect to ranges.

Loading content...

Implementations on Foreign Types

impl SliceExt<u8> for str[src]

impl<T> SliceExt<T> for [T][src]

Loading content...

Implementors

Loading content...