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. #137

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ tokio = { version = "1.33", features = ["full"] }
criterion = "0.5"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
edn-derive = "0.5.0"
edn-derive = { git = "https://github.com/Grinkers/edn-derive.git", branch = "no_move" }

[dev-dependencies.cargo-husky]
version = "1"
Expand Down
5 changes: 3 additions & 2 deletions benches/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ mod edn {
use criterion::Criterion;
use edn_derive::Serialize;

use edn_rs::{map, set};
use edn_rs::{map, set, Serialize};
use std::collections::{BTreeMap, BTreeSet};

pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("edn", |b| b.iter(|| edn_rs::to_string(val())));
let val = val();
c.bench_function("edn", |b| b.iter(|| val.serialize()));
}

fn val() -> ValEdn {
Expand Down
4 changes: 2 additions & 2 deletions examples/serialize.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use edn_derive::Serialize;
use edn_rs::{hmap, hset, map, set};
use edn_rs::{hmap, hset, map, set, Serialize};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

#[derive(Debug, Clone, Serialize)]
Expand Down Expand Up @@ -29,7 +29,7 @@ fn serialize() -> String {
nothing: (),
};

edn_rs::to_string(edn)
edn.serialize()
}

fn main() {
Expand Down
31 changes: 31 additions & 0 deletions examples/simple_serialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use edn_rs::Serialize;

struct Foo<'a> {
value: u64,
say: &'a str,
}

impl Serialize for Foo<'_> {
fn serialize(&self) -> String {
format!("{{:value {}, :say {:?}}}", self.value, self.say)
}
}

fn serialize() -> String {
let say = "Hello, World!";
let foo = Foo {
value: 42,
say: say,
};

foo.serialize()
}

fn main() {
println!("{}", serialize());
}

#[test]
fn test_serialize() {
assert_eq!(serialize(), "{:value 42, :say \"Hello, World!\"}");
}
38 changes: 2 additions & 36 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

use alloc::string::String;

/// Edn type implementation
pub mod edn;

Expand All @@ -21,7 +19,7 @@ pub mod edn;
/// ```rust
/// use std::collections::{BTreeMap, BTreeSet};
/// use edn_derive::Serialize;
/// use edn_rs::{set, map, edn::Edn};
/// use edn_rs::{set, map, edn::Edn, Serialize};
///
/// #[derive(Serialize)]
/// struct ExampleEdn {
Expand All @@ -35,7 +33,7 @@ pub mod edn;
/// set: set!{3i64, 4i64, 5i64},
/// tuples: (3i32, true, 'd')
/// };
/// println!("{}", edn_rs::to_string(edn));
/// println!("{}", edn.serialize());
/// // { :map {:this-is-a-key ["with", "many", "keys"]}, :set #{3, 4, 5}, :tuples (3, true, \d), }
/// }
///```
Expand Down Expand Up @@ -105,35 +103,3 @@ pub use edn::Error as EdnError;
pub use edn::Set;
pub use edn::{Edn, List, Map, Vector};
pub use serialize::Serialize;

/// Function for converting Rust types into EDN Strings.
/// For it to work, the type must implement the Serialize trait.
/// Use `#[derive(Serialize)]` from `edn-derive` crate.
///
/// Example:
/// ```rust
/// use std::collections::{BTreeMap, BTreeSet};
/// use edn_derive::Serialize;
/// use edn_rs::{set, map, edn::Edn};
///
/// #[derive(Debug, Serialize)]
/// struct ExampleEdn {
/// map: BTreeMap<String, Vec<String>>,
/// set: BTreeSet<i64>,
/// tuples: (i32, bool, char),
/// }
///
/// fn main() {
/// let edn = ExampleEdn {
/// map: map!{"this is a key".to_string() => vec!["with".to_string(), "many".to_string(), "keys".to_string()]},
/// set: set!{3i64, 4i64, 5i64},
/// tuples: (3i32, true, 'd')
/// };
/// println!("{}", edn_rs::to_string(edn));
/// // { :map {:this-is-a-key ["with", "many", "keys"]}, :set #{3, 4, 5}, :tuples (3, true, \d), }
/// }
///```
#[allow(clippy::needless_doctest_main)]
pub fn to_string<T: Serialize>(t: T) -> String {
t.serialize()
}
70 changes: 32 additions & 38 deletions src/serialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ use alloc::vec::Vec;
/// struct YourType;
///
/// impl Serialize for YourType {
/// fn serialize(self) -> String {
/// fn serialize(&self) -> String {
/// format!("{:?}", self)
/// }
/// }
/// ```
///
/// Implemented for all generic types.
pub trait Serialize {
fn serialize(self) -> String;
fn serialize(&self) -> String;
}

