Skip to main content

Data

Struct Data 

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

A NaN-boxed word, plus the word an int needs.

§Layout:

  • first 13 bits are NaN
  • remaining 51 bits: 3 bits for type (4 for the MAP type) & 48 bits of actual payload (47 for the MAP type)

§Data type:

  • BOOL = 001
  • STRING_SMALL = 010
  • STRING_LARGE = 011
  • ARRAY = 100
  • NULL = 101
  • INT = 110
  • STRUCT = 111
  • MAP = 1111

§Functions

A function value is an ARRAY with the top payload bit set, which is what tells it from the list it is built as.

§Strings

Strings are inlined if they’re 6 bytes long or less. If they’re longer, they’re stored in string_pool.

Implementations§

Source§

impl Data

Source

pub const fn into_words(self) -> (u64, i64)

The pair of words a .cdlb records a value as. The artifact keeps the box and the integer rather than a type tag, so a value comes back without being inspected.

Source

pub unsafe fn load(src: *const Self) -> Self

Reads the value at src as two separate words.

Most instructions write a result a word at a time: the tag and the integer are stored apart. A copy that then read the value back as one 16-byte load would have to wait for both stores to reach the cache, because the processor forwards a pending store only to a load that fits inside it. Reading the words apart lets each one forward. The reads are volatile so the compiler cannot fuse them back into one.

§Safety

src must point to a live value.

Source

pub const fn is_false(self) -> bool

Whether this is false. A bool keeps its whole value in the box, so the box alone answers, and the integer word is never read.

Source

pub const fn is_true(self) -> bool

Whether this is true, read from the box alone.

Source

pub const fn from_words(boxed: u64, int: i64) -> Self

Rebuilds a value from the words Data::into_words handed out.

Source

pub const fn tag(self) -> u64

Source

pub const fn is_null(self) -> bool

Source

pub const fn bool(b: bool) -> Self

Source

pub fn as_bool(self) -> bool

Source

pub const fn is_bool(self) -> bool

Source

pub const fn float(n: f64) -> Self

Source

pub const fn as_float(self) -> f64

Source

pub const fn is_float(self) -> bool

Source

pub const fn int(n: i64) -> Self

Convert the given integer to a tagged integer. The box carries the tag and the second word carries all 64 bits.

Source

pub const fn as_int(self) -> i64

Source

pub const fn is_int(self) -> bool

Source

pub const fn array(id: u32) -> Self

Source

pub const fn as_array(self) -> usize

Source

pub const fn is_array(self) -> bool

Source

pub const fn function(id: u32) -> Self

A function value over the pool entry id: the array a call reads the body location and the captured cells out of, marked as the function it is.

Source

pub const fn is_function(self) -> bool

Whether this value is a function. A function value answers Self::is_array too, since everything that holds, walks and frees an array holds, walks and frees this one.

Source

pub fn small_str(s: &str) -> Self

This will create a new inlined string. In debug it’ll panic (just in case). The caller guarantees that s is never longer than 6 bytes.

Source

pub const fn large_str_id(id: u64) -> Self

Source

pub fn as_map_key(self, string_pool: &StringPool) -> Self

This value as a map stores it for a key. A string in the pool takes a hash of its text into its second word, which only an int otherwise uses, arranged so the value hashes as the text does rather than as the slot: two strings with the same text in different slots then land in the same bucket, where the map compares them by text. See crate::vm::KeyByText.

Source

pub fn p_str(s: &str, string_pool: &mut StringPool) -> Self

Same as str(), except this never runs the GC because this function is called by the compiler

Source

pub fn interned(s: &str, string_pool: &mut StringPool) -> Option<Self>

s as a value without allocating: inline if it is six bytes or shorter, else the pool slot already holding it, if one does.

Source

pub fn string<S: PoolString>( s: S, array_pool: &mut ObjectPool, map_pool: &mut MapPool, string_pool: &mut StringPool, registers: &RegisterFile, recursion_stack: &RegisterFile, gc: &mut GcState, ) -> Self

Allocates a string, storing it directly inside the value if it is six bytes or shorter and in the string pool otherwise. A pooled string can start a collection, so every value the program still holds has to be reachable from registers or recursion_stack.

Source

pub fn as_str(&self, string_pool: &StringPool) -> &str

Source

pub fn str_is_ascii(&self, string_pool: &StringPool) -> bool

True when every character of this string takes a single byte. A character position on such a string is a byte position, which is the route indexing and slicing take when they can.

Source

pub fn str_char_len(&self, string_pool: &StringPool) -> usize

How many characters this string has. Positions on a string count characters, so this is what len answers and what an index or a slice bound is checked against.

Source

pub fn str_byte_offset( &self, char_idx: usize, string_pool: &StringPool, ) -> usize

The byte offset character char_idx starts at. The string has at least char_idx characters; at exactly that many the answer is its byte length, which is what a slice bound on the end of the string asks for.

Source

pub const fn is_string(self) -> bool

Source

pub const fn inc_int(&mut self)

Increments the integer stored in this Data in-place. Wraps.

