pub trait ToTypst {
// Required method
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result;
// Provided methods
fn to_typst(&self) -> TypstWrapper<'_, Self>
where Self: Sized { ... }
fn to_typst_string(&self) -> String
where Self: Sized { ... }
}Expand description
Converts a value to a Typst math-mode fragment.
The output is a fragment rather than a complete expression: it carries no $ of its own,
leaving that to the caller. That is what lets one fragment be embedded in another, so that a
value built out of smaller values can write its parts directly.
Every implementation guarantees that its output
- is valid wherever Typst is in math mode, and remains valid when wrapped in parentheses, so
that
(output)is well-formed; - leaves nothing open behind it: delimiters and string literals are closed, and nothing is defined or redefined.
Required Methods§
Sourcefn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a value as a Typst math-mode fragment.
This is the method implementors define. It takes a Formatter rather than returning a
String so that a value can write its parts into a caller’s buffer, which is what makes a
fragment embeddable without an allocation per level of nesting.
§Examples
use malachite_base::strings::typst::ToTypst;
use std::fmt::{Display, Formatter, Result};
// A type that embeds another value's fragment inside its own.
struct Negated(i32);
impl Display for Negated {
fn fmt(&self, f: &mut Formatter) -> Result {
f.write_str("-(")?;
self.0.fmt_typst(f)?;
f.write_str(")")
}
}
assert_eq!(Negated(5).to_string(), "-(5)");That fragment draws the negation of five.
Provided Methods§
Sourcefn to_typst(&self) -> TypstWrapper<'_, Self>where
Self: Sized,
fn to_typst(&self) -> TypstWrapper<'_, Self>where
Self: Sized,
Converts a value to a Typst math-mode fragment.
The returned TypstWrapper implements Display, so it can be converted to a String
with to_string, or written directly with write! and friends.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::strings::typst::ToTypst;
assert_eq!(123u32.to_typst_string(), "123");
assert_eq!((-45i16).to_typst_string(), "-45");
// The output is a fragment, so it can be embedded in a larger expression.
assert_eq!(format!("x^({})", 10u8.to_typst()), "x^(10)");Sourcefn to_typst_string(&self) -> Stringwhere
Self: Sized,
fn to_typst_string(&self) -> Stringwhere
Self: Sized,
Converts a value to a Typst math-mode fragment, as a String.
This is to_typst().to_string(), which is what a caller who wants the fragment itself,
rather than something to write into a Formatter, would otherwise have to say.
§Worst-case complexity
Same as the time and additional memory complexity of fmt_typst for Self.
§Examples
use malachite_base::strings::typst::ToTypst;
assert_eq!(123u32.to_typst_string(), "123");
assert_eq!((-45i16).to_typst_string(), "-45");
assert_eq!("100% α".to_typst_string(), r#""100% α""#);Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl ToTypst for &str
impl ToTypst for &str
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a string slice as a Typst math-mode fragment.
The fragment depicts the string: it is one quoted string, which Typst typesets as text, with
\ and " escaped and control characters spelled rather than written. Typst reads Unicode
natively, so no character needs a spelling of its own, and "100% α" comes out as itself.
No quotation marks beyond the string literal’s own are added; the fragment is the string’s
content and nothing else.
A run of superscript or subscript characters is the exception, and becomes a single script:
"2¹⁰" is two raised to the tenth rather than the two characters, which a text font may
not have at all. A run of one kind does not run into the next: "x¹₂" keeps its one
beside its two rather than stacking them.
The empty string becomes "" rather than nothing at all.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.chars().count().
§Examples
use malachite_base::strings::typst::ToTypst;
assert_eq!("hello".to_typst_string(), r#""hello""#);
assert_eq!("100%".to_typst_string(), r#""100%""#);
assert_eq!("100% α".to_typst_string(), r#""100% α""#);
assert_eq!("A ≤ B".to_typst_string(), r#""A ≤ B""#);
assert_eq!("--flag".to_typst_string(), r#""--flag""#);
assert_eq!("2¹⁰".to_typst_string(), r#""2"^("10")"#);
assert_eq!("H₂O".to_typst_string(), r#""H"_("2")"O""#);| value | fragment |
|---|---|
"hello" | "hello" |
"100%" | "100%" |
"100% α" | "100% α" |
"A ≤ B" | "A ≤ B" |
"--flag" | "--flag" |
"2¹⁰" | "2"^("10") |
"H₂O" | "H"_("2")"O" |
Source§impl ToTypst for ()
impl ToTypst for ()
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes the unit type as a Typst math-mode fragment.
The fragment is (), an empty pair of parentheses, since the unit type is the tuple of no
elements.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::strings::typst::ToTypst;
assert_eq!(().to_typst_string(), "()");| value | fragment |
|---|---|
() | () |
Source§impl ToTypst for String
impl ToTypst for String
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a String as a Typst math-mode fragment.
This is the same as the &str implementation.
The fragment depicts the string: it is one quoted string, which Typst typesets as text, with
\ and " escaped and control characters spelled rather than written. Typst reads Unicode
natively, so no character needs a spelling of its own, and "100% α" comes out as itself.
No quotation marks beyond the string literal’s own are added; the fragment is the string’s
content and nothing else.
A run of superscript or subscript characters is the exception, and becomes a single script:
"2¹⁰" is two raised to the tenth rather than the two characters, which a text font may
not have at all. A run of one kind does not run into the next: "x¹₂" keeps its one
beside its two rather than stacking them.
The empty string becomes "" rather than nothing at all.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.chars().count().
§Examples
use malachite_base::strings::typst::ToTypst;
assert_eq!("hello".to_string().to_typst_string(), r#""hello""#);
assert_eq!("100%".to_string().to_typst_string(), r#""100%""#);
assert_eq!("100% α".to_string().to_typst_string(), r#""100% α""#);
assert_eq!("A ≤ B".to_string().to_typst_string(), r#""A ≤ B""#);
assert_eq!("--flag".to_string().to_typst_string(), r#""--flag""#);
assert_eq!("2¹⁰".to_string().to_typst_string(), r#""2"^("10")"#);
assert_eq!("H₂O".to_string().to_typst_string(), r#""H"_("2")"O""#);| value | fragment |
|---|---|
"hello" | "hello" |
"100%" | "100%" |
"100% α" | "100% α" |
"A ≤ B" | "A ≤ B" |
"--flag" | "--flag" |
"2¹⁰" | "2"^("10") |
"H₂O" | "H"_("2")"O" |
Source§impl ToTypst for bool
impl ToTypst for bool
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a bool as a Typst math-mode fragment.
true becomes "T" and false becomes "F". The quotation marks make them a string,
which Typst typesets upright, rather than letting them be typeset as italic variables.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::strings::typst::ToTypst;
assert_eq!(true.to_typst_string(), r#""T""#);
assert_eq!(false.to_typst_string(), r#""F""#);| value | fragment |
|---|---|
true | "T" |
false | "F" |
Source§impl ToTypst for char
impl ToTypst for char
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a char as a Typst math-mode fragment.
The character is written inside a quoted string, where Typst typesets it as itself. Typst
reads Unicode natively, so a character needs no spelling of its own; only \ and ", and
the control characters, are written as escapes.
A superscript or subscript character is the exception: it becomes a real script, and is
given an empty base to attach to, since on its own it has none. '²' becomes ""^(2).
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::strings::typst::ToTypst;
assert_eq!('a'.to_typst_string(), r#""a""#);
assert_eq!('%'.to_typst_string(), r#""%""#);
assert_eq!('α'.to_typst_string(), r#""α""#);
assert_eq!('"'.to_typst_string(), r#""\"""#);
assert_eq!('²'.to_typst_string(), r#"""^("2")"#);| value | fragment |
|---|---|
'a' | "a" |
'%' | "%" |
'α' | "α" |
'"' | "\"" |
'²' | ""^("2") |
Source§impl ToTypst for f32
impl ToTypst for f32
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a primitive float as a Typst math-mode fragment.
A NaN becomes "NaN" and the infinities become infinity and -infinity. A finite
float is written as its NiceFloat representation, with the exponent, if there is
one, lifted into a real power of ten: 1.23e-3 becomes 1.23 times 10^(-3).
§Worst-case complexity
Constant time and additional memory.
§Examples
See here.
Source§impl ToTypst for f64
impl ToTypst for f64
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a primitive float as a Typst math-mode fragment.
A NaN becomes "NaN" and the infinities become infinity and -infinity. A finite
float is written as its NiceFloat representation, with the exponent, if there is
one, lifted into a real power of ten: 1.23e-3 becomes 1.23 times 10^(-3).
§Worst-case complexity
Constant time and additional memory.
§Examples
See here.
Source§impl ToTypst for i8
impl ToTypst for i8
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a primitive integer as a Typst math-mode fragment.
The fragment is the integer’s decimal digits, preceded by a minus sign if it is negative, which is what Typst math mode already writes a number as.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().
§Examples
See here.
Source§impl ToTypst for i16
impl ToTypst for i16
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a primitive integer as a Typst math-mode fragment.
The fragment is the integer’s decimal digits, preceded by a minus sign if it is negative, which is what Typst math mode already writes a number as.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().
§Examples
See here.
Source§impl ToTypst for i32
impl ToTypst for i32
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a primitive integer as a Typst math-mode fragment.
The fragment is the integer’s decimal digits, preceded by a minus sign if it is negative, which is what Typst math mode already writes a number as.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().
§Examples
See here.
Source§impl ToTypst for i64
impl ToTypst for i64
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a primitive integer as a Typst math-mode fragment.
The fragment is the integer’s decimal digits, preceded by a minus sign if it is negative, which is what Typst math mode already writes a number as.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().
§Examples
See here.
Source§impl ToTypst for i128
impl ToTypst for i128
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a primitive integer as a Typst math-mode fragment.
The fragment is the integer’s decimal digits, preceded by a minus sign if it is negative, which is what Typst math mode already writes a number as.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().
§Examples
See here.
Source§impl ToTypst for isize
impl ToTypst for isize
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a primitive integer as a Typst math-mode fragment.
The fragment is the integer’s decimal digits, preceded by a minus sign if it is negative, which is what Typst math mode already writes a number as.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().
§Examples
See here.
Source§impl ToTypst for self
impl ToTypst for self
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes an Ordering as a Typst math-mode fragment.
The three orderings become the three relations they stand for: <, =, and >.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::strings::typst::ToTypst;
use std::cmp::Ordering::*;
assert_eq!(Less.to_typst_string(), "<");
assert_eq!(Equal.to_typst_string(), "=");
assert_eq!(Greater.to_typst_string(), ">");| value | fragment |
|---|---|
Less | < |
Equal | = |
Greater | > |
Source§impl ToTypst for u8
impl ToTypst for u8
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a primitive integer as a Typst math-mode fragment.
The fragment is the integer’s decimal digits, preceded by a minus sign if it is negative, which is what Typst math mode already writes a number as.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().
§Examples
See here.
Source§impl ToTypst for u16
impl ToTypst for u16
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a primitive integer as a Typst math-mode fragment.
The fragment is the integer’s decimal digits, preceded by a minus sign if it is negative, which is what Typst math mode already writes a number as.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().
§Examples
See here.
Source§impl ToTypst for u32
impl ToTypst for u32
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a primitive integer as a Typst math-mode fragment.
The fragment is the integer’s decimal digits, preceded by a minus sign if it is negative, which is what Typst math mode already writes a number as.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().
§Examples
See here.
Source§impl ToTypst for u64
impl ToTypst for u64
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a primitive integer as a Typst math-mode fragment.
The fragment is the integer’s decimal digits, preceded by a minus sign if it is negative, which is what Typst math mode already writes a number as.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().
§Examples
See here.
Source§impl ToTypst for u128
impl ToTypst for u128
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a primitive integer as a Typst math-mode fragment.
The fragment is the integer’s decimal digits, preceded by a minus sign if it is negative, which is what Typst math mode already writes a number as.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().
§Examples
See here.
Source§impl ToTypst for usize
impl ToTypst for usize
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a primitive integer as a Typst math-mode fragment.
The fragment is the integer’s decimal digits, preceded by a minus sign if it is negative, which is what Typst math mode already writes a number as.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().
§Examples
See here.
Source§impl<A: ToTypst, B: ToTypst, C: ToTypst, D: ToTypst, E: ToTypst, F: ToTypst, G: ToTypst, H: ToTypst> ToTypst for (A, B, C, D, E, F, G, H)
impl<A: ToTypst, B: ToTypst, C: ToTypst, D: ToTypst, E: ToTypst, F: ToTypst, G: ToTypst, H: ToTypst> ToTypst for (A, B, C, D, E, F, G, H)
Source§impl<A: ToTypst, B: ToTypst, C: ToTypst, D: ToTypst, E: ToTypst, F: ToTypst, G: ToTypst> ToTypst for (A, B, C, D, E, F, G)
impl<A: ToTypst, B: ToTypst, C: ToTypst, D: ToTypst, E: ToTypst, F: ToTypst, G: ToTypst> ToTypst for (A, B, C, D, E, F, G)
Source§impl<A: ToTypst, B: ToTypst, C: ToTypst, D: ToTypst, E: ToTypst, F: ToTypst> ToTypst for (A, B, C, D, E, F)
impl<A: ToTypst, B: ToTypst, C: ToTypst, D: ToTypst, E: ToTypst, F: ToTypst> ToTypst for (A, B, C, D, E, F)
Source§impl<K: Eq + Hash + Ord + ToTypst, V: ToTypst> ToTypst for HashMap<K, V>
impl<K: Eq + Hash + Ord + ToTypst, V: ToTypst> ToTypst for HashMap<K, V>
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a HashMap as a LaTeX math-mode fragment.
Each entry is written as its key, a “maps to” arrow, and its value; the entries are separated by commas and wrapped in braces, as a map is a set of associations.
The entries are sorted by key first, which is why this asks for Ord where a HashMap
does not. A HashMap iterates in an order that depends on its hasher, so without sorting
two equal maps could have different fragments, and the same map could have a different
fragment in the next run. Sorting also makes a HashMap’s fragment agree with the
BTreeMap of the same entries.
Typst grows a matched pair of delimiters to fit what is between them, so the braces fit an
entry that is taller than one line without being asked to. An empty map becomes {} rather
than nothing at all.
§Worst-case complexity
$T(n) = O(n \log n + \sum_{i=0}^{n-1}(T^\prime(i) + T^{\prime\prime}(i)))$
$M(n) = O(n + \max_{i=0}^{n-1}(M^\prime(i) + M^{\prime\prime}(i)))$
where $T$ is time, $M$ is additional memory, $n$ is self.len(), $i$ is an entry’s index,
$T^\prime$ and $M^\prime$ are the time and memory functions of fmt_typst for K, and
$T^{\prime\prime}$ and $M^{\prime\prime}$ are those for V.
§Examples
use malachite_base::strings::typst::ToTypst;
use std::collections::HashMap;
let empty = HashMap::<u8, u8>::new();
assert_eq!(empty.to_typst_string(), "{}");
// The entries are sorted by key, so the fragment does not depend on the hasher.
let m = HashMap::from([(2u8, 20u8), (1, 10)]);
assert_eq!(m.to_typst_string(), "{1 |-> 10, 2 |-> 20}");| value | fragment |
|---|---|
HashMap::<u8, u8>::new() | {} |
HashMap::from([(2u8, 20u8), (1, 10)]) | {1 |-> 10, 2 |-> 20} |
Source§impl<K: ToTypst, V: ToTypst> ToTypst for BTreeMap<K, V>
impl<K: ToTypst, V: ToTypst> ToTypst for BTreeMap<K, V>
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a BTreeMap as a LaTeX math-mode fragment.
Each entry is written as its key, a “maps to” arrow, and its value; the entries are separated by commas and wrapped in braces, as a map is a set of associations. The entries come in the map’s own order, which is ascending by key.
Typst grows a matched pair of delimiters to fit what is between them, so the braces fit an
entry that is taller than one line without being asked to. An empty map becomes {} rather
than nothing at all.
§Worst-case complexity
$T(n) = O(n + \sum_{i=0}^{n-1}(T^\prime(i) + T^{\prime\prime}(i)))$
$M(n) = O(\max_{i=0}^{n-1}(M^\prime(i) + M^{\prime\prime}(i)))$
where $T$ is time, $M$ is additional memory, $n$ is self.len(), $i$ is an entry’s index,
$T^\prime$ and $M^\prime$ are the time and memory functions of fmt_typst for K, and
$T^{\prime\prime}$ and $M^{\prime\prime}$ are those for V.
§Examples
use malachite_base::strings::typst::ToTypst;
use std::collections::BTreeMap;
let empty = BTreeMap::<u8, u8>::new();
assert_eq!(empty.to_typst_string(), "{}");
let m = BTreeMap::from([(2u8, 20u8), (1, 10)]);
assert_eq!(m.to_typst_string(), "{1 |-> 10, 2 |-> 20}");| value | fragment |
|---|---|
BTreeMap::<u8, u8>::new() | {} |
BTreeMap::from([(2u8, 20u8), (1, 10)]) | {1 |-> 10, 2 |-> 20} |
Source§impl<T: Eq + Hash + Ord + ToTypst> ToTypst for HashSet<T>
impl<T: Eq + Hash + Ord + ToTypst> ToTypst for HashSet<T>
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a HashSet as a LaTeX math-mode fragment.
The elements’ fragments are separated by commas and wrapped in braces, as a set is written in mathematics.
The elements are sorted first, which is why this asks for Ord where a HashSet does
not. A HashSet iterates in an order that depends on its hasher, so without sorting two
equal sets could have different fragments, and the same set could have a different fragment
in the next run. Sorting also makes a HashSet’s fragment agree with the BTreeSet of
the same elements.
Typst grows a matched pair of delimiters to fit what is between them, so the braces fit an
element that is taller than one line without being asked to. An empty set becomes {}
rather than nothing at all.
§Worst-case complexity
$T(n) = O(n \log n + \sum_{i=0}^{n-1}T^\prime(i))$
$M(n) = O(n + \max_{i=0}^{n-1}M^\prime(i))$
where $T$ is time, $M$ is additional memory, $n$ is self.len(), $i$ is an element’s index,
and $T^\prime$ and $M^\prime$ are the time and memory functions of fmt_typst for T.
§Examples
use malachite_base::strings::typst::ToTypst;
use std::collections::HashSet;
let empty = HashSet::<u8>::new();
assert_eq!(empty.to_typst_string(), "{}");
// The elements are sorted, so the fragment does not depend on the hasher.
let xs = HashSet::from([3u8, 1, 2]);
assert_eq!(xs.to_typst_string(), "{1, 2, 3}");| value | fragment |
|---|---|
HashSet::<u8>::new() | {} |
HashSet::from([3u8, 1, 2]) | {1, 2, 3} |
Source§impl<T: ToTypst, const N: usize> ToTypst for [T; N]
impl<T: ToTypst, const N: usize> ToTypst for [T; N]
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes an array as a Typst math-mode fragment.
This is the same as the slice implementation.
§Worst-case complexity
$T(n) = O(n + \sum_{i=0}^{n-1}T^\prime(i))$
$M(n) = O(\max_{i=0}^{n-1}M^\prime(i))$
where $T$ is time, $M$ is additional memory, $n$ is N, $i$ is an element’s index, and
$T^\prime$ and $M^\prime$ are the time and memory functions of fmt_typst for T.
§Examples
use malachite_base::strings::typst::ToTypst;
assert_eq!([0u8; 0].to_typst_string(), "[]");
assert_eq!([5u8].to_typst_string(), "[5]");
assert_eq!([1u8, 2, 3].to_typst_string(), "[1, 2, 3]");
assert_eq!([[1u8, 2], [3, 4]].to_typst_string(), "[[1, 2], [3, 4]]");| value | fragment |
|---|---|
[0u8; 0] | [] |
[5u8] | [5] |
[1u8, 2, 3] | [1, 2, 3] |
[[1u8, 2], [3, 4]] | [[1, 2], [3, 4]] |
Source§impl<T: ToTypst> ToTypst for &T
impl<T: ToTypst> ToTypst for &T
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a reference as a Typst math-mode fragment.
The fragment is the referent’s own, so a reference is invisible: &5u8 and 5u8 have the
same fragment. That is what lets a value be written without being dereferenced first, and a
collection of references be written at all.
&str and slices have implementations of their own rather than reaching this one, since
their referents are unsized. They write the same fragments either way.
§Worst-case complexity
Same as the time and additional memory complexity of fmt_typst for T.
§Examples
use malachite_base::strings::typst::ToTypst;
// A reference is invisible, which is what lets a collection of references be written.
assert_eq!(
vec![&1u8, &2u8].to_typst_string(),
vec![1u8, 2u8].to_typst_string()
);
// A method call on a reference resolves to the referent's own implementation, so this is
// reached through a generic context instead.
fn fragment<T: ToTypst>(x: T) -> String {
x.to_typst_string()
}
let n = 5u8;
let n_ref: &u8 = &n;
assert_eq!(fragment(n_ref), "5");Source§impl<T: ToTypst> ToTypst for &[T]
impl<T: ToTypst> ToTypst for &[T]
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a slice as a Typst math-mode fragment.
The elements’ fragments are separated by commas and wrapped in square brackets, so that a slice’s fragment is built out of its elements’ own.
The brackets are not decoration: without them [1, 2] and [[1], [2]] would both be 1, 2, and distinct values would have the same fragment. Typst grows a matched pair of
delimiters to fit what is between them, so they fit an element that is taller than one line,
such as a fraction or a nested slice, without being asked to.
An empty slice becomes [] rather than nothing at all.
§Worst-case complexity
$T(n) = O(n + \sum_{i=0}^{n-1}T^\prime(i))$
$M(n) = O(\max_{i=0}^{n-1}M^\prime(i))$
where $T$ is time, $M$ is additional memory, $n$ is self.len(), $i$ is an element’s index,
and $T^\prime$ and $M^\prime$ are the time and memory functions of fmt_typst for T.
§Examples
use malachite_base::strings::typst::ToTypst;
assert_eq!([0u8; 0].as_slice().to_typst_string(), "[]");
assert_eq!([5u8].as_slice().to_typst_string(), "[5]");
assert_eq!([1u8, 2, 3].as_slice().to_typst_string(), "[1, 2, 3]");
assert_eq!(["hi", "yo"].as_slice().to_typst_string(), r#"["hi", "yo"]"#);| value | fragment |
|---|---|
[0u8; 0] | [] |
[5u8] | [5] |
[1u8, 2, 3] | [1, 2, 3] |
["hi", "yo"] | ["hi", "yo"] |
Source§impl<T: ToTypst> ToTypst for BTreeSet<T>
impl<T: ToTypst> ToTypst for BTreeSet<T>
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a BTreeSet as a LaTeX math-mode fragment.
The elements’ fragments are separated by commas and wrapped in braces, as a set is written in mathematics. The elements come in the set’s own order, which is ascending.
Typst grows a matched pair of delimiters to fit what is between them, so the braces fit an
element that is taller than one line without being asked to. An empty set becomes {}
rather than nothing at all.
§Worst-case complexity
$T(n) = O(n + \sum_{i=0}^{n-1}T^\prime(i))$
$M(n) = O(\max_{i=0}^{n-1}M^\prime(i))$
where $T$ is time, $M$ is additional memory, $n$ is self.len(), $i$ is an element’s index,
and $T^\prime$ and $M^\prime$ are the time and memory functions of fmt_typst for T.
§Examples
use malachite_base::strings::typst::ToTypst;
use std::collections::BTreeSet;
let empty = BTreeSet::<u8>::new();
assert_eq!(empty.to_typst_string(), "{}");
let xs = BTreeSet::from([3u8, 1, 2]);
assert_eq!(xs.to_typst_string(), "{1, 2, 3}");| value | fragment |
|---|---|
BTreeSet::<u8>::new() | {} |
BTreeSet::from([3u8, 1, 2]) | {1, 2, 3} |
Source§impl<T: ToTypst> ToTypst for Option<T>
impl<T: ToTypst> ToTypst for Option<T>
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes an Option as a Typst math-mode fragment.
None becomes bot, and Some wraps its value in square brackets. The brackets are
not decoration: without them Some(None) and None would both be bot, and distinct
values would have the same fragment.
Typst grows a matched pair of delimiters to fit what is between them, so the brackets fit a
value that is taller than one line, such as a fraction or a nested Option, without being
asked to.
§Worst-case complexity
Same as the time and additional memory complexity of fmt_typst for T.
§Examples
use malachite_base::strings::typst::ToTypst;
assert_eq!(None::<u8>.to_typst_string(), "bot");
assert_eq!(Some(5u8).to_typst_string(), "[5]");
assert_eq!(Some("hi").to_typst_string(), r#"["hi"]"#);
// The brackets keep nested `Option`s apart.
assert_eq!(Some(None::<u8>).to_typst_string(), "[bot]");
assert_eq!(Some(Some(5u8)).to_typst_string(), "[[5]]");| value | fragment |
|---|---|
None::<u8> | bot |
Some(5u8) | [5] |
Some("hi") | ["hi"] |
Some(None::<u8>) | [bot] |
Some(Some(5u8)) | [[5]] |
Source§impl<T: ToTypst> ToTypst for Vec<T>
impl<T: ToTypst> ToTypst for Vec<T>
Source§fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result
Writes a Vec as a Typst math-mode fragment.
This is the same as the slice implementation.
§Worst-case complexity
$T(n) = O(n + \sum_{i=0}^{n-1}T^\prime(i))$
$M(n) = O(\max_{i=0}^{n-1}M^\prime(i))$
where $T$ is time, $M$ is additional memory, $n$ is self.len(), $i$ is an element’s index,
and $T^\prime$ and $M^\prime$ are the time and memory functions of fmt_typst for T.
§Examples
use malachite_base::strings::typst::ToTypst;
assert_eq!(Vec::<u8>::new().to_typst_string(), "[]");
assert_eq!(vec![5u8].to_typst_string(), "[5]");
assert_eq!(vec![1u8, 2, 3].to_typst_string(), "[1, 2, 3]");
assert_eq!(
vec![vec![1u8], vec![2, 3]].to_typst_string(),
"[[1], [2, 3]]"
);| value | fragment |
|---|---|
Vec::<u8>::new() | [] |
vec![5u8] | [5] |
vec![1u8, 2, 3] | [1, 2, 3] |
vec![vec![1u8], vec![2, 3]] | [[1], [2, 3]] |