summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/typst-utils/src/lib.rs21
1 files changed, 20 insertions, 1 deletions
diff --git a/crates/typst-utils/src/lib.rs b/crates/typst-utils/src/lib.rs
index abe6423d..4cfe0c04 100644
--- a/crates/typst-utils/src/lib.rs
+++ b/crates/typst-utils/src/lib.rs
@@ -23,7 +23,7 @@ pub use self::scalar::Scalar;
#[doc(hidden)]
pub use once_cell;
-use std::fmt::{Debug, Formatter};
+use std::fmt::{Debug, Display, Formatter};
use std::hash::Hash;
use std::iter::{Chain, Flatten, Rev};
use std::num::{NonZeroU32, NonZeroUsize};
@@ -52,6 +52,25 @@ where
Wrapper(f)
}
+/// Turn a closure into a struct implementing [`Display`].
+pub fn display<F>(f: F) -> impl Display
+where
+ F: Fn(&mut Formatter) -> std::fmt::Result,
+{
+ struct Wrapper<F>(F);
+
+ impl<F> Display for Wrapper<F>
+ where
+ F: Fn(&mut Formatter) -> std::fmt::Result,
+ {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ self.0(f)
+ }
+ }
+
+ Wrapper(f)
+}
+
/// Calculate a 128-bit siphash of a value.
pub fn hash128<T: Hash + ?Sized>(value: &T) -> u128 {
let mut state = SipHasher13::new();