[][src]Trait core_extensions::SliceExt

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

fn contains_slice(&self, other: &Self) -> bool

Checks whether self fully contains another slice in memory.

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!( list.contains_slice(slice_0));
assert!( list.contains_slice(slice_1));
assert!(!list.contains_slice(&[]));

fn is_slice(&self, other: &Self) -> bool

Checks whether self is exactly the other slice in memory.

Example

use core_extensions::SliceExt;
let list=vec![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(&[]));

fn offset_of_slice(&self, other: &Self) -> usize

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());

fn get_offset_of_slice(&self, other: &Self) -> Option<usize>

Returns the index at which other starts.

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.get_offset_of_slice(slice_0),Some(0));
assert_eq!(list.get_offset_of_slice(slice_1),Some(3));
assert_eq!(list.get_offset_of_slice(&[])    ,None);

fn index_of(&self, other: *const T) -> usize

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());

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

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);

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

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),"");

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

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...