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 e580f12
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 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
4 changes: 2 additions & 2 deletions tests/both_ways.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use edn_derive::{Deserialize, Serialize};
use edn_rs::EdnError;
use edn_rs::{EdnError, Serialize};

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
struct Point {
Expand All @@ -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 = point.serialize();
let deserialized: Point = edn_rs::from_str(&serialized)?;

assert_eq!(point, deserialized);
Expand Down
4 changes: 2 additions & 2 deletions tests/complex.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use edn_derive::{Deserialize, Serialize};
use edn_rs::EdnError;
use edn_rs::{EdnError, Serialize};

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
enum AccountType {
Expand All @@ -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!(account.serialize(), account_edn_str);

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

Expand Down
3 changes: 2 additions & 1 deletion tests/serialize.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use edn_derive::Serialize;
use edn_rs::Serialize;

#[derive(Serialize)]
enum Kind {
Expand All @@ -21,7 +22,7 @@ fn main() {
kind: Kind::Chill,
};
assert_eq!(
edn_rs::to_string(person),
person.serialize(),
"{ :name \"joana\", :age 290000, :kind :kind/chill, }"
);
}
9 changes: 3 additions & 6 deletions tests/unit_struct.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use edn_derive::{Serialize, Deserialize};
use edn_rs::EdnError;
use edn_derive::{Deserialize, Serialize};
use edn_rs::{EdnError, Serialize};

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Nothing;

fn main() -> Result<(), EdnError> {
let nothing = Nothing;

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

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

Expand Down

0 comments on commit e580f12

Please sign in to comment.