-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It's now OS-independent and orders by type name automatically.
- Loading branch information
1 parent
d59fae0
commit 953f9b7
Showing
8 changed files
with
159 additions
and
122 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::cmp::Ordering; | ||
|
||
/// The unique Specta ID for the type. | ||
/// | ||
/// Be aware type aliases don't exist as far as Specta is concerned as they are flattened into their inner type by Rust's trait system. | ||
/// The Specta Type ID holds for the given properties: | ||
/// - `T::SID == T::SID` | ||
/// - `T::SID != S::SID` | ||
/// - `Type<T>::SID == Type<S>::SID` (unlike std::any::TypeId) | ||
/// - `&'a T::SID == &'b T::SID` (unlike std::any::TypeId which forces a static lifetime) | ||
/// - `Box<T> == Arc<T> == Rc<T>` (unlike std::any::TypeId) | ||
/// | ||
#[derive(Debug, Clone, Copy)] | ||
pub struct SpectaID { | ||
pub(crate) type_name: &'static str, | ||
pub(crate) hash: u64, | ||
} | ||
|
||
// We do custom impls so the order prefers type_name over hash. | ||
impl Ord for SpectaID { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
self.type_name | ||
.cmp(other.type_name) | ||
.then(self.hash.cmp(&other.hash)) | ||
} | ||
} | ||
|
||
// We do custom impls so the order prefers type_name over hash. | ||
impl PartialOrd<Self> for SpectaID { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
// We do custom impls so equals is by SID exclusively. | ||
impl Eq for SpectaID {} | ||
|
||
// We do custom impls so equals is by SID exclusively. | ||
impl PartialEq<Self> for SpectaID { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.hash.eq(&other.hash) | ||
} | ||
} | ||
|
||
/// The location of the impl block for a given type. This is used for error reporting. | ||
/// The content of it is transparent and is generated by the macros. | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct ImplLocation(pub(crate) &'static str); | ||
|
||
impl ImplLocation { | ||
/// Get the location as a string | ||
pub const fn as_str(&self) -> &'static str { | ||
self.0 | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use specta::{DefOpts, Type, TypeMap}; | ||
|
||
#[derive(Type)] | ||
#[specta(export = false)] | ||
pub struct A {} | ||
|
||
#[derive(Type)] | ||
#[specta(export = false)] | ||
pub struct B {} | ||
|
||
#[derive(Type)] | ||
#[specta(export = false)] | ||
pub struct C {} | ||
|
||
#[derive(Type)] | ||
#[specta(export = false)] | ||
pub struct Z {} | ||
|
||
#[derive(Type)] | ||
#[specta(export = false)] | ||
pub struct BagOfTypes { | ||
// Fields are outta order intentionally so we don't fluke the test | ||
a: A, | ||
z: Z, | ||
b: B, | ||
c: C, | ||
} | ||
|
||
#[test] | ||
fn test_sid() { | ||
// TODO: This is so hard for an end-user to work with. Add some convenience API's!!! | ||
let mut type_map = TypeMap::default(); | ||
// We are calling this for it's side-effects | ||
BagOfTypes::definition(DefOpts { | ||
parent_inline: false, | ||
type_map: &mut type_map, | ||
}); | ||
|
||
// `TypeMap` is a `BTreeMap` so it's sorted by SID. It should be sorted alphabetically by name | ||
assert_eq!( | ||
type_map | ||
.into_iter() | ||
.filter_map(|(_, t)| t.map(|t| t.name().clone())) | ||
.collect::<Vec<_>>(), | ||
["A", "B", "C", "Z"] | ||
); | ||
} |