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 @@ -22,7 +22,7 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0.60"
quote = "1.0"
syn = { version = "2.0", features = ["extra-traits"] }
syn = { version = "2.0", features = ["full", "extra-traits"] }

[dev-dependencies]
valuable = { path = "../valuable", features = ["derive"] }
84 changes: 68 additions & 16 deletions valuable-derive/src/expand.rs → valuable-derive/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fn derive_struct(input: &syn::DeriveInput, data: &syn::DataStruct) -> TokenStrea
let name = &input.ident;
let name_literal = name.to_string();
let visit_fields;
let visit_pointer;
let struct_def;
let mut named_fields_statics = None;

Expand All @@ -33,21 +34,39 @@ fn derive_struct(input: &syn::DeriveInput, data: &syn::DataStruct) -> TokenStrea
)
};

let fields = data.fields.iter().map(|field| {
let f = field.ident.as_ref();
let tokens = quote! {
&self.#f
};
respan(tokens, &field.ty)
});
let fields: Vec<_> = data
.fields
.iter()
.map(|field| {
let f = field.ident.as_ref();
let tokens = quote! {
&self.#f
};
respan(tokens, &field.ty)
})
.collect();
visit_fields = quote! {
visitor.visit_named_fields(&::valuable::NamedValues::new(
#named_fields_static_name,
&[
#(::valuable::Valuable::as_value(#fields),)*
],
));
}
};
let field_name_literals = data
.fields
.iter()
.map(|field| field.ident.as_ref().unwrap().to_string());
visit_pointer = quote! {
if let ::valuable::pointer::Segment::Field(f) = pointer.path()[0] {
match f {
#(#field_name_literals => {
::valuable::Valuable::visit_pointer(#fields, pointer.step(), visit)
})*
_ => {}
}
}
};
}
syn::Fields::Unnamed(_) | syn::Fields::Unit => {
let len = data.fields.len();
Expand All @@ -58,20 +77,40 @@ fn derive_struct(input: &syn::DeriveInput, data: &syn::DataStruct) -> TokenStrea
)
};

let indices = data.fields.iter().enumerate().map(|(i, field)| {
let index = syn::Index::from(i);
let tokens = quote! {
&self.#index
};
respan(tokens, &field.ty)
});
let indices: Vec<_> = data
.fields
.iter()
.enumerate()
.map(|(i, field)| {
let index = syn::Index::from(i);
let tokens = quote! {
&self.#index
};
respan(tokens, &field.ty)
})
.collect();
visit_fields = quote! {
visitor.visit_unnamed_fields(
&[
#(::valuable::Valuable::as_value(#indices),)*
],
);
};
let field_indices = data
.fields
.iter()
.enumerate()
.map(|(i, _)| syn::Index::from(i));
visit_pointer = quote! {
if let ::valuable::pointer::Segment::TupleIndex(i) = pointer.path()[0] {
match i {
#(#field_indices => {
::valuable::Valuable::visit_pointer(#indices, pointer.step(), visit)
})*
_ => {}
}
}
};
}
}

Expand All @@ -94,6 +133,18 @@ fn derive_struct(input: &syn::DeriveInput, data: &syn::DataStruct) -> TokenStrea
fn visit(&self, visitor: &mut dyn ::valuable::Visit) {
#visit_fields
}

fn visit_pointer(
&self,
pointer: ::valuable::pointer::Pointer<'_>,
visit: &mut dyn ::valuable::Visit,
) {
if pointer.path().is_empty() {
visit.visit_value(::valuable::Valuable::as_value(self));
return;
}
#visit_pointer
}
}
};

Expand Down Expand Up @@ -309,13 +360,14 @@ fn named_fields_static(name: &Ident, fields: &syn::Fields) -> TokenStream {
fn allowed_lints() -> TokenStream {
quote! {
#[allow(non_upper_case_globals)]
#[allow(clippy::collapsible_match)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::used_underscore_binding)]
#[allow(clippy::indexing_slicing)]
}
}

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

pub mod pointer;

mod slice;
pub use slice::Slice;

Expand All @@ -154,4 +156,4 @@ mod visit;
pub use visit::{visit, Visit};

#[cfg(feature = "derive")]
pub use valuable_derive::Valuable;
pub use valuable_derive::{visit_pointer, Valuable};
Loading