Skip to content

Commit

Permalink
fix: remove dependency on derivative (#294)
Browse files Browse the repository at this point in the history
* add derive-where

* gen Default for non unit enum

* remove derive_where and derivative

* use absolute path of std::default::Default
  • Loading branch information
flaneur2020 authored Jan 3, 2025
1 parent 47c136c commit 946bde4
Show file tree
Hide file tree
Showing 39 changed files with 961 additions and 520 deletions.
14 changes: 0 additions & 14 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ faststr = "0.2"
[dev-dependencies]

tokio = { version = "1", features = ["io-util"] }
derivative = "2"
tempfile = "3"
diffy = "0.4"
criterion = { version = "0.5", features = ["html_reports"] }
Expand Down
1 change: 0 additions & 1 deletion pilota-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ faststr = "0.2"
[dev-dependencies]
pilota = { path = "../pilota" }
tokio = { version = "1", features = ["io-util"] }
derivative = "2"
tempfile = "3"
diffy = "0.4"
criterion = { version = "0.5", features = ["html_reports"] }
Expand Down
48 changes: 36 additions & 12 deletions pilota-build/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,18 +391,42 @@ impl Plugin for ImplDefaultPlugin {
cx.with_adjust_mut(def_id, |adj| adj.add_attrs(&["#[derive(Default)]".into()]))
}
Item::Enum(e) => {
if !e.variants.is_empty() {
cx.with_adjust_mut(def_id, |adj| {
adj.add_attrs(&[
"#[derive(::pilota::derivative::Derivative)]".into(),
"#[derivative(Default)]".into(),
]);
});

if let Some(v) = e.variants.first() {
cx.with_adjust_mut(v.did, |adj| {
adj.add_attrs(&["#[derivative(Default)]".into()]);
})
if let Some(first_variant) = e.variants.first() {
let is_unit_variant = first_variant.fields.is_empty();
if is_unit_variant {
cx.with_adjust_mut(def_id, |adj| {
adj.add_attrs(&["#[derive(Default)]".into()]);
});

if let Some(v) = e.variants.first() {
cx.with_adjust_mut(v.did, |adj| {
adj.add_attrs(&["#[default]".into()]);
})
}
} else {
// for non unit variant, we need to impl Default for the enum
let enum_name = cx.rust_name(def_id);
let variant_name = cx.rust_name(first_variant.did);
let fields = first_variant
.fields
.iter()
.map(|_| "::std::default::Default::default()".to_string())
.join(",\n");

cx.with_adjust_mut(def_id, |adj| {
adj.add_nested_item(
format!(
r#"
impl ::std::default::Default for {enum_name} {{
fn default() -> Self {{
{enum_name}::{variant_name} ({fields})
}}
}}
"#
)
.into(),
)
});
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions pilota-build/test_data/plugin/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,16 @@ pub mod serde {
+ __protocol.struct_end_len()
}
}
#[derive(PartialOrd, Hash, Eq, Ord, Debug, ::pilota::derivative::Derivative)]
#[derivative(Default)]
#[derive(::pilota::serde::Serialize, ::pilota::serde::Deserialize)]
#[derive(
PartialOrd,
Hash,
Eq,
Ord,
Debug,
Default,
::pilota::serde::Serialize,
::pilota::serde::Deserialize,
)]
#[serde(untagged)]
#[serde(transparent)]
#[derive(Clone, PartialEq, Copy)]
Expand Down
4 changes: 1 addition & 3 deletions pilota-build/test_data/protobuf/nested_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ pub mod nested_message {
}

pub mod tt1 {
#[derive(PartialOrd, Hash, Eq, Ord, Debug, ::pilota::derivative::Derivative)]
#[derivative(Default)]
#[derive(Clone, PartialEq, Copy)]
#[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq, Copy)]
#[repr(transparent)]
pub struct Label(i32);

Expand Down
21 changes: 13 additions & 8 deletions pilota-build/test_data/protobuf/oneof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,14 @@ pub mod oneof {
}

pub mod test {
#[derive(PartialOrd, Hash, Eq, Ord, Debug, ::pilota::derivative::Derivative)]
#[derivative(Default)]
#[derive(Clone, PartialEq)]

impl ::std::default::Default for Test {
fn default() -> Self {
Test::A(::std::default::Default::default())
}
}
#[derive(PartialOrd, Hash, Eq, Ord, Debug, Clone, PartialEq)]
pub enum Test {
#[derivative(Default)]
A(::pilota::FastStr),

B(i32),
Expand Down Expand Up @@ -168,11 +171,13 @@ pub mod oneof {
::core::result::Result::Ok(())
}
}
#[derive(PartialOrd, Hash, Eq, Ord, Debug, ::pilota::derivative::Derivative)]
#[derivative(Default)]
#[derive(Clone, PartialEq)]
impl ::std::default::Default for Type {
fn default() -> Self {
Type::S(::std::default::Default::default())
}
}
#[derive(PartialOrd, Hash, Eq, Ord, Debug, Clone, PartialEq)]
pub enum Type {
#[derivative(Default)]
S(::pilota::FastStr),

I(i32),
Expand Down
Loading

0 comments on commit 946bde4

Please sign in to comment.