Created
March 6, 2019 11:43
-
-
Save michaelwoerister/85ae5e9bf443a9ee0e5e3f55bbd4c682 to your computer and use it in GitHub Desktop.
StringTable Sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MAX_PRE_RESERVED_STRING_ID: StringId = StringId(u32::MAX / 2); | |
#[derive(Clone, Copy, Eq, PartialEq, Debug)] | |
struct Addr(u32); | |
trait SerializationSink { | |
fn alloc(&self) -> (Addr, &mut [u8]); | |
} | |
/// Write-only version of the string table | |
struct StringTableBuilder<S: SerializationSink> { | |
data_sink: Arc<S>, | |
index_sink: Arc<S>, | |
id_counter: AromicU32, // initialized to MAX_PRE_RESERVED_STRING_ID + 1 | |
} | |
struct StringId(i32); | |
trait SerializableString { | |
fn serialized_size(&self) -> usize; | |
fn serialize(&self, bytes: &mut [u8]); | |
} | |
impl SerializableString for str { | |
fn serialized_size(&self) -> usize { | |
unimplemented!() | |
} | |
fn serialize(&self, bytes: &mut [u8]) { | |
unimplemented!() | |
} | |
} | |
enum StringComponent<'s> { | |
Value(&'s str), | |
Ref(StringId) | |
} | |
impl<'a> SerializableString for [StringComponent<'a>] { | |
fn serialized_size(&self) -> usize { | |
unimplemented!() | |
} | |
fn serialize(&self, bytes: &mut [u8]) { | |
unimplemented!() | |
} | |
} | |
impl<'s> StringComponent<'s> { | |
fn serialized_size(&self) -> usize { | |
unimplemented!() | |
} | |
} | |
fn serialize_index_entry<S: SerializationSink>(sink: &S, id: StringId, addr: Addr) { | |
unimplemented!() | |
} | |
impl StringTableBuilder { | |
pub fn alloc_with_reserved_id<STR: SerializableString>(&self, id: StringId, s: &STR) -> StringId { | |
assert!(id.0 <= MAX_PRE_RESERVED_STRING_ID); | |
self.alloc_unchecked(id, s); | |
id | |
} | |
pub fn alloc<STR: SerializableString>(&self, s: &STR) -> StringId { | |
let id = StringId(self.id_counter.fetch_add(1, Ordering::SeqCst)); | |
debug_assert!(id.0 > MAX_PRE_RESERVED_STRING_ID); | |
self.alloc_unchecked(id, s); | |
id | |
} | |
fn alloc_unchecked<STR: SerializableString>(&self, id: StringId, s: &STR) { | |
let size_in_bytes = s.serialized_size(); | |
let (addr, mem) = self.data_sink.alloc(size_in_bytes); | |
s.serialize(mem); | |
serialize_index_entry(&self.index_sink, id, addr); | |
} | |
} | |
#[derive(Copy, Clone)] | |
struct StringRef<'st> { | |
id: StringId, | |
table: &'st StringTable, | |
} | |
impl<'st> StringRef<'st> { | |
pub fn to_string(&self) -> Cow<&'st str> { | |
unimplemented!() | |
} | |
pub fn write_to_string(&self, output: &mut String) { | |
unimplemented!() | |
} | |
} | |
/// Read-only version of the string table | |
struct StringTable { | |
// TODO: Replace with something lazy maybe | |
data: Vec<u8>, | |
index: FxHashSet<StringId, Addr>, | |
} | |
impl StringTable { | |
pub fn new(data_file: &Path, index_file: &Path) -> StringTable { | |
unimplemented!() | |
} | |
pub fn get(&self, id: StringId) -> StringRef { | |
unimplemented! | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment