Skip to content

Commit 080e0c7

Browse files
committed
ensure len and capacity methods reflect their intentions
1 parent 2a50848 commit 080e0c7

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//!
1010
//! // Allocate a buffer with capacity 1024.
1111
//! let mut buf = pool.alloc(1024);
12+
//! buf.resize(1024, 0);
1213
//!
1314
//! // write some data into it
1415
//! for i in 0..100 {

src/pool.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ impl<T: Poolable> BytePool<T> {
9090

9191
impl<'a, T: Poolable> Drop for Block<'a, T> {
9292
fn drop(&mut self) {
93-
let data = mem::ManuallyDrop::into_inner(unsafe { ptr::read(&self.data) });
93+
let mut data = mem::ManuallyDrop::into_inner(unsafe { ptr::read(&self.data) });
94+
data.reset();
9495
self.pool.push_raw_block(data);
9596
}
9697
}
@@ -138,6 +139,20 @@ unsafe impl<'a, T: StableDeref + Poolable> StableDeref for Block<'a, T> {}
138139
#[cfg(test)]
139140
mod tests {
140141
use super::*;
142+
#[test]
143+
fn append() {
144+
let pool = BytePool::<Vec<u8>>::new();
145+
let mut buf = pool.alloc(4);
146+
assert_eq!(0, buf.len());
147+
assert_eq!(4, buf.capacity());
148+
buf.push(12u8);
149+
assert_eq!(1, buf.len());
150+
buf.extend_from_slice("hello".as_bytes());
151+
assert_eq!(6, buf.len());
152+
buf.clear();
153+
assert_eq!(0, buf.len());
154+
assert!(buf.capacity() > 0);
155+
}
141156

142157
#[test]
143158
fn basics_vec_u8() {
@@ -174,6 +189,7 @@ mod tests {
174189
let _slice: &[u8] = &buf;
175190

176191
assert_eq!(buf.capacity(), 10);
192+
buf.resize(10, 0);
177193
for i in 0..10 {
178194
buf[i] = 1;
179195
}
@@ -199,15 +215,15 @@ mod tests {
199215
let h1 = std::thread::spawn(move || {
200216
for _ in 0..100 {
201217
let mut buf = pool1.alloc(64);
202-
buf[10] = 10;
218+
buf.push(10);
203219
}
204220
});
205221

206222
let pool2 = pool.clone();
207223
let h2 = std::thread::spawn(move || {
208224
for _ in 0..100 {
209225
let mut buf = pool2.alloc(64);
210-
buf[10] = 10;
226+
buf.push(10);
211227
}
212228
});
213229

src/poolable.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,32 @@ use std::hash::{BuildHasher, Hash};
33

44
/// The trait required to be able to use a type in `BytePool`.
55
pub trait Poolable {
6+
fn empty(&self) -> bool;
7+
fn len(&self) -> usize;
68
fn capacity(&self) -> usize;
79
fn alloc(size: usize) -> Self;
10+
fn reset(&mut self);
811
}
912

1013
impl<T: Default + Clone> Poolable for Vec<T> {
11-
fn capacity(&self) -> usize {
14+
fn empty(&self) -> bool {
15+
self.len() == 0
16+
}
17+
18+
fn len(&self) -> usize {
1219
self.len()
1320
}
1421

22+
fn capacity(&self) -> usize {
23+
self.capacity()
24+
}
25+
1526
fn alloc(size: usize) -> Self {
16-
vec![T::default(); size]
27+
Vec::<T>::with_capacity(size)
28+
}
29+
30+
fn reset(&mut self) {
31+
self.clear();
1732
}
1833
}
1934

@@ -22,13 +37,25 @@ where
2237
K: Eq + Hash,
2338
S: BuildHasher + Default,
2439
{
25-
fn capacity(&self) -> usize {
40+
fn empty(&self) -> bool {
41+
self.len() == 0
42+
}
43+
44+
fn len(&self) -> usize {
2645
self.len()
2746
}
2847

48+
fn capacity(&self) -> usize {
49+
self.capacity()
50+
}
51+
2952
fn alloc(size: usize) -> Self {
3053
HashMap::with_capacity_and_hasher(size, Default::default())
3154
}
55+
56+
fn reset(&mut self) {
57+
self.clear();
58+
}
3259
}
3360

3461
/// A trait allowing for efficient reallocation.
@@ -42,7 +69,7 @@ impl<T: Default + Clone> Realloc for Vec<T> {
4269

4370
assert!(new_size > 0);
4471
match new_size.cmp(&self.capacity()) {
45-
Greater => self.resize(new_size, T::default()),
72+
Greater => self.reserve(new_size - self.capacity()),
4673
Less => {
4774
self.truncate(new_size);
4875
self.shrink_to_fit();

0 commit comments

Comments
 (0)