-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make it possible to extend type data of schemas after registrat…
…ion. (#250) This replaces the `SchemaTypeMap` with a new `TypeDatas` type that can have new type datas registered while only requring a read-only reference allowing the type datas of already-registered schemas to be updated. This allows for a pattern similar to Rust's traits where a new crate that can create their own type data type, and register it for foreign types.
- Loading branch information
Showing
10 changed files
with
134 additions
and
47 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,5 @@ mod vec; | |
pub use map::*; | ||
mod map; | ||
|
||
pub use type_set::*; | ||
mod type_set; | ||
pub use type_datas::*; | ||
mod type_datas; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use append_only_vec::AppendOnlyVec; | ||
|
||
use crate::prelude::*; | ||
|
||
/// A `TypeMap`-like structure, that does not allow removing entries or updating exisintg | ||
/// entries. | ||
/// | ||
/// This structure doesn't require a mutable reference to insert records | ||
#[derive(Debug)] | ||
pub struct TypeDatas(AppendOnlyVec<SchemaBox>); | ||
impl Default for TypeDatas { | ||
fn default() -> Self { | ||
Self(AppendOnlyVec::new()) | ||
} | ||
} | ||
impl Clone for TypeDatas { | ||
fn clone(&self) -> Self { | ||
let clone = TypeDatas::default(); | ||
for entry in self.0.iter() { | ||
clone.insert(entry.clone()).unwrap(); | ||
} | ||
clone | ||
} | ||
} | ||
|
||
impl TypeDatas { | ||
/// Insert data into the store. | ||
pub fn insert<T: HasSchema>(&self, data: T) -> Result<(), TypeDataAlreadyInserted> { | ||
self.insert_box(SchemaBox::new(data)) | ||
} | ||
|
||
/// Insert boxed data into the store. | ||
pub fn insert_box(&self, data: SchemaBox) -> Result<(), TypeDataAlreadyInserted> { | ||
let schema = data.schema(); | ||
for entry in self.0.iter() { | ||
if entry.schema() == schema { | ||
return Err(TypeDataAlreadyInserted(schema)); | ||
} | ||
} | ||
self.0.push(data); | ||
Ok(()) | ||
} | ||
|
||
/// Borrow data from the store, if it exists. | ||
pub fn get<T: HasSchema>(&self) -> Option<&T> { | ||
let id = T::schema().id(); | ||
for data in self.0.iter() { | ||
if data.schema().id() == id { | ||
return Some(data.cast_ref()); | ||
} | ||
} | ||
None | ||
} | ||
|
||
/// Borrow data from the store, if it exists. | ||
pub fn get_ref(&self, id: SchemaId) -> Option<SchemaRef> { | ||
for data in self.0.iter() { | ||
if data.schema().id() == id { | ||
return Some(data.as_ref()); | ||
} | ||
} | ||
None | ||
} | ||
|
||
/// Iterate over type datas. | ||
pub fn iter(&self) -> impl DoubleEndedIterator<Item = &SchemaBox> { | ||
self.0.iter() | ||
} | ||
} | ||
|
||
/// Error type for [`TypeDatas`] | ||
#[derive(Debug)] | ||
pub struct TypeDataAlreadyInserted(&'static Schema); | ||
|
||
impl std::fmt::Display for TypeDataAlreadyInserted { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.write_fmt(format_args!( | ||
"Type data already contains an entry of type: {}", | ||
self.0.full_name, | ||
)) | ||
} | ||
} | ||
impl std::error::Error for TypeDataAlreadyInserted {} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn smoke() { | ||
let tds = TypeDatas::default(); | ||
|
||
tds.insert(String::from("hi")).unwrap(); | ||
assert_eq!(Some("hi"), tds.get::<String>().map(|x| x.as_str())); | ||
|
||
tds.insert(7u32).unwrap(); | ||
assert_eq!(Some(&7), tds.get::<u32>()); | ||
|
||
let result = tds.insert(String::from("bye")); | ||
assert!(matches!(result, Err(TypeDataAlreadyInserted(_)))); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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