diff options
Diffstat (limited to 'src/util/mod.rs')
| -rw-r--r-- | src/util/mod.rs | 135 |
1 files changed, 34 insertions, 101 deletions
diff --git a/src/util/mod.rs b/src/util/mod.rs index df3858d6..e0ba312f 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,18 +1,14 @@ //! Utilities. #[macro_use] -mod eco_string; -mod mac_roman; -mod prehashed; +mod eco; +mod hash; -pub use eco_string::EcoString; -pub use mac_roman::decode_mac_roman; -pub use prehashed::Prehashed; +pub use eco::EcoString; +pub use hash::Prehashed; use std::any::TypeId; -use std::cmp::Ordering; use std::fmt::{self, Debug, Formatter}; -use std::ops::Range; use std::path::{Component, Path, PathBuf}; use std::sync::Arc; @@ -35,35 +31,6 @@ where Wrapper(f) } -/// An alternative type id that prints as something readable in debug mode. -#[derive(Copy, Clone, Eq, PartialEq, Hash)] -pub struct ReadableTypeId { - id: TypeId, - #[cfg(debug_assertions)] - name: &'static str, -} - -impl ReadableTypeId { - /// The type id of the given type. - pub fn of<T: 'static>() -> Self { - Self { - id: TypeId::of::<T>(), - #[cfg(debug_assertions)] - name: std::any::type_name::<T>(), - } - } -} - -impl Debug for ReadableTypeId { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - #[cfg(debug_assertions)] - f.pad(self.name)?; - #[cfg(not(debug_assertions))] - f.pad("ReadableTypeId")?; - Ok(()) - } -} - /// Extra methods for [`str`]. pub trait StrExt { /// The number of code units this string would use if it was encoded in @@ -77,41 +44,6 @@ impl StrExt for str { } } -/// Extra methods for [`Option<T>`]. -pub trait OptionExt<T> { - /// Sets `other` as the value if `self` is `None` or if it contains a value - /// larger than `other`. - fn set_min(&mut self, other: T) - where - T: Ord; - - /// Sets `other` as the value if `self` is `None` or if it contains a value - /// smaller than `other`. - fn set_max(&mut self, other: T) - where - T: Ord; -} - -impl<T> OptionExt<T> for Option<T> { - fn set_min(&mut self, other: T) - where - T: Ord, - { - if self.as_ref().map_or(true, |x| other < *x) { - *self = Some(other); - } - } - - fn set_max(&mut self, other: T) - where - T: Ord, - { - if self.as_ref().map_or(true, |x| other > *x) { - *self = Some(other); - } - } -} - /// Extra methods for [`Arc`]. pub trait ArcExt<T> { /// Takes the inner value if there is exactly one strong reference and @@ -131,7 +63,7 @@ where } } -/// Extra methods for `[T]`. +/// Extra methods for [`[T]`](slice). pub trait SliceExt<T> { /// Split a slice into consecutive runs with the same key and yield for /// each such run the key and the slice of elements with that key. @@ -170,34 +102,6 @@ where } } -/// Extra methods for [`Range<usize>`]. -pub trait RangeExt { - /// Locate a position relative to a range. - /// - /// This can be used for binary searching the range that contains the - /// position as follows: - /// ``` - /// # use typst::util::RangeExt; - /// assert_eq!( - /// [1..2, 2..7, 7..10].binary_search_by(|r| r.locate(5)), - /// Ok(1), - /// ); - /// ``` - fn locate(&self, pos: usize) -> Ordering; -} - -impl RangeExt for Range<usize> { - fn locate(&self, pos: usize) -> Ordering { - if pos < self.start { - Ordering::Greater - } else if pos < self.end { - Ordering::Equal - } else { - Ordering::Less - } - } -} - /// Extra methods for [`Path`]. pub trait PathExt { /// Lexically normalize a path. @@ -222,3 +126,32 @@ impl PathExt for Path { out } } + +/// An alternative type id that prints as something readable in debug mode. +#[derive(Copy, Clone, Eq, PartialEq, Hash)] +pub struct ReadableTypeId { + id: TypeId, + #[cfg(debug_assertions)] + name: &'static str, +} + +impl ReadableTypeId { + /// The type id of the given type. + pub fn of<T: 'static>() -> Self { + Self { + id: TypeId::of::<T>(), + #[cfg(debug_assertions)] + name: std::any::type_name::<T>(), + } + } +} + +impl Debug for ReadableTypeId { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + #[cfg(debug_assertions)] + f.pad(self.name)?; + #[cfg(not(debug_assertions))] + f.pad("ReadableTypeId")?; + Ok(()) + } +} |
