forked from lun3x/multi_index_map
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
165 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use proc_macro2::TokenStream; | ||
|
||
use syn::{ | ||
parenthesized, | ||
parse::{Parse, ParseStream}, | ||
parse_quote, token, Attribute, Field, Ident, Path, Token, Type, Visibility, | ||
}; | ||
|
||
struct AttrInner { | ||
_paren_token: token::Paren, | ||
pub path: Path, | ||
pub tokens: TokenStream, | ||
} | ||
impl Parse for AttrInner { | ||
fn parse(input: ParseStream) -> Result<Self, syn::Error> { | ||
let content; | ||
Ok(Self { | ||
_paren_token: parenthesized!(content in input), | ||
path: content.call(Path::parse_mod_style)?, | ||
tokens: content.parse()?, | ||
}) | ||
} | ||
} | ||
struct ExtraFiledAst { | ||
_paren_token: token::Paren, | ||
pub directly: Ident, | ||
_s1: Token![,], | ||
pub func: Ident, | ||
_s2: Token![,], | ||
pub ty: Type, | ||
} | ||
impl Parse for ExtraFiledAst { | ||
fn parse(input: ParseStream) -> Result<Self, syn::Error> { | ||
let content; | ||
Ok(Self { | ||
_paren_token: parenthesized!(content in input), | ||
directly: content.parse()?, | ||
_s1: content.parse()?, | ||
func: content.parse()?, | ||
_s2: content.parse()?, | ||
ty: content.parse()?, | ||
}) | ||
} | ||
} | ||
|
||
pub trait IntoExtraFiled { | ||
type Error; | ||
fn into_extra_filed(&self, vis: Visibility) -> Result<Option<Field>, Self::Error>; | ||
} | ||
// impl TryFrom<Attribute> for Option<ExtraField> { | ||
impl IntoExtraFiled for Attribute { | ||
fn into_extra_filed(&self, vis: Visibility) -> Result<Option<Field>, Self::Error> { | ||
if self.path.is_ident("multi_index") { | ||
let tokens = &self.tokens; | ||
let inner: AttrInner = parse_quote!(#tokens); | ||
if inner.path.is_ident("extra_field") { | ||
let tokens = &inner.tokens; | ||
let ExtraFiledAst { | ||
directly, | ||
func: ident, | ||
ty, | ||
.. | ||
} = parse_quote!(#tokens); | ||
let attr: Attribute = parse_quote!(#[multi_index(#directly)] ); | ||
let ident = Some(ident); | ||
return Ok(Some(Field { | ||
attrs: vec![attr], | ||
vis: vis, | ||
ident, | ||
colon_token: None, | ||
ty, | ||
})); | ||
} | ||
} | ||
Ok(None) | ||
} | ||
|
||
type Error = syn::Error; | ||
} |
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,34 @@ | ||
use multi_index_map::MultiIndexMap; | ||
|
||
#[derive(Hash, PartialEq, Eq, Clone)] | ||
struct TestNonPrimitiveType(u64); | ||
|
||
#[derive(MultiIndexMap, Clone)] | ||
#[multi_index(extra_field(hashed_unique, field2_extra, String))] | ||
struct TestElement { | ||
#[multi_index(hashed_unique)] | ||
field1: TestNonPrimitiveType, | ||
field2: String, | ||
} | ||
impl TestElement { | ||
pub fn field2_extra(&self) -> String { | ||
format!("{}{}", self.field2, "111") | ||
} | ||
} | ||
#[test] | ||
fn test_insert_and_get() { | ||
let mut map = MultiIndexTestElementMap::default(); | ||
let elem1 = TestElement { | ||
field1: TestNonPrimitiveType(42), | ||
field2: "ElementOne".to_string(), | ||
}; | ||
map.insert(elem1); | ||
|
||
assert_eq!(map.len(), 1); | ||
assert_eq!( | ||
map.get_by_field2_extra(&"ElementOne111".to_owned()) | ||
.unwrap() | ||
.field2, | ||
"ElementOne" | ||
); | ||
} |