Skip to content

Commit f7f8cf0

Browse files
authored
Merge pull request #6 from engali94/bug/non-zero
Fix #5 Remove non-zero offset related implementation
2 parents 7027aaf + a0f5cfb commit f7f8cf0

4 files changed

Lines changed: 0 additions & 50 deletions

File tree

examples/basic_usage.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ impl Node {
1414
self_ref: SelfRef::null(),
1515
};
1616

17-
// Set up the self-reference
1817
node.self_ref.set(&mut node.value).unwrap();
1918
node
2019
}

src/error.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ pub(crate) enum IntegerOffsetErrorImpl {
1212
Conversion(isize),
1313
/// Failed to subtract the two usizes (overflowed isize)
1414
Sub(usize, usize),
15-
/// Got a zero when a non-zero value was expected (for `NonZero*`)
16-
InvalidNonZero,
1715
}
1816

1917
#[cfg(not(feature = "no_std"))]
@@ -34,13 +32,6 @@ mod fmt {
3432
IntegerOffsetErrorImpl::Sub(a, b) => {
3533
write!(f, "Difference is beween {} and {} overflows `isize`", a, b)
3634
}
37-
38-
IntegerOffsetErrorImpl::InvalidNonZero => {
39-
write!(
40-
f,
41-
"Difference was zero when a `NonZero*` type was specified"
42-
)
43-
}
4435
}
4536
}
4637
}

src/offset/integers.rs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::delta::{Nullable, Offset};
22
use crate::error::{IntegerOffsetError, IntegerOffsetErrorImpl};
33
use crate::pointer::unreachable::{UncheckedOptionExt, OVERFLOW_SUB};
4-
use core::num::*;
54

65
macro_rules! impl_delta_zeroable {
76
($($type:ty),* $(,)?) => {$(
@@ -41,40 +40,3 @@ macro_rules! impl_delta_zeroable {
4140
}
4241

4342
impl_delta_zeroable! { i8, i16, i32, i64, i128, isize }
44-
45-
macro_rules! impl_delta_nonzero {
46-
($($type:ident $base:ident),* $(,)?) => {$(
47-
unsafe impl Offset for $type {
48-
type Error = IntegerOffsetError;
49-
50-
fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error> {
51-
let del = match isize::checked_sub(a as usize as _, b as usize as _) {
52-
None => return Err(IntegerOffsetError(IntegerOffsetErrorImpl::Sub(a as usize, b as usize))),
53-
Some(0) => return Err(IntegerOffsetError(IntegerOffsetErrorImpl::InvalidNonZero)),
54-
Some(del) => del,
55-
};
56-
57-
if std::mem::size_of::<Self>() < std::mem::size_of::<isize>() && (
58-
($base::MIN as isize) > del ||
59-
($base::MAX as isize) < del
60-
)
61-
{
62-
Err(IntegerOffsetError(IntegerOffsetErrorImpl::Conversion(del)))
63-
} else {
64-
// 0 case was checked in match before hand, so this is guarenteed ot be non zero
65-
unsafe { Ok(Self::new_unchecked(del as _)) }
66-
}
67-
}
68-
69-
unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self {
70-
Self::new_unchecked(isize::checked_sub(a as usize as _, b as usize as _).unchecked_unwrap(OVERFLOW_SUB) as _)
71-
}
72-
73-
unsafe fn add(self, a: *const u8) -> *mut u8 {
74-
<*mut u8>::offset(a as _, self.get() as isize) as *mut u8
75-
}
76-
}
77-
)*};
78-
}
79-
80-
impl_delta_nonzero! { NonZeroI8 i8, NonZeroI16 i16, NonZeroI32 i32, NonZeroI64 i64, NonZeroI128 i128, NonZeroIsize isize }

src/pointer/self_ref.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ fn nn_to_ptr<T: ?Sized>(nn: Ptr<T>) -> *mut T {
5959
/// the entire structure is always safe - it's only internal layout changes that cause issues.
6060
///
6161
/// Special care needed with packed structs: field reordering during drops can invalidate offsets.
62-
///
63-
/// Using `NonZero*` offset types with self-pointing (zero offset) is undefined behavior.
6462
pub struct SelfRef<T: ?Sized + PointerRecomposition, I: Offset = isize>(
6563
I,
6664
MaybeUninit<T::Components>,

0 commit comments

Comments
 (0)