https://github.com/bincode-org/virtue
A sinless derive helper
https://github.com/bincode-org/virtue
Last synced: about 1 year ago
JSON representation
A sinless derive helper
- Host: GitHub
- URL: https://github.com/bincode-org/virtue
- Owner: bincode-org
- License: mit
- Created: 2021-12-04T09:09:01.000Z (over 4 years ago)
- Default Branch: trunk
- Last Pushed: 2024-11-08T14:20:29.000Z (over 1 year ago)
- Last Synced: 2025-05-08T00:13:46.041Z (about 1 year ago)
- Language: Rust
- Size: 157 KB
- Stars: 72
- Watchers: 7
- Forks: 10
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Virtue, a sinless derive macro helper
## Goals
- Zero dependencies, so fast compile times
- No other dependencies needed
- Declarative code generation
- As much typesystem checking as possible
- Build for modern rust: 1.57 and up
- Build with popular crates in mind:
- [bincode](https://docs.rs/bincode)
- Will always respect semver. Minor releases will never have:
- Breaking API changes
- MSRV changes
## Example
```rust
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 {
// 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
```ignore
impl YourTrait for {
fn your_fn(&self) { // .generate_fn("your_fn").with_self_arg(FnSelfArg::RefSelf)
println!("Hello world"); // fn_body.push_parsed(...)
}
}
```