musli_zerocopy_macros/
lib.rs

1//! [<img alt="github" src="https://img.shields.io/badge/github-udoprog/musli-8da0cb?style=for-the-badge&logo=github" height="20">](https://github.com/udoprog/musli)
2//! [<img alt="crates.io" src="https://img.shields.io/crates/v/musli-zerocopy-macros.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/musli-zerocopy-macros)
3//! [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-musli--zerocopy--macros-66c2a5?style=for-the-badge&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20">](https://docs.rs/musli-zerocopy-macros)
4//!
5//! This crate provides the macros used in [Müsli zero-copy].
6//!
7//! Please refer to <https://docs.rs/musli> for documentation.
8//!
9//! [Müsli zero-copy]: <https://docs.rs/musli-zerocopy>
10
11#![allow(clippy::too_many_arguments)]
12#![allow(clippy::needless_late_init)]
13
14use proc_macro::TokenStream;
15
16#[cfg(feature = "sneaky-fields")]
17mod sneaky_fields;
18mod visit;
19mod zero_copy;
20
21#[proc_macro_derive(ZeroCopy, attributes(zero_copy))]
22pub fn zero_copy(input: TokenStream) -> TokenStream {
23    let input = syn::parse_macro_input!(input as syn::DeriveInput);
24    let expander = zero_copy::Expander::new(input);
25
26    match expander.expand() {
27        Ok(stream) => stream.into(),
28        Err(errors) => to_compile_errors(errors).into(),
29    }
30}
31
32#[proc_macro_derive(Visit, attributes(visit))]
33pub fn visit(input: TokenStream) -> TokenStream {
34    let input = syn::parse_macro_input!(input as syn::DeriveInput);
35    let expander = visit::Expander::new(&input);
36
37    match expander.expand() {
38        Ok(stream) => stream.into(),
39        Err(errors) => to_compile_errors(errors).into(),
40    }
41}
42
43// NB: Only used in UI tests.
44#[proc_macro_attribute]
45#[doc(hidden)]
46#[cfg(feature = "sneaky-fields")]
47pub fn sneaky_fields(attr: TokenStream, item: TokenStream) -> TokenStream {
48    sneaky_fields::expand(attr, item)
49}
50
51fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
52    let mut output = proc_macro2::TokenStream::new();
53
54    for e in errors {
55        output.extend(e.to_compile_error());
56    }
57
58    output
59}