From cb9e89dcb6b6ca16eda56507083e77f9da7fb492 Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Fri, 3 Mar 2023 07:59:38 -0800 Subject: [PATCH 1/6] removed try! --- Cargo.toml | 2 ++ src/lib.rs | 59 +++++++++++++++++++++++++++--------------------------- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4a0be2f..9796fda 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,12 +2,14 @@ name = "cons-list" version = "0.0.2" +edition = "2018" license = "MIT/Apache-2.0" description = "An immutable singly-linked list, as seen in basically every functional language" authors = [ "Alexis Beingessner ", ] + repository = "https://github.com/contain-rs/cons-list" homepage = "https://github.com/contain-rs/cons-list" documentation = "https://contain-rs.github.io/cons-list/cons-list" diff --git a/src/lib.rs b/src/lib.rs index 8eb57eb..501b833 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,12 +14,10 @@ #[cfg(test)] extern crate test; - - use std::cmp::Ordering; +use std::hash::{Hash, Hasher}; use std::iter; use std::rc::Rc; -use std::hash::{Hash, Hasher}; struct Node { elem: T, @@ -107,7 +105,6 @@ impl ConsList { } else { self.tailn(self.length - n) } - } /// Returns an iterator over references to the elements of the list in order @@ -136,21 +133,18 @@ impl Drop for ConsList { loop { let temp = head; match temp { - Some(node) => { - match Rc::try_unwrap(node) { - Ok(mut node) => { - head = node.next.take(); - } - _ => return, + Some(node) => match Rc::try_unwrap(node) { + Ok(mut node) => { + head = node.next.take(); } - } + _ => return, + }, _ => return, } } } } - impl<'a, T> Iterator for Iter<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<&'a T> { @@ -198,12 +192,10 @@ impl PartialOrd for ConsList { (None, None) => return Some(std::cmp::Ordering::Equal), (None, _) => return Some(std::cmp::Ordering::Less), (_, None) => return Some(std::cmp::Ordering::Greater), - (Some(x), Some(y)) => { - match x.partial_cmp(&y) { - Some(std::cmp::Ordering::Equal) => (), - non_eq => return non_eq, - } - } + (Some(x), Some(y)) => match x.partial_cmp(&y) { + Some(std::cmp::Ordering::Equal) => (), + non_eq => return non_eq, + }, } } } @@ -220,13 +212,13 @@ impl Clone for ConsList { impl std::fmt::Debug for ConsList { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - try!(write!(f, "[")); + write!(f, "[")?; for (i, e) in self.iter().enumerate() { if i != 0 { - try!(write!(f, ", ")); + write!(f, ", ")?; } - try!(write!(f, "{:?}", *e)); + write!(f, "{:?}", *e)?; } write!(f, "]") @@ -271,7 +263,11 @@ mod tests { m = m.tail(); assert_eq!(m.len(), 0); assert_eq!(m.head(), None); - m = m.append(Box::new(7)).append(Box::new(5)).append(Box::new(3)).append(Box::new(1)); + m = m + .append(Box::new(7)) + .append(Box::new(5)) + .append(Box::new(3)) + .append(Box::new(1)); assert_eq!(**m.head().unwrap(), 1); } @@ -424,27 +420,33 @@ mod tests { #[cfg(test)] mod bench { - use test::Bencher; use test; + use test::Bencher; use super::ConsList; #[bench] fn bench_collect_into(b: &mut test::Bencher) { let v = &[0i32; 64]; - b.iter(|| { let _: ConsList = v.iter().map(|x| *x).collect(); }) + b.iter(|| { + let _: ConsList = v.iter().map(|x| *x).collect(); + }) } #[bench] fn bench_append(b: &mut test::Bencher) { let mut m: ConsList = ConsList::new(); - b.iter(|| { m = m.append(0); }) + b.iter(|| { + m = m.append(0); + }) } #[bench] fn bench_append_tail(b: &mut test::Bencher) { let mut m: ConsList = ConsList::new(); - b.iter(|| { m = m.append(0).tail(); }) + b.iter(|| { + m = m.append(0).tail(); + }) } #[bench] @@ -452,8 +454,7 @@ mod bench { let v = &[0; 128]; let m: ConsList = v.iter().map(|&x| x).collect(); b.iter(|| { - assert!(m.iter().count() == 128); - }) + assert!(m.iter().count() == 128); + }) } } - From 6172b2a3c7b1004fab2a0e4ecd7c1999dbbee33c Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Fri, 3 Mar 2023 08:05:11 -0800 Subject: [PATCH 2/6] use Self --- src/lib.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 501b833..9329103 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,12 +21,12 @@ use std::rc::Rc; struct Node { elem: T, - next: Option>>, + next: Option>, } impl Node { - fn new(elem: T) -> Node { - Node { + fn new(elem: T) -> Self { + Self { elem: elem, next: None, } @@ -48,19 +48,19 @@ pub struct ConsList { impl ConsList { /// Constructs a new, empty `ConsList` - pub fn new() -> ConsList { - ConsList { + pub fn new() -> Self { + Self { front: None, length: 0, } } /// Returns a copy of the list, with `elem` appended to the front - pub fn append(&self, elem: T) -> ConsList { + pub fn append(&self, elem: T) -> Self { let mut new_node = Node::new(elem); new_node.next = self.front.clone(); - ConsList { + Self { front: Some(Rc::new(new_node)), length: self.len() + 1, } @@ -72,21 +72,21 @@ impl ConsList { } /// Returns a copy of the list, with the first element removed - pub fn tail(&self) -> ConsList { + pub fn tail(&self) -> Self { self.tailn(1) } /// Returns a copy of the list, with the first `n` elements removed - pub fn tailn(&self, n: usize) -> ConsList { + pub fn tailn(&self, n: usize) -> Self { if self.len() <= n { - ConsList::new() + Self::new() } else { let len = self.len() - n; let mut head = self.front.as_ref(); for _ in 0..n { head = head.unwrap().next.as_ref(); } - ConsList { + Self { front: Some(head.unwrap().clone()), length: len, } @@ -99,7 +99,7 @@ impl ConsList { } /// Returns a copy of the list, with only the last `n` elements remaining - pub fn lastn(&self, n: usize) -> ConsList { + pub fn lastn(&self, n: usize) -> Self { if n >= self.length { self.clone() } else { @@ -164,8 +164,8 @@ impl<'a, T> Iterator for Iter<'a, T> { } impl iter::FromIterator for ConsList { - fn from_iter>(iter: I) -> ConsList { - let mut list = ConsList::new(); + fn from_iter>(iter: I) -> Self { + let mut list = Self::new(); for elem in iter { list = list.append(elem); } @@ -174,17 +174,17 @@ impl iter::FromIterator for ConsList { } impl PartialEq for ConsList { - fn eq(&self, other: &ConsList) -> bool { + fn eq(&self, other: &Self) -> bool { self.len() == other.len() && self.iter().zip(other.iter()).all(|(x, y)| x == y) } - fn ne(&self, other: &ConsList) -> bool { + fn ne(&self, other: &Self) -> bool { self.len() != other.len() || self.iter().zip(other.iter()).all(|(x, y)| x != y) } } impl PartialOrd for ConsList { - fn partial_cmp(&self, other: &ConsList) -> Option { + fn partial_cmp(&self, other: &Self) -> Option { let mut a = self.iter(); let mut b = other.iter(); loop { @@ -202,8 +202,8 @@ impl PartialOrd for ConsList { } impl Clone for ConsList { - fn clone(&self) -> ConsList { - ConsList { + fn clone(&self) -> Self { + Self { front: self.front.clone(), length: self.length, } From 7d7e80f54d9835ed0b22984e2e758cf5701612e0 Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Fri, 3 Mar 2023 08:07:59 -0800 Subject: [PATCH 3/6] refactoring --- src/lib.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9329103..ff291cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,13 +11,13 @@ //! An immutable singly-linked list, as seen in basically every functional language. #![cfg_attr(test, feature(test))] -#[cfg(test)] -extern crate test; -use std::cmp::Ordering; -use std::hash::{Hash, Hasher}; -use std::iter; -use std::rc::Rc; +use std::{ + cmp::Ordering, + hash::{Hash, Hasher}, + iter, + rc::Rc, +}; struct Node { elem: T, @@ -26,10 +26,7 @@ struct Node { impl Node { fn new(elem: T) -> Self { - Self { - elem: elem, - next: None, - } + Self { elem, next: None } } } From 180cc820ceab26e0ec649176792334466b0ae192 Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Fri, 3 Mar 2023 08:10:35 -0800 Subject: [PATCH 4/6] bencher --- src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ff291cf..848293c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -417,13 +417,13 @@ mod tests { #[cfg(test)] mod bench { - use test; + extern crate test; use test::Bencher; use super::ConsList; #[bench] - fn bench_collect_into(b: &mut test::Bencher) { + fn bench_collect_into(b: &mut Bencher) { let v = &[0i32; 64]; b.iter(|| { let _: ConsList = v.iter().map(|x| *x).collect(); @@ -431,7 +431,7 @@ mod bench { } #[bench] - fn bench_append(b: &mut test::Bencher) { + fn bench_append(b: &mut Bencher) { let mut m: ConsList = ConsList::new(); b.iter(|| { m = m.append(0); @@ -439,7 +439,7 @@ mod bench { } #[bench] - fn bench_append_tail(b: &mut test::Bencher) { + fn bench_append_tail(b: &mut Bencher) { let mut m: ConsList = ConsList::new(); b.iter(|| { m = m.append(0).tail(); @@ -447,7 +447,7 @@ mod bench { } #[bench] - fn bench_iter(b: &mut test::Bencher) { + fn bench_iter(b: &mut Bencher) { let v = &[0; 128]; let m: ConsList = v.iter().map(|&x| x).collect(); b.iter(|| { From ab0e636c991c97c63ed081188dfa5e82d605607a Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Fri, 3 Mar 2023 08:14:21 -0800 Subject: [PATCH 5/6] default --- src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 848293c..22d1f6c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,6 +43,12 @@ pub struct ConsList { length: usize, } +impl Default for ConsList { + fn default() -> Self { + Self::new() + } +} + impl ConsList { /// Constructs a new, empty `ConsList` pub fn new() -> Self { From d8650be9bbe5f7159a78ed4ddcb3221fc8f0b8a0 Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Fri, 3 Mar 2023 08:22:52 -0800 Subject: [PATCH 6/6] defaulthasher --- src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 22d1f6c..727964e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,8 +25,8 @@ struct Node { } impl Node { - fn new(elem: T) -> Self { - Self { elem, next: None } + fn new(elem: T, next: Option>) -> Self { + Self { elem, next } } } @@ -60,11 +60,10 @@ impl ConsList { /// Returns a copy of the list, with `elem` appended to the front pub fn append(&self, elem: T) -> Self { - let mut new_node = Node::new(elem); - new_node.next = self.front.clone(); + let new_node = Node::new(elem, self.front.clone()); Self { - front: Some(Rc::new(new_node)), + front: Some(new_node.into()), length: self.len() + 1, } } @@ -123,7 +122,7 @@ impl ConsList { } pub fn is_empty(&self) -> bool { - return self.len() == 0; + self.len() == 0 } } @@ -250,6 +249,7 @@ mod tests { use std::hash; use super::ConsList; + use std::collections::hash_map::DefaultHasher; #[test] fn test_basic() { @@ -355,7 +355,7 @@ mod tests { let mut x = ConsList::new(); let mut y = ConsList::new(); - let mut h = hash::SipHasher::new(); + let mut h = DefaultHasher::new(); assert!(hash::Hash::hash(&x, &mut h) == hash::Hash::hash(&y, &mut h));