Skip to main content

Generics

Struct Generics 

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

The generic declarations of a program, the instantiations made from them, and the type parameters bound while a body is being compiled.

Implementations§

Source§

impl Generics

Source

pub const fn template_count(&self) -> usize

How many generic declarations are registered.

Source

pub fn mark_prelude_file(&mut self, file_idx: u16)

Records that file file_idx is a prelude module.

Source

pub const fn set_prelude_templates(&mut self, range: Range<usize>)

Records which templates the prelude registered.

Source

pub fn bound(&self, name: &str) -> Option<DataType>

The type this name is currently bound to, if it names a type parameter of the body being compiled. A parameter nothing named is any.

Source

pub fn names(&self, name: &str) -> bool

Whether the call site or an enclosing impl block fixed this type parameter to a type, as opposed to leaving it standing for any. An annotation that mentions a parameter nothing named stays un-pinned.

Source

pub fn params_of(&self, name: &str) -> Option<&[SmolStr]>

The parameters of the generic declaration name names, for a caller with no scope to resolve it in: a mangled method name (Cell<int>#get) carries the type name and no module path. Takes the declaration registered last when two modules declare the name.

Source

pub fn set_struct_defaults( &mut self, id: u16, file_idx: u16, values: Box<[Option<(Expr, Span)>]>, )

Records the values the fields of struct id were declared with, replacing whatever an earlier struct of that id left, so an id reused after a rollback starts clean.

Source

pub fn struct_default( &self, id: u16, field: usize, ) -> Option<(u16, &Expr, Span)>

The value field field of struct id was declared with, and the file it was written in.

Source

pub fn param_fields(&self, template_idx: usize) -> Vec<Option<SmolStr>>

For each type parameter of a struct template, the field declared with that parameter as its type. A literal written without type arguments takes each parameter from the value in that field.

Source

pub fn instantiation_of(&self, t: &DataType) -> Option<(usize, &[DataType])>

The declaration an instantiated type came from and the arguments it was made with, for a type that came from one.

Source

pub fn enum_template_with_variant(&self, variant: &str) -> Option<usize>

The generic enum declaration that declares a variant of this name, and the payload types the variant declares.

A generic enum has no registered type until something names one, so a bare Some(x) is looked up here rather than among the enums already registered. The last declaration of the name wins, the same as find_template does for a type name.

Source

pub fn only_the_prelude_declares(&self, variant: &str) -> bool

Whether the only generic enums declaring a variant called variant are the prelude’s.

Source

pub fn add_struct_template( &mut self, name: SmolStr, params: TypeParams, file_idx: u16, fields: Box<[(SmolStr, TypeExpr, Span)]>, defaults: Box<[Option<(Expr, Span)>]>, ) -> u32

Records a generic struct declaration and returns the index the declaring scope registers it under, which is what a name written behind a module path resolves to.

Source

pub fn add_enum_template( &mut self, name: SmolStr, params: TypeParams, file_idx: u16, variants: Box<[(SmolStr, Box<[TypeExpr]>, Span)]>, ) -> u32

Records a generic enum declaration. Returns its index, as Generics::add_struct_template does.

Source

pub fn defines_operator_methods(&self) -> bool

Records the impl blocks a file declared against a generic type, stamping each with the file it came from. Whether any impl block the program has read defines an operator, which makes an operator on that type a call to candela code.

Source

pub fn add_impls(&mut self, impls: Vec<ImplTemplate>, file_idx: u16)

Source

pub fn path_fn_of_template(&self, template: usize, name: &str) -> Option<String>

The path a receiverless function name of generic declaration template is called by, Set<T>::new(), when an impl block on it declares one.

Source

pub fn push_bindings(&mut self, frame: Box<[(SmolStr, DataType)]>)

Binds frame for the body about to be compiled. Every body pushes a frame, an empty one when it has no type parameters, so the caller’s parameters do not resolve inside it.

Source

pub fn push_frame(&mut self, frame: BindingFrame)

Binds a frame that also carries the parameters nothing named, which a function specialisation has and an impl block does not.

Source

pub fn pop_bindings(&mut self)

Source

pub const fn checkpoint(&self) -> GenericsCheckpoint

Snapshots the state a single generic instantiation (or a function specialization’s own type-parameter frame) can grow or unbalance, so an aborted compile attempt can undo it with Generics::rollback_to.

instantiations and the structs/enums it names are a cache: an instantiate call registers the pair before the instantiation’s own fields are resolved, so a type whose fields name itself resolves to the type being built instead of recursing. A diagnostic raised while resolving those fields (or while lowering an impl block against the instantiation, which pushes its lowered methods the same way a closure literal pushes an anonymous function) leaves the cache entry pointing at a struct or enum whose fields were never filled in. bindings and depth are pushed and incremented, respectively, before that same resolution and are only popped or decremented after it returns; a push_bindings without a matching pop_bindings (function specialization has its own such pair too, see functions/user_functions.rs) leaks a frame, and an unmatched depth increment eventually trips the instantiation-depth cap for programs nowhere near it.

Source

pub fn rollback_to(&mut self, checkpoint: &GenericsCheckpoint)

Undoes everything a failed compile attempt did to the instantiation cache and the binding/depth bookkeeping, back to checkpoint. The structs and enums an aborted instantiate call registered are not Generics’ to remove; the caller truncates those (see Program::rollback_to) using the same pre-attempt lengths.

Trait Implementations§

Source§

impl Debug for Generics

Source§

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

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

impl Default for Generics

Source§

fn default() -> Generics

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