Skip to content

Commit 90299da

Browse files
committed
0.3.2: less code is better code
1 parent 403318e commit 90299da

File tree

8 files changed

+142
-124
lines changed

8 files changed

+142
-124
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "toolshed"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
authors = ["maciejhirsz <[email protected]>"]
55
license = "MIT/Apache-2.0"
66
description = "Arena allocator and a handful of useful data structures"
@@ -18,3 +18,6 @@ serde_json = "1.0"
1818
default = []
1919

2020
impl_serialize = ["serde"]
21+
22+
[profile.bench]
23+
lto = true

src/cell.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,21 @@ use std::marker::PhantomData;
66
/// This should be identical to the `Cell` implementation in the standard
77
/// library, but always require that the internal type implements `Copy`
88
/// and implements `Copy` itself.
9-
#[derive(PartialEq)]
10-
pub struct CopyCell<T: Copy> {
9+
#[derive(PartialEq, Eq)]
10+
pub struct CopyCell<T> {
1111
/// Internal value
12-
pub(crate) value: T,
12+
value: T,
1313

1414
/// We trick the compiler to think that `CopyCell` contains a raw pointer,
1515
/// this way we make sure the `Sync` marker is not implemented and `CopyCell`
1616
/// cannot be shared across threads!
17-
pub(crate) _no_sync: PhantomData<*mut T>
17+
_no_sync: PhantomData<*mut T>
1818
}
1919

2020
/// `Sync` is unsafe due to mutability, however `Send` is totally fine!
21-
unsafe impl<T: Copy> Send for CopyCell<T> {}
21+
unsafe impl<T> Send for CopyCell<T> {}
2222

23-
impl<T: Copy + Eq> Eq for CopyCell<T> {}
24-
25-
impl<T: Copy> CopyCell<T> {
23+
impl<T> CopyCell<T> {
2624
/// Creates a new `CopyCell` containing the given value.
2725
#[inline]
2826
pub fn new(value: T) -> Self {
@@ -31,7 +29,9 @@ impl<T: Copy> CopyCell<T> {
3129
_no_sync: PhantomData
3230
}
3331
}
32+
}
3433

34+
impl<T: Copy> CopyCell<T> {
3535
#[inline]
3636
fn mut_ptr(&self) -> *mut T {
3737
&self.value as *const T as *mut T
@@ -119,4 +119,13 @@ mod test {
119119
assert_eq!(cell_b.get(), 300);
120120
assert_eq!(cell_c.get(), 200);
121121
}
122+
123+
#[test]
124+
fn contain_static_ref() {
125+
static REF: &(&u64, u64) = &(&0, 0);
126+
127+
let cell = CopyCell::new(REF);
128+
129+
assert_eq!(cell.get(), REF);
130+
}
122131
}

src/impl_debug.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::fmt::{self, Debug};
2-
use std::hash::Hash;
32
use list::List;
43
use map::{Map, BloomMap};
54
use set::{Set, BloomSet};
65

76
impl<'arena, T> Debug for List<'arena, T>
87
where
9-
T: 'arena + Debug,
8+
T: Debug,
109
{
1110
#[inline]
1211
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -16,8 +15,8 @@ where
1615

1716
impl<'arena, K, V> Debug for Map<'arena, K, V>
1817
where
19-
K: 'arena + Debug + Eq + Hash + Copy,
20-
V: 'arena + Debug + Copy,
18+
K: Debug,
19+
V: Debug + Copy,
2120
{
2221
#[inline]
2322
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -27,8 +26,8 @@ where
2726

2827
impl<'arena, K, V> Debug for BloomMap<'arena, K, V>
2928
where
30-
K: 'arena + Debug + Eq + Hash + Copy + AsRef<[u8]>,
31-
V: 'arena + Debug + Copy,
29+
K: Debug,
30+
V: Debug + Copy,
3231
{
3332
#[inline]
3433
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -38,7 +37,7 @@ where
3837

3938
impl<'arena, I> Debug for Set<'arena, I>
4039
where
41-
I: 'arena + Debug + Eq + Hash + Copy,
40+
I: Debug,
4241
{
4342
#[inline]
4443
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -48,7 +47,7 @@ where
4847

4948
impl<'arena, I> Debug for BloomSet<'arena, I>
5049
where
51-
I: 'arena + Debug + Eq + Hash + Copy + AsRef<[u8]>,
50+
I: Debug,
5251
{
5352
#[inline]
5453
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

src/impl_partial_eq.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,57 @@
1-
use std::hash::Hash;
21
use list::List;
32
use map::{Map, BloomMap};
43
use set::{Set, BloomSet};
54

6-
impl<'arena, T> PartialEq for List<'arena, T>
5+
impl<'a, 'b, A, B> PartialEq<List<'b, B>> for List<'a, A>
76
where
8-
T: 'arena + PartialEq,
7+
A: PartialEq<B>,
98
{
109
#[inline]
11-
fn eq(&self, other: &Self) -> bool {
10+
fn eq(&self, other: &List<'b, B>) -> bool {
1211
self.iter().eq(other.iter())
1312
}
1413
}
1514

16-
impl<'arena, K, V> PartialEq for Map<'arena, K, V>
15+
impl<'a, 'b, KA, VA, KB, VB> PartialEq<Map<'b, KB, VB>> for Map<'a, KA, VA>
1716
where
18-
K: 'arena + Eq + Hash + Copy,
19-
V: 'arena + PartialEq + Copy,
17+
(&'a KA, VA): PartialEq<(&'b KB, VB)>,
18+
VA: Copy,
19+
VB: Copy,
2020
{
2121
#[inline]
22-
fn eq(&self, other: &Self) -> bool {
22+
fn eq(&self, other: &Map<'b, KB, VB>) -> bool {
2323
self.iter().eq(other.iter())
2424
}
2525
}
2626

27-
impl<'arena, K, V> PartialEq for BloomMap<'arena, K, V>
27+
impl<'a, 'b, KA, VA, KB, VB> PartialEq<BloomMap<'b, KB, VB>> for BloomMap<'a, KA, VA>
2828
where
29-
K: 'arena + Eq + Hash + Copy + AsRef<[u8]>,
30-
V: 'arena + PartialEq + Copy,
29+
(&'a KA, VA): PartialEq<(&'b KB, VB)>,
30+
VA: Copy,
31+
VB: Copy,
3132
{
3233
#[inline]
33-
fn eq(&self, other: &Self) -> bool {
34+
fn eq(&self, other: &BloomMap<'b, KB, VB>) -> bool {
3435
self.iter().eq(other.iter())
3536
}
3637
}
3738

38-
impl<'arena, I> PartialEq for Set<'arena, I>
39+
impl<'a, 'b, A, B> PartialEq<Set<'b, B>> for Set<'a, A>
3940
where
40-
I: 'arena + Eq + Hash + Copy,
41+
A: PartialEq<B>,
4142
{
4243
#[inline]
43-
fn eq(&self, other: &Self) -> bool {
44+
fn eq(&self, other: &Set<'b, B>) -> bool {
4445
self.iter().eq(other.iter())
4546
}
4647
}
4748

48-
impl<'arena, I> PartialEq for BloomSet<'arena, I>
49+
impl<'a, 'b, A, B> PartialEq<BloomSet<'b, B>> for BloomSet<'a, A>
4950
where
50-
I: 'arena + Eq + Hash + Copy + AsRef<[u8]>,
51+
A: PartialEq<B>,
5152
{
5253
#[inline]
53-
fn eq(&self, other: &Self) -> bool {
54+
fn eq(&self, other: &BloomSet<'b, B>) -> bool {
5455
self.iter().eq(other.iter())
5556
}
5657
}

src/impl_serialize.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::hash::Hash;
21
use serde::ser::{Serialize, Serializer};
32
use list::List;
43
use map::{Map, BloomMap};
@@ -19,8 +18,8 @@ where
1918

2019
impl<'arena, K, V> Serialize for Map<'arena, K, V>
2120
where
22-
K: 'arena + Serialize + Eq + Hash + Copy,
23-
V: 'arena + Serialize + Copy,
21+
K: Serialize,
22+
V: Serialize + Copy,
2423
{
2524
#[inline]
2625
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@@ -33,8 +32,8 @@ where
3332

3433
impl<'arena, K, V> Serialize for BloomMap<'arena, K, V>
3534
where
36-
K: 'arena + Serialize + Eq + Hash + Copy + AsRef<[u8]>,
37-
V: 'arena + Serialize + Copy,
35+
K: Serialize,
36+
V: Serialize + Copy,
3837
{
3938
#[inline]
4039
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@@ -47,7 +46,7 @@ where
4746

4847
impl<'arena, I> Serialize for Set<'arena, I>
4948
where
50-
I: 'arena + Serialize + Eq + Hash + Copy,
49+
I: Serialize,
5150
{
5251
#[inline]
5352
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@@ -60,7 +59,7 @@ where
6059

6160
impl<'arena, I> Serialize for BloomSet<'arena, I>
6261
where
63-
I: 'arena + Serialize + Eq + Hash + Copy + AsRef<[u8]>,
62+
I: Serialize,
6463
{
6564
#[inline]
6665
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>

src/list.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub struct List<'arena, T: 'arena> {
99
root: CopyCell<Option<&'arena ListItem<'arena, T>>>,
1010
}
1111

12+
impl<'arena, T: Copy> Copy for List<'arena, T> { }
13+
1214
impl<'arena, T: 'arena> List<'arena, T> {
1315
/// Create a new empty `List`.
1416
#[inline]
@@ -143,7 +145,6 @@ impl<'arena, T: 'arena + Copy> List<'arena, T> {
143145
}
144146
}
145147

146-
147148
impl<'arena, T: 'arena> IntoIterator for List<'arena, T> {
148149
type Item = &'arena T;
149150
type IntoIter = ListIter<'arena, T>;
@@ -164,7 +165,6 @@ impl<'a, 'arena, T: 'arena> IntoIterator for &'a List<'arena, T> {
164165
}
165166
}
166167

167-
impl<'arena, T: Copy> Copy for List<'arena, T> { }
168168

169169
#[derive(Debug, PartialEq, Clone)]
170170
struct ListItem<'arena, T: 'arena> {

0 commit comments

Comments
 (0)