diff --git a/Cargo.lock b/Cargo.lock index f940c4984..dae8a8276 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -730,11 +730,8 @@ dependencies = [ name = "cairo-lang-macro" version = "0.0.1" dependencies = [ - "anyhow", "cairo-lang-macro-attributes", "cairo-lang-macro-stable", - "serde", - "serde_json", ] [[package]] diff --git a/plugins/cairo-lang-macro-stable/src/lib.rs b/plugins/cairo-lang-macro-stable/src/lib.rs index fcf853b96..58c5f4d2f 100644 --- a/plugins/cairo-lang-macro-stable/src/lib.rs +++ b/plugins/cairo-lang-macro-stable/src/lib.rs @@ -16,7 +16,7 @@ pub struct StableTokenStream(*mut c_char); #[derive(Debug)] pub enum StableAuxData { None, - Some(*mut c_char), + Some(StableSlice), } /// Diagnostic returned by the procedural macro. diff --git a/plugins/cairo-lang-macro/Cargo.toml b/plugins/cairo-lang-macro/Cargo.toml index 7ca51d792..73a14dac0 100644 --- a/plugins/cairo-lang-macro/Cargo.toml +++ b/plugins/cairo-lang-macro/Cargo.toml @@ -13,8 +13,5 @@ readme = "README.md" repository.workspace = true [dependencies] -anyhow.workspace = true cairo-lang-macro-attributes = { path = "../cairo-lang-macro-attributes" } cairo-lang-macro-stable = { path = "../cairo-lang-macro-stable" } -serde.workspace = true -serde_json.workspace = true diff --git a/plugins/cairo-lang-macro/src/lib.rs b/plugins/cairo-lang-macro/src/lib.rs index acd3d27e0..fbee1b56a 100644 --- a/plugins/cairo-lang-macro/src/lib.rs +++ b/plugins/cairo-lang-macro/src/lib.rs @@ -56,21 +56,23 @@ impl Display for TokenStream { /// Auxiliary data returned by procedural macro. #[derive(Debug)] -pub struct AuxData(String); +pub struct AuxData(Vec); impl AuxData { - pub fn new(s: String) -> Self { - Self(s) + pub fn new(data: Vec) -> Self { + Self(data) } +} - pub fn try_new(value: T) -> Result { - Ok(Self(serde_json::to_string(&value)?)) +impl From<&[u8]> for AuxData { + fn from(bytes: &[u8]) -> Self { + Self(bytes.to_vec()) } } -impl Display for AuxData { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) +impl From for Vec { + fn from(aux_data: AuxData) -> Vec { + aux_data.0 } } @@ -346,8 +348,8 @@ impl AuxData { /// # Safety #[doc(hidden)] pub fn into_stable(self) -> StableAuxData { - let cstr = CString::new(self.0.to_string()).unwrap(); - StableAuxData::Some(cstr.into_raw()) + let value: Vec = self.into(); + StableAuxData::Some(StableSlice::new(value)) } /// Convert to native Rust representation, without taking the ownership of the string. @@ -359,7 +361,11 @@ impl AuxData { pub unsafe fn from_stable(aux_data: &StableAuxData) -> Option { match aux_data { StableAuxData::None => None, - StableAuxData::Some(raw) => Some(Self::new(from_raw_cstr(*raw))), + StableAuxData::Some(raw) => { + let (ptr, n) = raw.raw_parts(); + let value = slice::from_raw_parts(ptr, n); + Some(value.into()) + } } } @@ -373,7 +379,7 @@ impl AuxData { pub unsafe fn from_owned_stable(aux_data: StableAuxData) -> Option { match aux_data { StableAuxData::None => None, - StableAuxData::Some(raw) => Some(Self::new(from_raw_cstring(raw))), + StableAuxData::Some(raw) => Some(Self::new(raw.into_owned())), } } } diff --git a/scarb/src/compiler/plugin/proc_macro/host.rs b/scarb/src/compiler/plugin/proc_macro/host.rs index 6e29f8615..38ccfdc3e 100644 --- a/scarb/src/compiler/plugin/proc_macro/host.rs +++ b/scarb/src/compiler/plugin/proc_macro/host.rs @@ -49,13 +49,13 @@ pub struct ProcMacroInput { #[derive(Clone, Debug)] pub struct ProcMacroAuxData { - value: String, + value: Vec, macro_id: ProcMacroId, macro_package_id: PackageId, } impl ProcMacroAuxData { - pub fn new(value: String, macro_id: ProcMacroId, macro_package_id: PackageId) -> Self { + pub fn new(value: Vec, macro_id: ProcMacroId, macro_package_id: PackageId) -> Self { Self { value, macro_id, @@ -210,7 +210,7 @@ impl MacroPlugin for ProcMacroHostPlugin { token_stream = new_token_stream; if let Some(new_aux_data) = new_aux_data { aux_data = Some(ProcMacroAuxData::new( - new_aux_data.to_string(), + new_aux_data.into(), input.id, input.macro_package_id, )); diff --git a/scarb/tests/build_cairo_plugin.rs b/scarb/tests/build_cairo_plugin.rs index ca6f2c214..b825cacf3 100644 --- a/scarb/tests/build_cairo_plugin.rs +++ b/scarb/tests/build_cairo_plugin.rs @@ -91,7 +91,7 @@ fn simple_project_with_code(t: &impl PathChild, code: impl ToString) { fn simple_project(t: &impl PathChild) { let code = indoc! {r#" - use cairo_lang_macro::{ProcMacroResult, TokenStream, attribute_macro}; + use cairo_lang_macro::{ProcMacroResult, TokenStream, attribute_macro, AuxData}; #[attribute_macro] pub fn some_macro(token_stream: TokenStream) -> ProcMacroResult {