Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trait Serialize uses reference to self instead of move. #37

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .rustfmt.toml
Empty file.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ name = "tests"
path = "tests/progress.rs"

[dependencies]
syn = "1.0"
syn = "2.0"
quote = "1.0"
proc-macro2 = "1.0"
edn-rs = { git = "https://github.com/edn-rs/edn-rs.git" } # TODO lock in version once stable

[dev-dependencies]
trybuild = { version = "1.0", features = ["diff"] }
criterion = "0.3"
criterion = "0.5"

[dev-dependencies.cargo-husky]
version = "1"
Expand Down
8 changes: 4 additions & 4 deletions src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn expand_named_struct(struct_name: &Ident, fields: &Punctuated<Field, Comma>) -

quote! {
impl edn_rs::Serialize for #struct_name {
fn serialize(self) -> std::string::String {
fn serialize(&self) -> std::string::String {
let mut s = std::string::String::new();
s.push_str("{ ");
#(s.push_str(&#it);)*
Expand All @@ -58,7 +58,7 @@ fn expand_unnamed_struct(struct_name: &Ident, fields: &Punctuated<Field, Comma>)

quote! {
impl edn_rs::Serialize for #struct_name {
fn serialize(self) -> std::string::String {
fn serialize(&self) -> std::string::String {
let mut s = std::string::String::from("{ ");
#(s.push_str(&#it);)*
s.push_str("}");
Expand All @@ -71,7 +71,7 @@ fn expand_unnamed_struct(struct_name: &Ident, fields: &Punctuated<Field, Comma>)
fn expand_unit_struct(struct_name: &Ident) -> TokenStream2 {
quote! {
impl edn_rs::Serialize for #struct_name {
fn serialize(self) -> std::string::String {
fn serialize(&self) -> std::string::String {
String::from("nil")
}
}
Expand All @@ -94,7 +94,7 @@ fn expand_enum(enum_name: &Ident, data_enum: &DataEnum) -> TokenStream2 {

quote! {
impl edn_rs::Serialize for #enum_name {
fn serialize(self) -> std::string::String {
fn serialize(&self) -> std::string::String {
match self {
#(#it)*
}
Expand Down
2 changes: 1 addition & 1 deletion tests/both_ways.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct Point {
fn main() -> Result<(), EdnError> {
let point = Point { x: 1, y: 2 };

let serialized = edn_rs::to_string(point.clone());
let serialized = edn_rs::to_string(&point);
let deserialized: Point = edn_rs::from_str(&serialized)?;

assert_eq!(point, deserialized);
Expand Down
2 changes: 1 addition & 1 deletion tests/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() -> Result<(), EdnError> {
let account_edn_str =
"{ :crux.db/id \"123\", :account/amount 42, :account-type :account-type/premium-plus, }";

assert_eq!(edn_rs::to_string(account), account_edn_str);
assert_eq!(edn_rs::to_string(&account), account_edn_str);

let account: Account = edn_rs::from_str(account_edn_str)?;

Expand Down
2 changes: 1 addition & 1 deletion tests/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {
kind: Kind::Chill,
};
assert_eq!(
edn_rs::to_string(person),
edn_rs::to_string(&person),
"{ :name \"joana\", :age 290000, :kind :kind/chill, }"
);
}
16 changes: 6 additions & 10 deletions tests/unit_struct.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use edn_derive::{Serialize, Deserialize};
use edn_derive::{Deserialize, Serialize};
use edn_rs::EdnError;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
Expand All @@ -7,23 +7,19 @@ struct Nothing;
fn main() -> Result<(), EdnError> {
let nothing = Nothing;

assert_eq!(
edn_rs::to_string(nothing),
"nil"
);
assert_eq!(edn_rs::to_string(&nothing), "nil");

let nothing: Nothing = edn_rs::from_str("nil")?;

assert_eq!(
nothing,
Nothing
);
assert_eq!(nothing, Nothing);

let nothing_err: Result<Nothing, EdnError> = edn_rs::from_str(":a-key");

assert_eq!(
nothing_err,
Err(EdnError::Deserialize("couldn't convert :a-key into an unit struct".to_string()))
Err(EdnError::Deserialize(
"couldn't convert :a-key into an unit struct".to_string()
))
);

Ok(())
Expand Down
3 changes: 1 addition & 2 deletions tests/unnamed_struct_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ fn main() {
let person = Person("joana".to_string(), 290000, Kind::Chill);

assert_eq!(
edn_rs::to_string(person),
edn_rs::to_string(&person),
"{ 0 \"joana\", 1 290000, 2 :kind/chill, }"
);
}

Loading