-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement valuable value pointer #59
Open
taiki-e
wants to merge
14
commits into
master
Choose a base branch
from
taiki-e/pointer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5acfd45
Implement valuable value pointer
taiki-e 98efc04
temporarily hide Pointer::step from doc
taiki-e 2ae881c
cleanup
taiki-e 32df15f
Override visit_pointer on slice, Vec, VecDeque
taiki-e a713d89
Merge remote-tracking branch 'origin/master' into taiki-e/pointer
taiki-e 7ed1114
Add explanation to pointer module
taiki-e fd43080
Merge remote-tracking branch 'origin/master' into taiki-e/pointer
taiki-e acf64f7
Override visit_pointer on struct
taiki-e b84bc52
fix doc links
taiki-e 216fda5
fix ui test
taiki-e 6e93711
remove needless #[non_exhaustive]
taiki-e 382674d
Merge remote-tracking branch 'origin/master' into taiki-e/pointer
taiki-e 6952c8a
Merge branch 'master' into taiki-e/pointer
taiki-e fd2b30a
Merge remote-tracking branch 'origin/master' into taiki-e/pointer
taiki-e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 valuable::{pointer, Valuable, Value, Visit}; | ||
|
||
#[derive(Valuable)] | ||
struct Struct1 { | ||
x: String, | ||
y: Struct2, | ||
} | ||
|
||
#[derive(Valuable)] | ||
struct Struct2 { | ||
z: String, | ||
} | ||
|
||
#[derive(Default)] | ||
struct CollectValues(Vec<String>); | ||
|
||
impl Visit for CollectValues { | ||
fn visit_value(&mut self, value: Value<'_>) { | ||
self.0.push(format!("{:?}", value)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn basic() { | ||
let value = Struct1 { | ||
x: "a".to_owned(), | ||
y: Struct2 { z: "b".to_owned() }, | ||
}; | ||
|
||
let mut visitor = CollectValues::default(); | ||
value.visit_pointer( | ||
pointer::Pointer::new(&[pointer::Segment::Field("x")]), | ||
&mut visitor, | ||
); | ||
assert_eq!(visitor.0.len(), 1); | ||
assert_eq!(visitor.0[0], r#""a""#); | ||
|
||
let mut visitor = CollectValues::default(); | ||
value.visit_pointer( | ||
pointer::Pointer::new(&[pointer::Segment::Field("y")]), | ||
&mut visitor, | ||
); | ||
assert_eq!(visitor.0.len(), 1); | ||
assert_eq!(visitor.0[0], r#"Struct2 { z: "b" }"#); | ||
|
||
let mut visitor = CollectValues::default(); | ||
value.visit_pointer( | ||
pointer::Pointer::new(&[pointer::Segment::Field("y"), pointer::Segment::Field("z")]), | ||
&mut visitor, | ||
); | ||
assert_eq!(visitor.0.len(), 1); | ||
assert_eq!(visitor.0[0], r#""b""#); | ||
} | ||
|
||
#[cfg(feature = "derive")] | ||
#[test] | ||
fn visit_pointer_macro() { | ||
use valuable::visit_pointer; | ||
|
||
let value = Struct1 { | ||
x: "a".to_owned(), | ||
y: Struct2 { z: "b".to_owned() }, | ||
}; | ||
|
||
let mut visitor = CollectValues::default(); | ||
visit_pointer!(value.x, visitor); | ||
assert_eq!(visitor.0.len(), 1); | ||
assert_eq!(visitor.0[0], r#""a""#); | ||
|
||
let mut visitor = CollectValues::default(); | ||
visit_pointer!(value.y, visitor); | ||
assert_eq!(visitor.0.len(), 1); | ||
assert_eq!(visitor.0[0], r#"Struct2 { z: "b" }"#); | ||
|
||
let mut visitor = CollectValues::default(); | ||
visit_pointer!(value.y.z, visitor); | ||
assert_eq!(visitor.0.len(), 1); | ||
assert_eq!(visitor.0[0], r#""b""#); | ||
} |
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,94 @@ | ||
use std::collections::VecDeque; | ||
|
||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::parse::{Parse, ParseStream}; | ||
use syn::{Expr, Result, Token}; | ||
|
||
use crate::derive::respan; | ||
|
||
pub(crate) fn visit_pointer(input: TokenStream) -> Result<TokenStream> { | ||
let Input { | ||
expr, | ||
segments, | ||
visit, | ||
} = syn::parse2(input)?; | ||
|
||
let segments = segments.iter().map(|segment| match segment { | ||
Segment::Member(syn::Member::Named(ident)) => { | ||
let literal = ident.to_string(); | ||
quote! { | ||
::valuable::pointer::Segment::Field(#literal), | ||
} | ||
} | ||
Segment::Member(syn::Member::Unnamed(index)) => { | ||
quote! { | ||
::valuable::pointer::Segment::TupleIndex(#index), | ||
} | ||
} | ||
Segment::Index(expr) => { | ||
let expr = respan(quote! { &#expr }, expr); | ||
quote! { | ||
::valuable::pointer::Segment::Index( | ||
::valuable::Valuable::as_value(#expr) | ||
), | ||
} | ||
} | ||
}); | ||
|
||
let visit_pointer = respan(quote! { ::valuable::Valuable::visit_pointer }, &expr); | ||
Ok(quote! { | ||
#visit_pointer( | ||
&#expr, | ||
::valuable::pointer::Pointer::new(&[ | ||
#(#segments)* | ||
]), | ||
&mut #visit, | ||
) | ||
}) | ||
} | ||
|
||
struct Input { | ||
expr: Expr, | ||
segments: VecDeque<Segment>, | ||
visit: Expr, | ||
} | ||
|
||
enum Segment { | ||
Member(syn::Member), | ||
Index(Box<Expr>), | ||
} | ||
|
||
impl Parse for Input { | ||
fn parse(input: ParseStream<'_>) -> Result<Self> { | ||
let mut chain = input.parse()?; | ||
let _: Token![,] = input.parse()?; | ||
let visit = input.parse()?; | ||
let _: Option<Token![,]> = input.parse()?; | ||
|
||
let mut segments = VecDeque::new(); | ||
let expr; | ||
loop { | ||
match chain { | ||
Expr::Field(e) => { | ||
chain = *e.base; | ||
segments.push_front(Segment::Member(e.member)) | ||
} | ||
Expr::Index(e) => { | ||
chain = *e.expr; | ||
segments.push_front(Segment::Index(e.index)) | ||
} | ||
e => { | ||
expr = e; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
Ok(Self { | ||
expr, | ||
segments, | ||
visit, | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we maybe also consider having a
pointer!
macro that returns aPointer
without actually trying to visit it? the use-case i have in mind is storing a pointer in a struct so that it can be use to traverse multipleValuable
s.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on that, in my case I would like to create the pointer when parsing the templates where I don't have the data structures yet