Skip to content

Commit c20dd80

Browse files
authored
Implement Deref on pool buffers (#1533)
Minor usability improvement on the buffers for the newly introduced memory pool. ### Does this change impact existing behavior? No. ### Does this change need a changelog entry? Does it require a version change? No. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and I agree to the terms of the [Developer Certificate of Origin (DCO)](https://developercertificate.org/). Signed-off-by: Alessandro Passaro <alexpax@amazon.co.uk>
1 parent 5732b47 commit c20dd80

File tree

1 file changed

+49
-23
lines changed

1 file changed

+49
-23
lines changed

mountpoint-s3-fs/src/memory/buffers.rs

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::io::Read;
2+
use std::ops::{Deref, DerefMut};
23

34
use bytes::Bytes;
45

@@ -45,8 +46,22 @@ impl PoolBuffer {
4546
}
4647
}
4748

48-
impl AsMut<[u8]> for PoolBuffer {
49-
fn as_mut(&mut self) -> &mut [u8] {
49+
impl Deref for PoolBuffer {
50+
type Target = [u8];
51+
52+
fn deref(&self) -> &Self::Target {
53+
match &self.0 {
54+
PoolBufferInner::Primary { buffer_ptr, size } => {
55+
// SAFETY: returned slice will be valid until this buffer is dropped.
56+
unsafe { std::slice::from_raw_parts(buffer_ptr.as_raw_ptr(), *size) }
57+
}
58+
PoolBufferInner::Secondary(boxed) => &boxed.data,
59+
}
60+
}
61+
}
62+
63+
impl DerefMut for PoolBuffer {
64+
fn deref_mut(&mut self) -> &mut Self::Target {
5065
match &mut self.0 {
5166
PoolBufferInner::Primary { buffer_ptr, size } => {
5267
// SAFETY: returned slice will be valid until this buffer is dropped.
@@ -57,15 +72,15 @@ impl AsMut<[u8]> for PoolBuffer {
5772
}
5873
}
5974

75+
impl AsMut<[u8]> for PoolBuffer {
76+
fn as_mut(&mut self) -> &mut [u8] {
77+
self
78+
}
79+
}
80+
6081
impl AsRef<[u8]> for PoolBuffer {
6182
fn as_ref(&self) -> &[u8] {
62-
match &self.0 {
63-
PoolBufferInner::Primary { buffer_ptr, size } => {
64-
// SAFETY: returned slice will be valid until this buffer is dropped.
65-
unsafe { std::slice::from_raw_parts(buffer_ptr.as_raw_ptr(), *size) }
66-
}
67-
PoolBufferInner::Secondary(boxed) => &boxed.data,
68-
}
83+
self
6984
}
7085
}
7186

@@ -125,15 +140,29 @@ impl PoolBufferMut {
125140
}
126141
}
127142

143+
impl Deref for PoolBufferMut {
144+
type Target = [u8];
145+
146+
fn deref(&self) -> &Self::Target {
147+
&self.buffer[..self.len]
148+
}
149+
}
150+
151+
impl DerefMut for PoolBufferMut {
152+
fn deref_mut(&mut self) -> &mut Self::Target {
153+
&mut self.buffer[..self.len]
154+
}
155+
}
156+
128157
impl AsRef<[u8]> for PoolBufferMut {
129158
fn as_ref(&self) -> &[u8] {
130-
&self.buffer.as_ref()[..self.len]
159+
self
131160
}
132161
}
133162

134163
impl AsMut<[u8]> for PoolBufferMut {
135164
fn as_mut(&mut self) -> &mut [u8] {
136-
&mut self.buffer.as_mut()[..self.len]
165+
self
137166
}
138167
}
139168

@@ -188,7 +217,7 @@ mod tests {
188217
assert_eq!(pool_buffer.capacity(), BUFFER_SIZE);
189218

190219
let data = &[42u8; BUFFER_SIZE];
191-
pool_buffer.as_mut().copy_from_slice(data);
220+
pool_buffer.copy_from_slice(data);
192221

193222
assert_eq!(pool_buffer.as_ref(), data);
194223

@@ -209,8 +238,8 @@ mod tests {
209238
assert!(pool_buffer.is_empty());
210239
assert!(!pool_buffer.is_full());
211240

212-
let slice = pool_buffer.as_ref();
213-
assert!(slice.is_empty());
241+
let buffer_as_slice: &[u8] = &pool_buffer;
242+
assert!(buffer_as_slice.is_empty());
214243

215244
let data = vec![42u8; write_size];
216245
let mut slice = &data[..];
@@ -222,10 +251,10 @@ mod tests {
222251
assert_eq!(slice.len(), appended_size);
223252

224253
assert_eq!(pool_buffer.len(), appended_size);
225-
assert_eq!(pool_buffer.as_ref(), &data[..appended_size]);
254+
assert_eq!(&pool_buffer[..], &data[..appended_size]);
226255

227256
let bytes = pool_buffer.into_bytes();
228-
assert_eq!(bytes.as_ref(), &data[..appended_size]);
257+
assert_eq!(&bytes[..], &data[..appended_size]);
229258
}
230259

231260
#[test_matrix([primary, secondary], [SMALLER_THEN_BUFFER_SIZE, BUFFER_SIZE, LARGER_THAN_BUFFER_SIZE])]
@@ -241,7 +270,7 @@ mod tests {
241270
let result = pool_buffer.fill_from_reader(&data[..]);
242271
if read_size >= BUFFER_SIZE {
243272
result.expect("fill from large enough slice should succeed");
244-
assert_eq!(pool_buffer.as_ref(), &data[..BUFFER_SIZE]);
273+
assert_eq!(&pool_buffer[..], &data[..BUFFER_SIZE]);
245274
} else {
246275
result.expect_err("fill from small slice should fail");
247276
assert!(pool_buffer.is_empty());
@@ -268,14 +297,11 @@ mod tests {
268297
let result = pool_buffer.fill_from_reader(&data[..]);
269298
if read_size + INITIAL_SIZE >= BUFFER_SIZE {
270299
result.expect("fill from large enough slice should succeed");
271-
assert_eq!(&pool_buffer.as_ref()[..INITIAL_SIZE], &initial[..]);
272-
assert_eq!(
273-
&pool_buffer.as_ref()[INITIAL_SIZE..],
274-
&data[..BUFFER_SIZE - INITIAL_SIZE]
275-
);
300+
assert_eq!(&pool_buffer[..INITIAL_SIZE], &initial[..]);
301+
assert_eq!(&pool_buffer[INITIAL_SIZE..], &data[..BUFFER_SIZE - INITIAL_SIZE]);
276302
} else {
277303
result.expect_err("fill from small slice should fail");
278-
assert_eq!(pool_buffer.as_ref(), &initial[..]);
304+
assert_eq!(&pool_buffer[..], &initial[..]);
279305
}
280306
}
281307
}

0 commit comments

Comments
 (0)