Crates.io | virtue |
lib.rs | virtue |
version | |
source | src |
created_at | 2021-12-04 11:41:31.341048 |
updated_at | 2024-11-08 14:20:44.51073 |
description | A sinless derive macro helper |
homepage | |
repository | https://github.com/bincode-org/virtue |
max_upload_size | |
id | 492222 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
use virtue::prelude::*;
#[proc_macro_derive(YourDerive, attributes(some, attributes, go, here))]
pub fn derive_your_derive(input: TokenStream) -> TokenStream {
derive_your_derive_inner(input)
.unwrap_or_else(|error| error.into_token_stream())
}
fn derive_your_derive_inner(input: TokenStream) -> Result<TokenStream> {
// Parse the struct or enum you want to implement a derive for
let parse = Parse::new(input)?;
// Get a reference to the generator
let (mut generator, body) = parse.into_generator();
match body {
Body::Struct(body) => {
// Implement your struct body here
// See `Generator` for more information
generator.impl_for("YourTrait")?
.generate_fn("your_fn")
.with_self_arg(FnSelfArg::RefSelf)
.body(|fn_body| {
fn_body.push_parsed("println!(\"Hello world\");");
})?;
},
Body::Enum(body) => {
// Implement your enum body here
// See `Generator` for more information
generator.impl_for("YourTrait")?
.generate_fn("your_fn")
.with_self_arg(FnSelfArg::RefSelf)
.body(|fn_body| {
fn_body.push_parsed("println!(\"Hello world\");");
})?;
},
}
generator.finish()
}
Will generate
impl YourTrait for <Struct or Enum> {
fn your_fn(&self) { // .generate_fn("your_fn").with_self_arg(FnSelfArg::RefSelf)
println!("Hello world"); // fn_body.push_parsed(...)
}
}