pub trait WidthSliceable {
type Output: Sized;
// Required method
fn slice_width<R>(&self, range: R) -> Option<Self::Output>
where R: RangeBounds<usize>;
}Expand description
Provides a function for slicing by grapheme width rather than bytes.
This is useful for ensuring that a text object fits in a given terminal width.
Required Associated Types§
Required Methods§
Sourcefn slice_width<R>(&self, range: R) -> Option<Self::Output>where
R: RangeBounds<usize>,
fn slice_width<R>(&self, range: R) -> Option<Self::Output>where
R: RangeBounds<usize>,
Slice an object by width rather than by bytes.
§Example
use stylish_stringlike::text::WidthSliceable;
let foo = String::from("foobar");
assert_eq!(Some(String::from("oob")), foo.slice_width(1..4));
let bar = String::from("🙈🙉🙊");
// Monkeys are two columns wide, so we get nothing back
assert_eq!(None, bar.slice_width(..1));
// We get one monkey for two columns
assert_eq!(Some(String::from("🙈")), bar.slice_width(..2));
// If we aren't column-aligned, we get nothing because no one monkey fits between 1 and 3
assert_eq!(None, bar.slice_width(1..3));Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.