-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
- Loading branch information
Showing
8 changed files
with
124 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 <[email protected]>"] | ||
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,10 @@ | ||
use quote::quote; | ||
|
||
#[proc_macro_derive(ImplicitClone)] | ||
pub fn derive_implicit_clone(item: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
let syn::ItemStruct { ident, .. } = syn::parse_macro_input!(item as syn::ItemStruct); | ||
let res = quote! { | ||
impl ::implicit_clone::ImplicitClone for #ident {} | ||
}; | ||
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"); | ||
} |
6 changes: 6 additions & 0 deletions
6
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,6 @@ | ||
use implicit_clone::ImplicitClone; | ||
|
||
#[derive(ImplicitClone)] | ||
pub struct NotClonableStruct; | ||
|
||
fn main() {} |
16 changes: 16 additions & 0 deletions
16
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,16 @@ | ||
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)] | ||
| |
44 changes: 44 additions & 0 deletions
44
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,44 @@ | ||
#![no_implicit_prelude] | ||
|
||
// Shadow primitives | ||
#[allow(non_camel_case_types)] | ||
pub struct bool; | ||
#[allow(non_camel_case_types)] | ||
pub struct char; | ||
#[allow(non_camel_case_types)] | ||
pub struct f32; | ||
#[allow(non_camel_case_types)] | ||
pub struct f64; | ||
#[allow(non_camel_case_types)] | ||
pub struct i128; | ||
#[allow(non_camel_case_types)] | ||
pub struct i16; | ||
#[allow(non_camel_case_types)] | ||
pub struct i32; | ||
#[allow(non_camel_case_types)] | ||
pub struct i64; | ||
#[allow(non_camel_case_types)] | ||
pub struct i8; | ||
#[allow(non_camel_case_types)] | ||
pub struct isize; | ||
#[allow(non_camel_case_types)] | ||
pub struct str; | ||
#[allow(non_camel_case_types)] | ||
pub struct u128; | ||
#[allow(non_camel_case_types)] | ||
pub struct u16; | ||
#[allow(non_camel_case_types)] | ||
pub struct u32; | ||
#[allow(non_camel_case_types)] | ||
pub struct u64; | ||
#[allow(non_camel_case_types)] | ||
pub struct u8; | ||
#[allow(non_camel_case_types)] | ||
pub struct usize; | ||
|
||
#[derive(Clone, ::implicit_clone::ImplicitClone)] | ||
struct ExampleStruct; | ||
|
||
fn main() { | ||
let _ = ::implicit_clone::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