Struct core_extensions::slices::SliceBias

source ·
pub struct SliceBias {
    pub start: BiasDirection,
    pub end: BiasDirection,
}
Expand description

What directions SliceExt::{slice_lossy, slice_lossy_mut} are biased towards.

For str this has the effect of going in those directions if the start and/or end bound is between char boundaries.

For [T] this has no effect and it is recommended to use () as a parameter instead (() is converted to SliceBias::OUT).

§Example

§String

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

let word = "niño";

assert_eq!(
    word.char_indices().collect::<Vec<_>>(),
    &[(0, 'n'), (1, 'i'), (2, 'ñ'), (4, 'o')]
);

assert_eq!(word.slice_lossy(0..1000, ()), word);
assert_eq!(word.slice_lossy(10..10000, ()), "");
assert_eq!(word.slice_lossy(0..4, ()), "niñ");
assert_eq!(word.slice_lossy(0..3, ()), "niñ");
assert_eq!(word.slice_lossy(0..2, ()), "ni");
assert_eq!(word.slice_lossy(3..3, ()), "ñ");
assert_eq!(word.slice_lossy(3..4, ()), "ñ");
assert_eq!(word.slice_lossy(2..3, ()), "ñ");

assert_eq!(word.slice_lossy(0..1000, SliceBias::OUT), word);
assert_eq!(word.slice_lossy(10..10000, SliceBias::OUT), "");
assert_eq!(word.slice_lossy(0..4, SliceBias::OUT), "niñ");
assert_eq!(word.slice_lossy(0..3, SliceBias::OUT), "niñ");
assert_eq!(word.slice_lossy(0..2, SliceBias::OUT), "ni");
assert_eq!(word.slice_lossy(3..3, SliceBias::OUT), "ñ");
assert_eq!(word.slice_lossy(3..4, SliceBias::OUT), "ñ");
assert_eq!(word.slice_lossy(2..3, SliceBias::OUT), "ñ");

assert_eq!(word.slice_lossy(0..10000, SliceBias::IN), word);
assert_eq!(word.slice_lossy(10..10000, SliceBias::IN), "");
assert_eq!(word.slice_lossy(0..4, SliceBias::IN), "niñ");
assert_eq!(word.slice_lossy(0..3, SliceBias::IN), "ni");
assert_eq!(word.slice_lossy(0..2, SliceBias::IN), "ni");
assert_eq!(word.slice_lossy(3..3, SliceBias::IN), "");
assert_eq!(word.slice_lossy(3..4, SliceBias::IN), "");
assert_eq!(word.slice_lossy(2..3, SliceBias::IN), "");

assert_eq!(word.slice_lossy(0..1000, SliceBias::LEFT), word);
assert_eq!(word.slice_lossy(10..10000, SliceBias::LEFT), "");
assert_eq!(word.slice_lossy(0..4, SliceBias::LEFT), "niñ");
assert_eq!(word.slice_lossy(0..3, SliceBias::LEFT), "ni");
assert_eq!(word.slice_lossy(0..2, SliceBias::LEFT), "ni");
assert_eq!(word.slice_lossy(3..3, SliceBias::LEFT), "");
assert_eq!(word.slice_lossy(3..4, SliceBias::LEFT), "ñ");
assert_eq!(word.slice_lossy(2..3, SliceBias::LEFT), "");

assert_eq!(word.slice_lossy(0..1000, SliceBias::RIGHT), word);
assert_eq!(word.slice_lossy(10..10000, SliceBias::RIGHT), "");
assert_eq!(word.slice_lossy(0..4, SliceBias::RIGHT), "niñ");
assert_eq!(word.slice_lossy(0..3, SliceBias::RIGHT), "niñ");
assert_eq!(word.slice_lossy(0..2, SliceBias::RIGHT), "ni");
assert_eq!(word.slice_lossy(3..3, SliceBias::RIGHT), "");
assert_eq!(word.slice_lossy(3..4, SliceBias::RIGHT), "");
assert_eq!(word.slice_lossy(2..3, SliceBias::RIGHT), "ñ");

Fields§

§start: BiasDirection

bias of the start bound

§end: BiasDirection

bias of the end bound

Implementations§

source§

impl SliceBias

source

pub const IN: Self = _

Biased inwards, start bounds go right, end bounds go left.

source

pub const OUT: Self = _

Biased outwards, start bounds go left, end bounds go right.

source

pub const LEFT: Self = _

