AuxData

Struct AuxData 

Source
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§

Source§

impl AuxData

Source

pub fn new(data: Vec<u8>) -> Self

Create new AuxData struct from serialized data.

Trait Implementations§

Source§

impl Clone for AuxData

Source§

fn clone(&self) -> AuxData

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AuxData

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&[u8]> for AuxData

Source§

fn from(bytes: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<AuxData> for Vec<u8>

Source§

fn from(aux_data: AuxData) -> Vec<u8>

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.