Skip to main content

StyleTable

Struct StyleTable 

Source
pub struct StyleTable { /* private fields */ }
Expand description

Styles, stored once each. Runs refer to them by StyleId.

Interning hashes a style’s public parts (colours, attributes and link) with a fast non-cryptographic hash and confirms a match with ==, so metadata still tells styles apart. A style made of two plain colours can be interned by the colours alone (intern_rgb), without building a Style unless it is new.

The table only grows while it is used; a table kept between frames (a Screen’s) drops what its cells no longer use with retain.

Implementations§

Source§

impl StyleTable

Source

pub fn new() -> Self

Source

pub fn intern(&mut self, style: Option<&Style>) -> StyleId

The id of style, adding it if it is new. None is StyleId::NONE.

Source

pub fn intern_rgb(&mut self, fg: Option<Rgb>, bg: Option<Rgb>) -> StyleId

The id of a style of foreground fg and background bg in true colour (either missing), as Style::from_color with Color::from_rgb would make it; neither is StyleId::NONE. A pixel’s cell: the style is built only the first time the pair is seen.

use rich::{Color, Style};
use rich_ext::frame::StyleTable;

let mut table = StyleTable::new();
let id = table.intern_rgb(Some([255, 0, 0]), None);
let red = Style::from_color(Some(Color::from_rgb(255, 0, 0)), None);
assert_eq!(table.intern(Some(&red)), id);
assert_eq!(table.rgb(id), Some((Some([255, 0, 0]), None)));
Source

pub fn rgb(&self, id: StyleId) -> Option<(Option<Rgb>, Option<Rgb>)>

The colours of a style interned with intern_rgb (or found by it): what a painter can write without asking the style.

Source

pub fn get(&self, id: StyleId) -> Option<&Style>

The style behind id; None for StyleId::NONE.

Source

pub fn len(&self) -> usize

The number of entries, counting StyleId::NONE.

Source

pub fn is_empty(&self) -> bool

Always false: the table always holds StyleId::NONE.

Source

pub fn piled_up(&self) -> bool

Whether the table has at least doubled since the last retain and holds more than a few hundred styles: when dropping the unused ones is worth a pass over the cells.

Source

pub fn retain(&mut self, keep: &[bool]) -> StyleRemap

Keep only the styles keep marks (by index; StyleId::NONE is always kept, and ids past keep’s end are dropped), renumbered in their order. The result maps the old ids to the new: everything holding an old id must be moved over with it.

use rich::Style;
use rich_ext::frame::{StyleId, StyleTable};

let mut table = StyleTable::new();
let bold = table.intern(Some(&Style::parse("bold").unwrap()));
let red = table.intern(Some(&Style::parse("red").unwrap()));
let mut keep = vec![false; table.len()];
keep[red.index()] = true;
let remap = table.retain(&keep);
assert_eq!(table.len(), 2);
assert_eq!(remap.get(bold), None);
let red = remap.get(red).unwrap();
assert_eq!(table.get(red), Some(&Style::parse("red").unwrap()));
assert_eq!(remap.get(StyleId::NONE), Some(StyleId::NONE));

Trait Implementations§

Source§

impl Clone for StyleTable

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for StyleTable

Source§

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

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

impl Default for StyleTable

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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, dest: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more