Source

pub const fn dec_int(&mut self)

Decrements the integer stored in this Data in-place. Wraps.

Source

pub const fn inc_into(&mut self, src: Self)

Writes src + 1 into self. Wraps.

Source

pub const fn dec_into(&mut self, src: Self)

Writes src - 1 into self. Wraps.

Source

pub const fn is_large_str(self) -> bool

Source

pub const fn get_str_pool_id(self) -> usize

Source

pub const fn struct_instance(type_id: u16, id: u32) -> Self

Source

pub const fn as_struct(self) -> usize

Source

pub const fn enum_instance(type_id: u16, id: u32) -> Self

Source

pub const fn as_enum(self) -> usize

Source

pub const fn enum_type_id(self) -> u16

Source

pub const fn is_enum(self) -> bool

Source

pub const fn struct_type_id(self) -> u16

Source

pub const fn is_struct(self) -> bool

Source

pub const fn map(id: u32) -> Self

Source

pub const fn as_map(self) -> usize

Source

pub const fn is_map(self) -> bool

Source

pub const fn type_name(self) -> &'static str

A short runtime type name, used in downcast error messages. Reads only the box tag, so it needs no pools.

Source

pub fn format( self, obj_pool: &ObjectPool, string_pool: &StringPool, map_pool: &MapPool, structs: &[Struct], enums: &[EnumType], show_str: bool, ) -> SmolStr

Trait Implementations§

Source§

impl Clone for Data

Source§

fn clone(&self) -> Data

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 Copy for Data

Source§

impl Debug for Data

Source§

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

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

impl Eq for Data

Source§

impl Equivalent<Data> for KeyByText<'_>

Source§

fn equivalent(&self, stored: &Data) -> bool

Compare self to key and return true if they are equal.
Source§

impl From<Data> for f64

Source§

fn from(value: Data) -> Self

Converts to this type from the input type.
Source§

impl From<Data> for i64

Source§

fn from(value: Data) -> Self

Converts to this type from the input type.
Source§

impl From<Data> for bool

Source§

fn from(value: Data) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Data

Source§