macro_rules! ser_primitives {
( $( $name:ty ),+ ) => {
$(
impl Serialize for $name
{
fn serialize(self) -> String {
fn serialize(&self) -> String {
format!("{:?}", self)
}
}
Expand All @@ -40,9 +40,9 @@ impl<T> Serialize for Vec<T>
where
T: Serialize,
{
fn serialize(self) -> String {
fn serialize(&self) -> String {
let aux_vec = self
.into_iter()
.iter()
.map(Serialize::serialize)
.collect::<Vec<String>>();
let mut s = String::new();
Expand All @@ -58,9 +58,9 @@ impl<T, H: std::hash::BuildHasher> Serialize for std::collections::HashSet<T, H>
where
T: Serialize,
{
fn serialize(self) -> String {
fn serialize(&self) -> String {
let aux_vec = self
.into_iter()
.iter()
.map(Serialize::serialize)
.collect::<Vec<String>>();
let mut s = String::new();
Expand All @@ -76,9 +76,9 @@ impl<T> Serialize for BTreeSet<T>
where
T: Serialize,
{
fn serialize(self) -> String {
fn serialize(&self) -> String {
let aux_vec = self
.into_iter()
.iter()
.map(Serialize::serialize)
.collect::<Vec<String>>();
let mut s = String::new();
Expand All @@ -94,9 +94,9 @@ impl<T> Serialize for LinkedList<T>
where
T: Serialize,
{
fn serialize(self) -> String {
fn serialize(&self) -> String {
let aux_vec = self
.into_iter()
.iter()
.map(Serialize::serialize)
.collect::<Vec<String>>();
let mut s = String::new();
Expand All @@ -112,9 +112,9 @@ impl<T, H: std::hash::BuildHasher> Serialize for std::collections::HashMap<Strin
where
T: Serialize,
{
fn serialize(self) -> String {
fn serialize(&self) -> String {
let aux_vec = self
.into_iter()
.iter()
.map(|(k, v)| format!(":{} {}", k.replace([' ', '_'], "-"), v.serialize()))
.collect::<Vec<String>>();
let mut s = String::new();
Expand All @@ -130,9 +130,9 @@ impl<T, H: ::std::hash::BuildHasher> Serialize for std::collections::HashMap<&st
where
T: Serialize,
{
fn serialize(self) -> String {
fn serialize(&self) -> String {
let aux_vec = self
.into_iter()
.iter()
.map(|(k, v)| format!(":{} {}", k.replace([' ', '_'], "-"), v.serialize()))
.collect::<Vec<String>>();
let mut s = String::new();
Expand All @@ -147,9 +147,9 @@ impl<T> Serialize for BTreeMap<String, T>
where
T: Serialize,
{
fn serialize(self) -> String {
fn serialize(&self) -> String {
let aux_vec = self
.into_iter()
.iter()
.map(|(k, v)| format!(":{} {}", k.replace([' ', '_'], "-"), v.serialize()))
.collect::<Vec<String>>();
let mut s = String::new();
Expand All @@ -164,16 +164,10 @@ impl<T> Serialize for BTreeMap<&str, T>
where
T: Serialize,
{
fn serialize(self) -> String {
fn serialize(&self) -> String {
let aux_vec = self
.into_iter()
.map(|(k, v)| {
format!(
":{} {}",
k.to_string().replace([' ', '_'], "-"),
v.serialize()
)
})
.iter()
.map(|(k, v)| format!(":{} {}", k.replace([' ', '_'], "-"), v.serialize()))
.collect::<Vec<String>>();
let mut s = String::new();
s.push('{');
Expand All @@ -187,25 +181,25 @@ where
ser_primitives![i8, i16, i32, i64, u8, u16, u32, u64, f32, f64, bool];

impl Serialize for () {
fn serialize(self) -> String {
fn serialize(&self) -> String {
"nil".to_string()
}
}

impl Serialize for String {
fn serialize(self) -> String {
fn serialize(&self) -> String {
format!("{self:?}")
}
}

impl Serialize for &str {
fn serialize(self) -> String {
fn serialize(&self) -> String {
format!("{self:?}")
}
}

impl Serialize for char {
fn serialize(self) -> String {
fn serialize(&self) -> String {
format!("\\{self}")
}
}
Expand All @@ -214,8 +208,8 @@ impl<T> Serialize for Option<T>
where
T: Serialize,
{
fn serialize(self) -> String {
self.map_or_else(
fn serialize(&self) -> String {
self.as_ref().map_or_else(
|| String::from("nil"),
crate::serialize::Serialize::serialize,
)
Expand All @@ -224,19 +218,19 @@ where

// Complex types
impl<A: Serialize> Serialize for (A,) {
fn serialize(self) -> String {
fn serialize(&self) -> String {
format!("({})", self.0.serialize())
}
}

impl<A: Serialize, B: Serialize> Serialize for (A, B) {
fn serialize(self) -> String {
fn serialize(&self) -> String {
format!("({}, {})", self.0.serialize(), self.1.serialize())
}
}

impl<A: Serialize, B: Serialize, C: Serialize> Serialize for (A, B, C) {
fn serialize(self) -> String {
fn serialize(&self) -> String {
format!(
"({}, {}, {})",
self.0.serialize(),
Expand All @@ -247,7 +241,7 @@ impl<A: Serialize, B: Serialize, C: Serialize> Serialize for (A, B, C) {
}

impl<A: Serialize, B: Serialize, C: Serialize, D: Serialize> Serialize for (A, B, C, D) {
fn serialize(self) -> String {
fn serialize(&self) -> String {
format!(
"({}, {}, {}, {})",
self.0.serialize(),
Expand All @@ -261,7 +255,7 @@ impl<A: Serialize, B: Serialize, C: Serialize, D: Serialize> Serialize for (A, B
impl<A: Serialize, B: Serialize, C: Serialize, D: Serialize, E: Serialize> Serialize
for (A, B, C, D, E)
{
fn serialize(self) -> String {
fn serialize(&self) -> String {
format!(
"({}, {}, {}, {}, {})",
self.0.serialize(),
Expand All @@ -276,7 +270,7 @@ impl<A: Serialize, B: Serialize, C: Serialize, D: Serialize, E: Serialize> Seria
impl<A: Serialize, B: Serialize, C: Serialize, D: Serialize, E: Serialize, F: Serialize> Serialize
for (A, B, C, D, E, F)
{
fn serialize(self) -> String {
fn serialize(&self) -> String {
format!(
"({}, {}, {}, {}, {}, {})",
self.0.serialize(),
Expand Down
6 changes: 3 additions & 3 deletions tests/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests {
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

use edn_derive::Serialize;
use edn_rs::{hmap, hset, map, set};
use edn_rs::{hmap, hset, map, set, Serialize};

#[test]
fn serializes_a_complex_structure() {
Expand All @@ -24,7 +24,7 @@ mod tests {
tuples: (3i32, true, 'd'),
};

assert_eq!(edn_rs::to_string(edn), "{ :btreemap {:this-is-a-key [\"with\", \"many\", \"keys\"]}, :btreeset #{3, 4, 5}, :hashmap {:this-is-a-key [\"with\", \"many\", \"keys\"]}, :hashset #{3}, :tuples (3, true, \\d), }");
assert_eq!(edn.serialize(), "{ :btreemap {:this-is-a-key [\"with\", \"many\", \"keys\"]}, :btreeset #{3, 4, 5}, :hashmap {:this-is-a-key [\"with\", \"many\", \"keys\"]}, :hashset #{3}, :tuples (3, true, \\d), }");
}

#[test]
Expand Down Expand Up @@ -54,7 +54,7 @@ mod tests {
},
};

assert_eq!(edn_rs::to_string(edn), "{ :value 3.4, :bar { :value \"data\", :foo-vec [{ :value false, }, { :value true, }], }, }");
assert_eq!(edn.serialize(), "{ :value 3.4, :bar { :value \"data\", :foo-vec [{ :value false, }, { :value true, }], }, }");
}
}

Expand Down