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.
master
Mike Gerwitz 2020-01-09 11:02:18 -05:00
parent ff0c8bb34f
commit e8cd378d59
1 changed files with 18 additions and 0 deletions

View File

@ -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 {