diff --git a/src/collections/string_pool.rs b/src/collections/string_pool.rs index 5a31cb902ff697833f222b25997d0e90574810b4..80ced6346d462aef57339049fc57f06395c1e473 100644 --- a/src/collections/string_pool.rs +++ b/src/collections/string_pool.rs @@ -1,4 +1,4 @@ -use std::ptr::null_mut; +use std::ptr::{null_mut, null}; use std::hash::{Hash, Hasher}; use std::marker::PhantomData; use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; @@ -29,6 +29,12 @@ impl<'a> StringRef<'a> { StringRef{ data, length, _phantom: PhantomData } } + /// `new_empty` creates a empty StringRef. It is a null pointer with a + /// length of zero. + pub(crate) fn new_empty() -> StringRef<'static> { + StringRef{ data: null(), length: 0, _phantom: PhantomData } + } + pub fn as_str(&self) -> &'a str { unsafe { let slice = std::slice::from_raw_parts::<'a, u8>(self.data, self.length); @@ -161,6 +167,13 @@ unsafe impl Send for StringPool {} mod tests { use super::*; + #[test] + fn display_empty_string_ref() { + // Makes sure that null pointer inside StringRef will not cause issues + let v = StringRef::new_empty(); + let val = format!("{}{:?}", v, v); + } + #[test] fn test_string_just_fits() { let large = "0".repeat(SLAB_SIZE);