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
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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""#);
}
26 changes: 22 additions & 4 deletions tests/tests/ui/not_valuable.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0277]: the trait bound `S: Valuable` is not satisfied
--> $DIR/tests/ui/not_valuable.rs:7:8
--> tests/ui/not_valuable.rs:7:8
|
5 | #[derive(Valuable)]
| -------- required by a bound introduced by this call
Expand All @@ -10,7 +10,16 @@ error[E0277]: the trait bound `S: Valuable` is not satisfied
= note: required because of the requirements on the impl of `Valuable` for `Option<S>`

error[E0277]: the trait bound `S: Valuable` is not satisfied
--> $DIR/tests/ui/not_valuable.rs:11:14
--> tests/ui/not_valuable.rs:5:10
|
5 | #[derive(Valuable)]
| ^^^^^^^^ the trait `Valuable` is not implemented for `S`
|
= note: required because of the requirements on the impl of `Valuable` for `Option<S>`
= note: this error originates in the derive macro `Valuable` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `S: Valuable` is not satisfied
--> tests/ui/not_valuable.rs:11:14
|
10 | #[derive(Valuable)]
| -------- required by a bound introduced by this call
Expand All @@ -20,7 +29,16 @@ error[E0277]: the trait bound `S: Valuable` is not satisfied
= note: required because of the requirements on the impl of `Valuable` for `Option<S>`

error[E0277]: the trait bound `S: Valuable` is not satisfied
--> $DIR/tests/ui/not_valuable.rs:15:17
--> tests/ui/not_valuable.rs:10:10
|
10 | #[derive(Valuable)]
| ^^^^^^^^ the trait `Valuable` is not implemented for `S`
|
= note: required because of the requirements on the impl of `Valuable` for `Option<S>`
= note: this error originates in the derive macro `Valuable` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `S: Valuable` is not satisfied
--> tests/ui/not_valuable.rs:15:17
|
13 | #[derive(Valuable)]
| -------- required by a bound introduced by this call
Expand All @@ -33,7 +51,7 @@ error[E0277]: the trait bound `S: Valuable` is not satisfied
= note: required because of the requirements on the impl of `Valuable` for `&Option<S>`

error[E0277]: the trait bound `S: Valuable` is not satisfied
--> $DIR/tests/ui/not_valuable.rs:16:11
--> tests/ui/not_valuable.rs:16:11
|
13 | #[derive(Valuable)]
| -------- required by a bound introduced by this call
Expand Down
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"
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"] }
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,12 +360,13 @@ 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)]
}
}

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()
}
Loading