Skip to content

Commit

Permalink
trait Serialize uses reference to self instead of move.
Browse files Browse the repository at this point in the history
  • Loading branch information
Grinkers committed Feb 14, 2024
1 parent f968e9d commit 9fcb8aa
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 19 deletions.
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, }"
);
}

0 comments on commit 9fcb8aa

Please sign in to comment.