Skip to content
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
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
79 changes: 79 additions & 0 deletions tests/tests/pointer.rs
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""#);
}
2 changes: 1 addition & 1 deletion valuable-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0.60", features = ["extra-traits"] }
syn = { version = "1.0.60", features = ["full", "extra-traits"] }

[dev-dependencies]
valuable = { path = "../valuable", features = ["derive"] }
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ fn allowed_lints() -> TokenStream {
}
}

fn respan(tokens: TokenStream, span: &impl ToTokens) -> TokenStream {
pub(crate) fn respan(tokens: TokenStream, span: &impl ToTokens) -> TokenStream {
let mut iter = span.to_token_stream().into_iter();
// `Span` on stable Rust has a limitation that only points to the first
// token, not the whole tokens. We can work around this limitation by
Expand Down
16 changes: 12 additions & 4 deletions valuable-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
extern crate proc_macro;
#![warn(rust_2018_idioms, unreachable_pub)]

mod expand;
mod derive;
mod pointer;

use proc_macro::TokenStream;
use syn::parse_macro_input;
use syn::{parse_macro_input, Error};

/// Derive a `Valuable` implementation for a struct or enum.
///
Expand All @@ -26,5 +27,12 @@ use syn::parse_macro_input;
#[proc_macro_derive(Valuable, attributes(valuable))]
pub fn derive_valuable(input: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(input as syn::DeriveInput);
expand::derive_valuable(&mut input).into()
derive::derive_valuable(&mut input).into()
}

#[proc_macro]
pub fn visit_pointer(input: TokenStream) -> TokenStream {
pointer::visit_pointer(input.into())
.unwrap_or_else(Error::into_compile_error)
.into()
}
94 changes: 94 additions & 0 deletions valuable-derive/src/pointer.rs
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,
)
Comment on lines +41 to +47
Copy link
Member

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 a Pointer 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 multiple Valuables.

Copy link

@Keats Keats Mar 9, 2022

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

})
}

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,
})
}
}
2 changes: 2 additions & 0 deletions valuable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ pub use mappable::Mappable;
mod named_values;
pub use named_values::NamedValues;

pub mod pointer;

mod slice;
pub use slice::Slice;

Expand Down
Loading