Skip to content

Commit

Permalink
maybe fix miri complaint (thanks @semicoleon!)
Browse files Browse the repository at this point in the history
  • Loading branch information
droundy committed Sep 16, 2022
1 parent 1baad6a commit f0298ac
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
23 changes: 20 additions & 3 deletions src/setu32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,16 @@ impl SetU32 {
} else {
let s = unsafe { &*self.0 };
let b = &s.b;
let a = unsafe { std::slice::from_raw_parts(&s.array as *const u32, b.cap as usize) };
let array = unsafe {
// Use the calculated offset to jump the pointer to where the array starts directly, keeping the permissions for the whole allocation intact.
self.0.cast::<u32>().offset(
// get a raw pointer to the array field
(&s.array as *const u32)
// calculate the offset from `*mut S` to the array field
.offset_from(self.0.cast()),
)
};
let a = unsafe { std::slice::from_raw_parts(array, b.cap as usize) };
if b.bits == 0 || b.bits > 32 {
Internal::Big { s: b, a }
} else if b.bits == 32 {
Expand All @@ -1542,8 +1551,16 @@ impl SetU32 {
} else {
let s = unsafe { &mut *self.0 };
let b = &mut s.b;
let a =
unsafe { std::slice::from_raw_parts_mut(&mut s.array as *mut u32, b.cap as usize) };
let array = unsafe {
// Use the calculated offset to jump the pointer to where the array starts directly, keeping the permissions for the whole allocation intact.
self.0.cast::<u32>().offset(
// get a raw pointer to the array field
(&mut s.array as *mut u32)
// calculate the offset from `*mut S` to the array field
.offset_from(self.0.cast()),
)
};
let a = unsafe { std::slice::from_raw_parts_mut(array, b.cap as usize) };
if b.bits == 0 || b.bits > 32 {
InternalMut::Big { s: b, a }
} else if b.bits == 32 {
Expand Down
22 changes: 20 additions & 2 deletions src/setu64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,16 @@ impl SetU64 {
} else {
let s = unsafe { &*self.0 };
let b = &s.b;
let a = unsafe { std::slice::from_raw_parts(&s.array as *const u64, b.cap) };
let array = unsafe {
// Use the calculated offset to jump the pointer to where the array starts directly, keeping the permissions for the whole allocation intact.
self.0.cast::<u64>().offset(
// get a raw pointer to the array field
(&s.array as *const u64)
// calculate the offset from `*mut S` to the array field
.offset_from(self.0.cast()),
)
};
let a = unsafe { std::slice::from_raw_parts(array, b.cap as usize) };
if b.bits == 0 || b.bits > 64 {
Internal::Big { s: b, a }
} else if b.bits == 64 {
Expand All @@ -1273,7 +1282,16 @@ impl SetU64 {
} else {
let s = unsafe { &mut *self.0 };
let b = &mut s.b;
let a = unsafe { std::slice::from_raw_parts_mut(&mut s.array as *mut u64, b.cap) };
let array = unsafe {
// Use the calculated offset to jump the pointer to where the array starts directly, keeping the permissions for the whole allocation intact.
self.0.cast::<u64>().offset(
// get a raw pointer to the array field
(&mut s.array as *mut u64)
// calculate the offset from `*mut S` to the array field
.offset_from(self.0.cast()),
)
};
let a = unsafe { std::slice::from_raw_parts_mut(array, b.cap as usize) };
if b.bits == 0 || b.bits > 64 {
InternalMut::Big { s: b, a }
} else if b.bits == 64 {
Expand Down

0 comments on commit f0298ac

Please sign in to comment.