Skip to content

Commit

Permalink
Store AuxData as Vec<u8> instead of serde/string (#1188)
Browse files Browse the repository at this point in the history
commit-id:eacd6dd6

---

**Stack**:
- #1195
- #1194
- #1192
- #1186
- #1188⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do
not merge manually using the UI - doing so may have unexpected results.*
  • Loading branch information
maciektr authored Mar 15, 2024
1 parent fd27eac commit 1b4f7aa
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/cairo-lang-macro-stable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct StableTokenStream(*mut c_char);
#[derive(Debug)]
pub enum StableAuxData {
None,
Some(*mut c_char),
Some(StableSlice<u8>),
}

/// Diagnostic returned by the procedural macro.
Expand Down
3 changes: 0 additions & 3 deletions plugins/cairo-lang-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 18 additions & 12 deletions plugins/cairo-lang-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,23 @@ impl Display for TokenStream {

/// Auxiliary data returned by procedural macro.
#[derive(Debug)]
pub struct AuxData(String);
pub struct AuxData(Vec<u8>);

impl AuxData {
pub fn new(s: String) -> Self {
Self(s)
pub fn new(data: Vec<u8>) -> Self {
Self(data)
}
}

pub fn try_new<T: serde::Serialize>(value: T) -> Result<Self, serde_json::Error> {
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<AuxData> for Vec<u8> {
fn from(aux_data: AuxData) -> Vec<u8> {
aux_data.0
}
}

Expand Down Expand Up @@ -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<u8> = self.into();
StableAuxData::Some(StableSlice::new(value))
}

/// Convert to native Rust representation, without taking the ownership of the string.
Expand All @@ -359,7 +361,11 @@ impl AuxData {
pub unsafe fn from_stable(aux_data: &StableAuxData) -> Option<Self> {
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())
}
}
}

Expand All @@ -373,7 +379,7 @@ impl AuxData {
pub unsafe fn from_owned_stable(aux_data: StableAuxData) -> Option<Self> {
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())),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions scarb/src/compiler/plugin/proc_macro/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ pub struct ProcMacroInput {

#[derive(Clone, Debug)]
pub struct ProcMacroAuxData {
value: String,
value: Vec<u8>,
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<u8>, macro_id: ProcMacroId, macro_package_id: PackageId) -> Self {
Self {
value,
macro_id,
Expand Down Expand Up @@ -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,
));
Expand Down
2 changes: 1 addition & 1 deletion scarb/tests/build_cairo_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1b4f7aa

Please sign in to comment.