Skip to main content

TeXString

Struct TeXString 

Source
pub struct TeXString(/* private fields */);
Expand description

A string of TeX markup — text that is safe to hand back to the tokenizer.

It is not a path and not an input .tex file (source_directory, --source-map and docs/performance/SOURCE_PROVENANCE.md own that sense of “source”); it is the character content a crate::mouth::Mouth will read.

§Why the type exists

Flattening Tokens with Display welds control words. TeX consumes the space that terminates a control word, so \v S tokenizes to [\v][S]; re-emitting that with Display gives \vS, a control sequence that exists in no LaTeX. Tokens::untex re-emits the space, Display deliberately does not — this is faithful to Perl (Core/Tokens.pm:61 toString joins the token strings, and Core/Token.pm:306 returns a CS name with no trailing space), whose own comment says the result is “NOT for creating valid TeX (use revert or UnTeX for that!)”.

Perl relies on author discipline there. It has failed three times in this port — \bib@@names (PR #399), dcolumn/overpic (PR #400), and the MathSciNet review path (issue 410: MRREVIEWER = {Fran\c cois\ Digne} became undefined:\ccois) — each found by a user-visible failure years after the code was written. TeXString makes the mistake unrepresentable instead: the tokenizing sinks take impl Into<TeXString>, and a bare String has no way in.

§The three ways in

  • From<&'static str> — a string literal in a binding is TeX its author typed by hand, so it converts implicitly and the ~125 literal call sites stay untouched. There is deliberately no From<String> and no From<&str>: those are exactly the shapes a welded Tokens::to_string() arrives in, and s!(…)/format!(…) returns the former.
  • Tokens::untex_string — the blessed path from Tokens.
  • TeXString::assembled — the explicit escape hatch, for a format! of literal TeX around already-safe pieces. It names the obligation it imposes.
let s: TeXString = r"\relax".into(); // literal: implicit
assert_eq!(s.as_str(), r"\relax");

A String cannot get in on its own — this is the guard, and it bites:

let welded: String = String::from(r"\vS");
let _: TeXString = welded.into(); // no `From<String>`: does not compile

Implementations§

Source§

impl TeXString

Source

pub fn assembled(tex: String) -> Self

Assert that an owned String is valid TeX markup.

The caller’s obligation: every interpolated fragment must be either literal TeX written at the call site, or a fragment that came from Tokens::untex_string / another TeXString. It must not be a bare Tokens::to_string() — that is the welding bug this type exists to prevent (\v S\vS); use Tokens::untex_string for those.

The typical honest use is a format! whose shape is literal TeX:

let counter = "section";
let tex = TeXString::assembled(format!(r"\the{counter}"));
assert_eq!(tex.as_str(), r"\thesection");
Source

pub fn as_str(&self) -> &str

The TeX markup, borrowed.

Source

pub fn into_string(self) -> String

The TeX markup, owned (allocates only when this was built from a literal).

Source

pub fn is_empty(&self) -> bool

Is there any markup at all?

Trait Implementations§

Source§

impl AsRef<str> for TeXString

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for TeXString

Source§

fn clone(&self) -> TeXString

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 TeXString

Source§

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

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

impl Default for TeXString

Source§

fn default() -> TeXString

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

impl Display for TeXString

Source§

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

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

impl Eq for TeXString

Source§

impl From<&'static str> for TeXString

Source§

fn from(tex: &'static str) -> Self

A &'static str in a binding is a TeX literal its author typed by hand.

Deliberately the only blanket string conversion — see the type docs.

Source§

impl Hash for TeXString

Source§

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

Feeds this value into the given Hasher. Read more
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 TeXString

Source§

fn eq(&self, other: &TeXString) -> 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 TeXString

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
§

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

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.