mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-07 00:00:13 +02:00
Guarantee address in slice() for empty slices. (#780)
This commit is contained in:
+1
-1
@@ -401,7 +401,7 @@ impl Bytes {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if end == begin {
|
if end == begin {
|
||||||
return Bytes::new();
|
return Bytes::new_empty_with_ptr(self.ptr.wrapping_add(begin));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut ret = self.clone();
|
let mut ret = self.clone();
|
||||||
|
|||||||
@@ -1421,6 +1421,26 @@ fn try_reclaim_arc() {
|
|||||||
assert_eq!(true, buf.try_reclaim(6));
|
assert_eq!(true, buf.try_reclaim(6));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn slice_empty_addr() {
|
||||||
|
let buf = Bytes::from(vec![0; 1024]);
|
||||||
|
|
||||||
|
let ptr_start = buf.as_ptr();
|
||||||
|
let ptr_end = ptr_start.wrapping_add(1024);
|
||||||
|
|
||||||
|
let empty_end = buf.slice(1024..);
|
||||||
|
assert_eq!(empty_end.len(), 0);
|
||||||
|
assert_eq!(empty_end.as_ptr(), ptr_end);
|
||||||
|
|
||||||
|
let empty_start = buf.slice(..0);
|
||||||
|
assert_eq!(empty_start.len(), 0);
|
||||||
|
assert_eq!(empty_start.as_ptr(), ptr_start);
|
||||||
|
|
||||||
|
// Is miri happy about the provenance?
|
||||||
|
let _ = &empty_end[..];
|
||||||
|
let _ = &empty_start[..];
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn split_off_empty_addr() {
|
fn split_off_empty_addr() {
|
||||||
let mut buf = Bytes::from(vec![0; 1024]);
|
let mut buf = Bytes::from(vec![0; 1024]);
|
||||||
|
|||||||
Reference in New Issue
Block a user