-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add derive macro to get ImplicitClone implemented quicker (#46)
I think it would be handy to have a derive macro for ImplicitClone so we don't have to write the entire impl all the time.
- Loading branch information
Showing
8 changed files
with
139 additions
and
0 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
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,26 @@ | ||
[package] | ||
name = "implicit-clone-derive" | ||
version = "0.1.0" | ||
authors = ["Cecile Tonglet <cecile.tonglet@cecton.com>"] | ||
license = "MIT OR Apache-2.0" | ||
edition = "2021" | ||
description = "Immutable types and ImplicitClone trait similar to Copy" | ||
repository = "https://github.com/yewstack/implicit-clone" | ||
homepage = "https://github.com/yewstack/implicit-clone" | ||
documentation = "https://docs.rs/implicit-clone" | ||
readme = "README.md" | ||
keywords = ["immutable", "cheap-clone", "copy", "rc"] | ||
categories = ["rust-patterns"] | ||
rust-version = "1.64" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
quote = "1" | ||
syn = { version = "2", features = ["full"] } | ||
|
||
[dev-dependencies] | ||
implicit-clone = { path = ".." } | ||
trybuild = "1" | ||
rustversion = "1" |
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,44 @@ | ||
use quote::quote; | ||
|
||
#[proc_macro_derive(ImplicitClone)] | ||
pub fn derive_implicit_clone(item: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
let syn::ItemStruct { | ||
ident, generics, .. | ||
} = syn::parse_macro_input!(item as syn::ItemStruct); | ||
let (_impl_generics, ty_generics, where_clause) = generics.split_for_impl(); | ||
let generics = generics | ||
.params | ||
.iter() | ||
.map(|param| match param { | ||
syn::GenericParam::Type(syn::TypeParam { | ||
attrs, | ||
ident, | ||
colon_token: _, | ||
bounds, | ||
eq_token, | ||
default, | ||
}) => { | ||
let bounds = bounds | ||
.iter() | ||
.map(|bound| quote! { #bound }) | ||
.chain(std::iter::once(quote! { ::implicit_clone::ImplicitClone })) | ||
.collect::<Vec<_>>(); | ||
quote! { | ||
#(#attrs)* #ident: #(#bounds)+* #eq_token #default | ||
} | ||
} | ||
_ => quote! { #param }, | ||
}) | ||
.collect::<Vec<_>>(); | ||
let generics = if generics.is_empty() { | ||
quote! {} | ||
} else { | ||
quote! { | ||
<#(#generics),*> | ||
} | ||
}; | ||
let res = quote! { | ||
impl #generics ::implicit_clone::ImplicitClone for #ident #ty_generics #where_clause {} | ||
}; | ||
res.into() | ||
} |
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,13 @@ | ||
#[allow(dead_code)] | ||
#[test] | ||
fn tests_pass() { | ||
let t = trybuild::TestCases::new(); | ||
t.pass("tests/function_component_attr/*-pass.rs"); | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[rustversion::attr(stable(1.64), test)] | ||
fn tests_fail() { | ||
let t = trybuild::TestCases::new(); | ||
t.compile_fail("tests/function_component_attr/*-fail.rs"); | ||
} |
9 changes: 9 additions & 0 deletions
9
implicit-clone-derive/tests/function_component_attr/derive-fail.rs
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,9 @@ | ||
use implicit_clone::ImplicitClone; | ||
|
||
#[derive(ImplicitClone)] | ||
pub struct NotClonableStruct; | ||
|
||
#[derive(ImplicitClone)] | ||
fn foo() {} | ||
|
||
fn main() {} |
24 changes: 24 additions & 0 deletions
24
implicit-clone-derive/tests/function_component_attr/derive-fail.stderr
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,24 @@ | ||
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s | ||
--> tests/function_component_attr/derive-fail.rs:6:1 | ||
| | ||
6 | #[derive(ImplicitClone)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ not applicable here | ||
7 | fn foo() {} | ||
| ----------- not a `struct`, `enum` or `union` | ||
|
||
error[E0277]: the trait bound `NotClonableStruct: Clone` is not satisfied | ||
--> tests/function_component_attr/derive-fail.rs:3:10 | ||
| | ||
3 | #[derive(ImplicitClone)] | ||
| ^^^^^^^^^^^^^ the trait `Clone` is not implemented for `NotClonableStruct` | ||
| | ||
note: required by a bound in `ImplicitClone` | ||
--> $WORKSPACE/src/lib.rs | ||
| | ||
| pub trait ImplicitClone: Clone { | ||
| ^^^^^ required by this bound in `ImplicitClone` | ||
= note: this error originates in the derive macro `ImplicitClone` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
help: consider annotating `NotClonableStruct` with `#[derive(Clone)]` | ||
| | ||
4 | #[derive(Clone)] | ||
| |
14 changes: 14 additions & 0 deletions
14
implicit-clone-derive/tests/function_component_attr/derive-pass.rs
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,14 @@ | ||
use implicit_clone::ImplicitClone; | ||
|
||
#[derive(Clone, ImplicitClone)] | ||
struct ExampleStruct; | ||
|
||
#[derive(Clone, ImplicitClone)] | ||
struct StructWithGenerics<T>(T); | ||
|
||
#[derive(Clone, ImplicitClone)] | ||
struct StructWithGenericsWithBounds<T: PartialEq>(T); | ||
|
||
fn main() { | ||
let _ = ImplicitClone::implicit_clone(&ExampleStruct); | ||
} |
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