Skip to content

Commit 4950c50

Browse files
authored
Offset from (#705)
1 parent 86694b0 commit 4950c50

File tree

3 files changed

+20
-39
lines changed

3 files changed

+20
-39
lines changed

src/bytes.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::buf::IntoIter;
1515
#[allow(unused)]
1616
use crate::loom::sync::atomic::AtomicMut;
1717
use crate::loom::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
18-
use crate::{Buf, BytesMut};
18+
use crate::{offset_from, Buf, BytesMut};
1919

2020
/// A cheaply cloneable and sliceable chunk of contiguous memory.
2121
///
@@ -1037,7 +1037,7 @@ unsafe fn promotable_to_vec(
10371037

10381038
let buf = f(shared);
10391039

1040-
let cap = (ptr as usize - buf as usize) + len;
1040+
let cap = offset_from(ptr, buf) + len;
10411041

10421042
// Copy back buffer
10431043
ptr::copy(ptr, buf, len);
@@ -1150,7 +1150,7 @@ unsafe fn promotable_is_unique(data: &AtomicPtr<()>) -> bool {
11501150
}
11511151

11521152
unsafe fn free_boxed_slice(buf: *mut u8, offset: *const u8, len: usize) {
1153-
let cap = (offset as usize - buf as usize) + len;
1153+
let cap = offset_from(offset, buf) + len;
11541154
dealloc(buf, Layout::from_size_align(cap, 1).unwrap())
11551155
}
11561156

@@ -1312,7 +1312,7 @@ unsafe fn shallow_clone_vec(
13121312
// vector.
13131313
let shared = Box::new(Shared {
13141314
buf,
1315-
cap: (offset as usize - buf as usize) + len,
1315+
cap: offset_from(offset, buf) + len,
13161316
// Initialize refcount to 2. One for this reference, and one
13171317
// for the new clone that will be returned from
13181318
// `shallow_clone`.
@@ -1422,23 +1422,6 @@ where
14221422
new_addr as *mut u8
14231423
}
14241424

1425-
/// Precondition: dst >= original
1426-
///
1427-
/// The following line is equivalent to:
1428-
///
1429-
/// ```rust,ignore
1430-
/// self.ptr.as_ptr().offset_from(ptr) as usize;
1431-
/// ```
1432-
///
1433-
/// But due to min rust is 1.39 and it is only stabilized
1434-
/// in 1.47, we cannot use it.
1435-
#[inline]
1436-
fn offset_from(dst: *const u8, original: *const u8) -> usize {
1437-
debug_assert!(dst >= original);
1438-
1439-
dst as usize - original as usize
1440-
}
1441-
14421425
// compile-fails
14431426

14441427
/// ```compile_fail

src/bytes_mut.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::bytes::Vtable;
1717
#[allow(unused)]
1818
use crate::loom::sync::atomic::AtomicMut;
1919
use crate::loom::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
20-
use crate::{Buf, BufMut, Bytes};
20+
use crate::{offset_from, Buf, BufMut, Bytes};
2121

2222
/// A unique reference to a contiguous slice of memory.
2323
///
@@ -1683,23 +1683,6 @@ fn invalid_ptr<T>(addr: usize) -> *mut T {
16831683
ptr.cast::<T>()
16841684
}
16851685

1686-
/// Precondition: dst >= original
1687-
///
1688-
/// The following line is equivalent to:
1689-
///
1690-
/// ```rust,ignore
1691-
/// self.ptr.as_ptr().offset_from(ptr) as usize;
1692-
/// ```
1693-
///
1694-
/// But due to min rust is 1.39 and it is only stabilized
1695-
/// in 1.47, we cannot use it.
1696-
#[inline]
1697-
fn offset_from(dst: *mut u8, original: *mut u8) -> usize {
1698-
debug_assert!(dst >= original);
1699-
1700-
dst as usize - original as usize
1701-
}
1702-
17031686
unsafe fn rebuild_vec(ptr: *mut u8, mut len: usize, mut cap: usize, off: usize) -> Vec<u8> {
17041687
let ptr = ptr.sub(off);
17051688
len += off;

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,18 @@ fn panic_does_not_fit(size: usize, nbytes: usize) -> ! {
148148
size, nbytes
149149
);
150150
}
151+
152+
/// Precondition: dst >= original
153+
///
154+
/// The following line is equivalent to:
155+
///
156+
/// ```rust,ignore
157+
/// self.ptr.as_ptr().offset_from(ptr) as usize;
158+
/// ```
159+
///
160+
/// But due to min rust is 1.39 and it is only stabilized
161+
/// in 1.47, we cannot use it.
162+
#[inline]
163+
fn offset_from(dst: *const u8, original: *const u8) -> usize {
164+
dst as usize - original as usize
165+
}

0 commit comments

Comments
 (0)