Skip to content

Commit ced0507

Browse files
authored
Make BufMut an unsafe trait (#432)
Users of `BufMut` are unable to defend against incorrect implementations of `BufMut`, this makes the trait unsafe to implement. Fixes #329
1 parent 94c543f commit ced0507

File tree

5 files changed

+9
-9
lines changed

5 files changed

+9
-9
lines changed

src/buf/buf_mut.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use alloc::{boxed::Box, vec::Vec};
3030
///
3131
/// assert_eq!(buf, b"hello world");
3232
/// ```
33-
pub trait BufMut {
33+
pub unsafe trait BufMut {
3434
/// Returns the number of bytes that can be written from the current
3535
/// position until the end of the buffer is reached.
3636
///
@@ -992,15 +992,15 @@ macro_rules! deref_forward_bufmut {
992992
};
993993
}
994994

995-
impl<T: BufMut + ?Sized> BufMut for &mut T {
995+
unsafe impl<T: BufMut + ?Sized> BufMut for &mut T {
996996
deref_forward_bufmut!();
997997
}
998998

999-
impl<T: BufMut + ?Sized> BufMut for Box<T> {
999+
unsafe impl<T: BufMut + ?Sized> BufMut for Box<T> {
10001000
deref_forward_bufmut!();
10011001
}
10021002

1003-
impl BufMut for &mut [u8] {
1003+
unsafe impl BufMut for &mut [u8] {
10041004
#[inline]
10051005
fn remaining_mut(&self) -> usize {
10061006
self.len()
@@ -1020,7 +1020,7 @@ impl BufMut for &mut [u8] {
10201020
}
10211021
}
10221022

1023-
impl BufMut for Vec<u8> {
1023+
unsafe impl BufMut for Vec<u8> {
10241024
#[inline]
10251025
fn remaining_mut(&self) -> usize {
10261026
usize::MAX - self.len()

src/buf/chain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ where
174174
}
175175
}
176176

177-
impl<T, U> BufMut for Chain<T, U>
177+
unsafe impl<T, U> BufMut for Chain<T, U>
178178
where
179179
T: BufMut,
180180
U: BufMut,

src/buf/limit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<T> Limit<T> {
5555
}
5656
}
5757

58-
impl<T: BufMut> BufMut for Limit<T> {
58+
unsafe impl<T: BufMut> BufMut for Limit<T> {
5959
fn remaining_mut(&self) -> usize {
6060
cmp::min(self.inner.remaining_mut(), self.limit)
6161
}

src/bytes_mut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ impl Buf for BytesMut {
966966
}
967967
}
968968

969-
impl BufMut for BytesMut {
969+
unsafe impl BufMut for BytesMut {
970970
#[inline]
971971
fn remaining_mut(&self) -> usize {
972972
usize::MAX - self.len()

tests/test_buf_mut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn test_mut_slice() {
7575
fn test_deref_bufmut_forwards() {
7676
struct Special;
7777

78-
impl BufMut for Special {
78+
unsafe impl BufMut for Special {
7979
fn remaining_mut(&self) -> usize {
8080
unreachable!("remaining_mut");
8181
}

0 commit comments

Comments
 (0)