pub struct AuxData(/* private fields */);Expand description
Auxiliary data returned by procedural macro code generation.
This struct can be used to collect additional information from the Cairo source code of compiled project. For instance, you can create a procedural macro that collects some information stored by the Cairo programmer as attributes in the project source code.
The auxiliary data struct stores Vec<u8> leaving the serialization and deserialization
of the data as user responsibility. No assumptions regarding the serialization algorithm
are made.
For instance, auxiliary data can be serialized as JSON.
use cairo_lang_macro::{AuxData, ProcMacroResult, TokenStream, TokenTree, Token, TextSpan, attribute_macro, post_process, PostProcessContext};
use serde::{Serialize, Deserialize};
#[derive(Debug, Serialize, Deserialize)]
struct SomeAuxDataFormat {
some_message: String
}
#[attribute_macro]
pub fn some_macro(_attr: TokenStream, token_stream: TokenStream) -> ProcMacroResult {
// Remove macro call to avoid infinite loop.
let code = token_stream.to_string().replace("#[some]", "");
let token_stream = TokenStream::new(vec![
TokenTree::Ident(
Token::new(
&code,
TextSpan::new(0, code.len() as u32)
)
)
]);
let value = SomeAuxDataFormat { some_message: "Hello from some macro!".to_string() };
let value = serde_json::to_string(&value).unwrap();
let value: Vec<u8> = value.into_bytes();
let aux_data = AuxData::new(value);
ProcMacroResult::new(token_stream).with_aux_data(aux_data)
}
#[post_process]
pub fn callback(context: PostProcessContext) {
let aux_data = context.aux_data.into_iter()
.map(|aux_data| {
let value: Vec<u8> = aux_data.into();
let aux_data: SomeAuxDataFormat = serde_json::from_slice(&value).unwrap();
aux_data
})
.collect::<Vec<_>>();
println!("{:?}", aux_data);
}All auxiliary data emitted during compilation can be consumed
in the post_process implementation.
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl Freeze for AuxData
impl RefUnwindSafe for AuxData
impl Send for AuxData
impl Sync for AuxData
impl Unpin for AuxData
impl UnwindSafe for AuxData
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more