From 525154a730dfdb224fe2ced3dae0cfb33114fafa Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 11 Nov 2024 15:11:44 +0100 Subject: Add support for raw range spans --- crates/typst-utils/src/pico.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'crates/typst-utils/src') diff --git a/crates/typst-utils/src/pico.rs b/crates/typst-utils/src/pico.rs index dcab39b6..7fcd3343 100644 --- a/crates/typst-utils/src/pico.rs +++ b/crates/typst-utils/src/pico.rs @@ -1,6 +1,7 @@ use std::cmp::Ordering; use std::collections::HashMap; use std::fmt::{self, Debug, Formatter}; +use std::num::NonZeroU32; use std::sync::{LazyLock, RwLock}; /// The global string interner. @@ -21,7 +22,7 @@ struct Interner { /// unnecessarily. For this reason, the user should use the [`PicoStr::resolve`] /// method to get the underlying string, such that the lookup is done only once. #[derive(Copy, Clone, Eq, PartialEq, Hash)] -pub struct PicoStr(u32); +pub struct PicoStr(NonZeroU32); impl PicoStr { /// Creates a new interned string. @@ -38,7 +39,10 @@ impl PicoStr { // Create a new entry forever by leaking the string. PicoStr is only // used for strings that aren't created en masse, so it is okay. - let num = interner.from_id.len().try_into().expect("out of string ids"); + let num = u32::try_from(interner.from_id.len() + 1) + .and_then(NonZeroU32::try_from) + .expect("out of string ids"); + let id = Self(num); let string = Box::leak(string.to_string().into_boxed_str()); interner.to_id.insert(string, id); @@ -48,7 +52,7 @@ impl PicoStr { /// Resolves the interned string. pub fn resolve(&self) -> &'static str { - INTERNER.read().unwrap().from_id[self.0 as usize] + INTERNER.read().unwrap().from_id[(self.0.get() - 1) as usize] } } -- cgit v1.2.3