fn from(value: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Data

Source§

fn from(value: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Data

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl Hash for Data

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Both words in one, so an int hashes by its value and every other type hashes by its box exactly as it did when the box was the whole value, except a pooled string stored as a map key, which hashes by its text (see Data::as_map_key). DataHash keeps only the last word written, so writing them apart would collapse every non-int onto the same hash.

1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Data

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Data

Auto Trait Implementations§

§

impl Freeze for Data

§

impl RefUnwindSafe for Data

§

impl Send for Data

§

impl Sync for Data

§

impl Unpin for Data

§

impl UnsafeUnpin for Data

§

impl UnwindSafe for Data

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
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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.

§

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

§

fn fg(&self, value: Color) -> Painted<&T>

Returns a styled value derived from self with the foreground set to value.

This method should be used rarely. Instead, prefer to use color-specific builder methods like red() and green(), which have the same functionality but are pithier.

§Example

Set foreground color to white using fg():

use yansi::{Paint, Color};

painted.fg(Color::White);

Set foreground color to white using white().

use yansi::Paint;

painted.white();
§

fn primary(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Primary].

§Example
println!("{}", value.primary());
§

fn fixed(&self, color: u8) -> Painted<&T>

Returns self with the fg() set to [Color :: Fixed].

§Example
println!("{}", value.fixed(color));
§

fn rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the fg() set to [Color :: Rgb].

§Example
println!("{}", value.rgb(r, g, b));
§

fn black(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Black].

§Example
println!("{}", value.black());
§

fn red(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Red].

§Example
println!("{}", value.red());
§

fn green(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Green].

§Example
println!("{}", value.green());
§

fn yellow(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Yellow].

§Example
println!("{}", value.yellow());
§

fn blue(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Blue].

§Example
println!("{}", value.blue());
§

fn magenta(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Magenta].

§Example
println!("{}", value.magenta());
§

fn cyan(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Cyan].

§Example
println!("{}", value.cyan());
§

fn white(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: White].

§Example
println!("{}", value.white());
§

fn bright_black(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightBlack].

§Example
println!("{}", value.bright_black());
§

fn bright_red(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightRed].

§Example
println!("{}", value.bright_red());
§

fn bright_green(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightGreen].

§Example
println!("{}", value.bright_green());
§

fn bright_yellow(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightYellow].

§Example
println!("{}", value.bright_yellow());
§

fn bright_blue(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightBlue].

§Example
println!("{}", value.bright_blue());
§

fn bright_magenta(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightMagenta].

§Example
println!("{}", value.bright_magenta());
§

fn bright_cyan(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightCyan].

§Example
println!("{}", value.bright_cyan());
§

fn bright_white(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightWhite].

§Example
println!("{}", value.bright_white());
§

fn bg(&self, value: Color) -> Painted<&T>

Returns a styled value derived from self with the background set to value.

This method should be used rarely. Instead, prefer to use color-specific builder methods like on_red() and on_green(), which have the same functionality but are pithier.

§Example

Set background color to red using fg():

use yansi::{Paint, Color};

painted.bg(Color::Red);

Set background color to red using on_red().

use yansi::Paint;

painted.on_red();
§

fn on_primary(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Primary].

§Example
println!("{}", value.on_primary());
§

fn on_fixed(&self, color: u8) -> Painted<&T>

Returns self with the bg() set to [Color :: Fixed].

§Example
println!("{}", value.on_fixed(color));
§

fn on_rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the bg() set to [Color :: Rgb].

§Example
println!("{}", value.on_rgb(r, g, b));
§

fn on_black(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Black].

§Example
println!("{}", value.on_black());
§

fn on_red(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Red].

§Example
println!("{}", value.on_red());
§

fn on_green(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Green].

§Example
println!("{}", value.on_green());
§

fn on_yellow(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Yellow].

§Example
println!("{}", value.on_yellow());
§

fn on_blue(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Blue].

§Example
println!("{}", value.on_blue());
§

fn on_magenta(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Magenta].

§Example
println!("{}", value.on_magenta());
§

fn on_cyan(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Cyan].

§Example
println!("{}", value.on_cyan());
§

fn on_white(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: White].

§Example
println!("{}", value.on_white());
§

fn on_bright_black(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightBlack].

§Example
println!("{}", value.on_bright_black());
§

fn on_bright_red(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightRed].

§Example
println!("{}", value.on_bright_red());
§

fn on_bright_green(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightGreen].

§Example
println!("{}", value.on_bright_green());
§

fn on_bright_yellow(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightYellow].

§Example
println!("{}", value.on_bright_yellow());
§

fn on_bright_blue(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightBlue].

§Example
println!("{}", value.on_bright_blue());
§

fn on_bright_magenta(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightMagenta].

§Example
println!("{}", value.on_bright_magenta());
§

fn on_bright_cyan(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightCyan].

§Example
println!("{}", value.on_bright_cyan());
§

fn on_bright_white(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightWhite].

§Example
println!("{}", value.on_bright_white());
§

fn attr(&self, value: Attribute) -> Painted<&T>

Enables the styling [Attribute] value.

This method should be used rarely. Instead, prefer to use attribute-specific builder methods like bold() and underline(), which have the same functionality but are pithier.

§Example

Make text bold using attr():

use yansi::{Paint, Attribute};

painted.attr(Attribute::Bold);

Make text bold using using bold().

use yansi::Paint;

painted.bold();
§

fn bold(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Bold].

§Example
println!("{}", value.bold());
§

fn dim(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Dim].

§Example
println!("{}", value.dim());
§

fn italic(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Italic].

§Example
println!("{}", value.italic());
§

fn underline(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Underline].

§Example
println!("{}", value.underline());

Returns self with the attr() set to [Attribute :: Blink].

§Example
println!("{}", value.blink());

Returns self with the attr() set to [Attribute :: RapidBlink].

§Example
println!("{}", value.rapid_blink());
§

fn invert(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Invert].

§Example
println!("{}", value.invert());
§

fn conceal(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Conceal].

§Example
println!("{}", value.conceal());
§

fn strike(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Strike].

§Example
println!("{}", value.strike());
§

fn quirk(&self, value: Quirk) -> Painted<&T>

Enables the yansi [Quirk] value.

This method should be used rarely. Instead, prefer to use quirk-specific builder methods like mask() and wrap(), which have the same functionality but are pithier.

§Example

Enable wrapping using .quirk():

use yansi::{Paint, Quirk};

painted.quirk(Quirk::Wrap);

Enable wrapping using wrap().

use yansi::Paint;

painted.wrap();
§

fn mask(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Mask].

§Example
println!("{}", value.mask());
§

fn wrap(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Wrap].

§Example
println!("{}", value.wrap());
§

fn linger(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Linger].

§Example
println!("{}", value.linger());
§

fn clear(&self) -> Painted<&T>

👎Deprecated since 1.0.1:

renamed to resetting() due to conflicts with Vec::clear(). The clear() method will be removed in a future release.

Returns self with the quirk() set to [Quirk :: Clear].

§Example
println!("{}", value.clear());
§

fn resetting(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Resetting].

§Example
println!("{}", value.resetting());
§

fn bright(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Bright].

§Example
println!("{}", value.bright());
§

fn on_bright(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: OnBright].

§Example
println!("{}", value.on_bright());
§

fn whenever(&self, value: Condition) -> Painted<&T>

Conditionally enable styling based on whether the [Condition] value applies. Replaces any previous condition.

See the crate level docs for more details.

§Example

Enable styling painted only when both stdout and stderr are TTYs:

use yansi::{Paint, Condition};

painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);
§

fn new(self) -> Painted<Self>
where Self: Sized,

Create a new [Painted] with a default [Style]. Read more
§

fn paint<S>(&self, style: S) -> Painted<&Self>
where S: Into<Style>,

Apply a style wholesale to self. Any previous style is replaced. 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.