@@ -15,7 +15,7 @@ use crate::buf::IntoIter;
1515#[ allow( unused) ]
1616use crate :: loom:: sync:: atomic:: AtomicMut ;
1717use 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
11521152unsafe 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
0 commit comments