Biased leftwards, both bounds go left.

source

pub const RIGHT: Self = _

Biased rightwards. both bounds go right.

Trait Implementations§

source§

impl Clone for SliceBias

source§

fn clone(&self) -> SliceBias

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for SliceBias

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<()> for SliceBias

Returns a SliceBias::OUT

source§

fn from(_: ()) -> Self

Converts to this type from the input type.
source§

impl From<(BiasDirection,)> for SliceBias

source§

fn from((dir): (BiasDirection,)) -> Self

Converts to this type from the input type.
source§

impl From<(BiasDirection, BiasDirection)> for SliceBias

source§

fn from((start, end): (BiasDirection, BiasDirection)) -> Self

Converts to this type from the input type.
source§

impl From<BiasDirection> for SliceBias

source§

fn from(dir: BiasDirection) -> Self

Converts to this type from the input type.
source§

impl PartialEq for SliceBias

source§

fn eq(&self, other: &SliceBias) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for SliceBias

source§

impl Eq for SliceBias

source§

impl StructuralPartialEq for SliceBias

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> SelfOps for T
where T: ?Sized,

source§

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

Compares the address of self with the address of other. Read more
source§

fn piped<F, U>(self, f: F) -> U
where F: FnOnce(Self) -> U, Self: Sized,

Emulates the pipeline operator, allowing method syntax in more places. Read more
source§

fn piped_ref<'a, F, U>(&'a self, f: F) -> U
where F: FnOnce(&'a Self) -> U,

The same as piped except that the function takes &Self Useful for functions that take &Self instead of Self. Read more
source§

fn piped_mut<'a, F, U>(&'a mut self, f: F) -> U
where F: FnOnce(&'a mut Self) -> U,

The same as piped, except that the function takes &mut Self. Useful for functions that take &mut Self instead of Self.
source§

fn mutated<F>(self, f: F) -> Self
where F: FnOnce(&mut Self), Self: Sized,

Mutates self using a closure taking self by mutable reference, passing it along the method chain. Read more
source§

fn observe<F>(self, f: F) -> Self
where F: FnOnce(&Self), Self: Sized,

Observes the value of self, passing it along unmodified. Useful in long method chains. Read more
source§

fn into_<T>(self) -> T
where Self: Into<T>,

Performs a conversion with Into. using the turbofish .into_::<_>() syntax. Read more
source§

fn as_ref_<T: ?Sized>(&self) -> &T
where Self: AsRef<T>,

Performs a reference to reference conversion with AsRef, using the turbofish .as_ref_::<_>() syntax. Read more
source§

fn as_mut_<T: ?Sized>(&mut self) -> &mut T
where Self: AsMut<T>,

Performs a mutable reference to mutable reference conversion with AsMut, using the turbofish .as_mut_::<_>() syntax. Read more
source§

fn drop_(self)
where Self: Sized,

Drops self using method notation. Alternative to std::mem::drop. Read more
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> TypeIdentity for T
where T: ?Sized,

source§

type Type = T

This is always Self.
source§

fn into_type(self) -> Self::Type
where Self: Sized, Self::Type: Sized,

Converts a value back to the original type.
source§

fn as_type(&self) -> &Self::Type

Converts a reference back to the original type.
source§

fn as_type_mut(&mut self) -> &mut Self::Type

Converts a mutable reference back to the original type.
source§

fn into_type_box(self: Box<Self>) -> Box<Self::Type>

Converts a box back to the original type.
source§

fn into_type_arc(this: Arc<Self>) -> Arc<Self::Type>

Converts an Arc back to the original type. Read more
source§

fn into_type_rc(this: Rc<Self>) -> Rc<Self::Type>

Converts an Rc back to the original type. Read more
source§

fn from_type(this: Self::Type) -> Self
where Self: Sized, Self::Type: Sized,

Converts a value back to the original type.
source§

fn from_type_ref(this: &Self::Type) -> &Self

Converts a reference back to the original type.
source§

fn from_type_mut(this: &mut Self::Type) -> &mut Self

Converts a mutable reference back to the original type.
source§

fn from_type_box(this: Box<Self::Type>) -> Box<Self>

Converts a box back to the original type.
source§

fn from_type_arc(this: Arc<Self::Type>) -> Arc<Self>

Converts an Arc back to the original type.
source§

fn from_type_rc(this: Rc<Self::Type>) -> Rc<Self>

Converts an Rc back to the original type.