Skip to content

Commit fc60e97

Browse files
feat: add stabledref
1 parent 6929f6d commit fc60e97

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ default = []
1818

1919
[dependencies]
2020
crossbeam-queue = "0.2.0"
21+
stable_deref_trait = "1.1.1"

src/pool.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::ops::{Deref, DerefMut};
44
use std::ptr;
55

66
use crossbeam_queue::SegQueue;
7+
use stable_deref_trait::StableDeref;
78

89
use crate::poolable::{Poolable, Realloc};
910

@@ -131,6 +132,9 @@ impl<'a, T: Poolable> DerefMut for Block<'a, T> {
131132
}
132133
}
133134

135+
// Safe because Block is just a wrapper around `T`.
136+
unsafe impl<'a, T: StableDeref + Poolable> StableDeref for Block<'a, T> {}
137+
134138
#[cfg(test)]
135139
mod tests {
136140
use super::*;
@@ -245,6 +249,18 @@ mod tests {
245249
use std::collections::HashMap;
246250
let pool: BytePool<HashMap<String, String>> = BytePool::new();
247251

252+
let mut map = pool.alloc(4);
253+
for i in 0..4 {
254+
map.insert(format!("hello_{}", i), "world".into());
255+
}
256+
for i in 0..4 {
257+
assert_eq!(
258+
map.get(&format!("hello_{}", i)).unwrap(),
259+
&"world".to_string()
260+
);
261+
}
262+
drop(map);
263+
248264
for i in 0..100 {
249265
let mut block_1k = pool.alloc(1 * 1024);
250266
let mut block_4k = pool.alloc(4 * 1024);

0 commit comments

Comments
 (0)