Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Gerwitz <mike.gerwitz@ryansg.com>2020-01-09 11:02:18 -0500
committerMike Gerwitz <mike.gerwitz@ryansg.com>2020-02-24 14:56:28 -0500
commite8cd378d5952f3ae51d4cfecf29d8f70426de8e9 (patch)
tree750495c911f7de1e06360a2e54564c9faf68c486
parentff0c8bb34fbf2616143205a9fb0848bc783f60a4 (diff)
downloadtame-e8cd378d5952f3ae51d4cfecf29d8f70426de8e9.tar.gz
tame-e8cd378d5952f3ae51d4cfecf29d8f70426de8e9.tar.bz2
tame-e8cd378d5952f3ae51d4cfecf29d8f70426de8e9.zip
TAMER: Display for Symbol
One of the benefits of storing a reference to the interned string on the symbol itself is that we get to get its underlying value essentially for free.
-rw-r--r--tamer/src/sym.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/tamer/src/sym.rs b/tamer/src/sym.rs
index b247601..13e2c02 100644
--- a/tamer/src/sym.rs
+++ b/tamer/src/sym.rs
@@ -263,6 +263,7 @@ use bumpalo::Bump;
use fxhash::FxBuildHasher;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
+use std::fmt;
use std::hash::BuildHasher;
use std::num::NonZeroU32;
use std::ops::Deref;
@@ -372,6 +373,16 @@ impl<'i> Deref for Symbol<'i> {
}
}
+impl<'i> fmt::Display for Symbol<'i> {
+ /// Display name of underlying string.
+ ///
+ /// Since symbols contain pointers to their interned slices,
+ /// we effectively get this for free.
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{}", self.str)
+ }
+}
+
/// Create, store, compare, and retrieve [`Symbol`] values.
///
/// Interners accept string slices and produce values of type [`Symbol`].
@@ -654,6 +665,13 @@ mod test {
assert_eq!(index, Symbol::new(index, "").index());
}
+
+ #[test]
+ fn displays_as_interned_value() {
+ let sym = Symbol::new(SymbolIndex::from_u32(1), "foo");
+
+ assert_eq!(format!("{}", sym), sym.str);
+ }
}
mod interner {