Skip to content

Commit

Permalink
Auto merge of #501 - saethlin:less-unchecked, r=Amanieu
Browse files Browse the repository at this point in the history
Use a bit less NonNull::new_unchecked

Using `NonNull::cast` instead of `NonNull::new_unchecked` is a bit less unsafe code, and it may provide a small improvement to compile times since rust-lang/rust#120594
  • Loading branch information
bors committed Feb 12, 2024
2 parents f2e6212 + 46f1afc commit cf82944
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fnv = "1.0.7"
serde_test = "1.0"
doc-comment = "0.3.1"
bumpalo = { version = "3.13.0", features = ["allocator-api2"] }
rkyv = { version = "0.7.42", features = ["validation"] }
rkyv = { version = "0.7.42", default-features = false, features = ["size_32"] }

[features]
default = ["ahash", "inline-more", "allocator-api2"]
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//! [CppCon talk]: https://www.youtube.com/watch?v=ncHmEUmJZf4
#![no_std]
#![cfg_attr(feature = "nightly", allow(internal_features))]
#![cfg_attr(
feature = "nightly",
feature(
Expand Down
11 changes: 3 additions & 8 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,9 +919,7 @@ impl<T, A: Allocator> RawTable<T, A> {
/// [`undefined behavior`]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
#[inline]
pub fn data_end(&self) -> NonNull<T> {
// SAFETY: `self.table.ctrl` is `NonNull`, so casting it is safe
//
// `self.table.ctrl.as_ptr().cast()` returns pointer that
// `self.table.ctrl.cast()` returns pointer that
// points here (to the end of `T0`)
// ∨
// [Pad], T_n, ..., T1, T0, |CT0, CT1, ..., CT_n|, CTa_0, CTa_1, ..., CTa_m
Expand All @@ -938,7 +936,7 @@ impl<T, A: Allocator> RawTable<T, A> {
//
// P.S. `h1(hash) & self.bucket_mask` is the same as `hash as usize % self.buckets()` because the number
// of buckets is a power of two, and `self.bucket_mask = self.buckets() - 1`.
unsafe { NonNull::new_unchecked(self.table.ctrl.as_ptr().cast()) }
self.table.ctrl.cast()
}

/// Returns pointer to start of data table.
Expand Down Expand Up @@ -2595,10 +2593,7 @@ impl RawTableInner {
/// [`undefined behavior`]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
#[inline]
fn data_end<T>(&self) -> NonNull<T> {
unsafe {
// SAFETY: `self.ctrl` is `NonNull`, so casting it is safe
NonNull::new_unchecked(self.ctrl.as_ptr().cast())
}
self.ctrl.cast()
}

/// Returns an iterator-like object for a probe sequence on the table.
Expand Down
2 changes: 2 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2820,6 +2820,7 @@ mod test_set {
assert_eq!(last_i, 49);
}

#[allow(clippy::never_loop)] // The whole point is that it doesn't loop
for _ in &s {
panic!("s should be empty!");
}
Expand All @@ -2833,6 +2834,7 @@ mod test_set {
fn test_replace() {
use core::hash;

#[allow(dead_code)] // Having an unused field is the point of this test
#[derive(Debug)]
struct Foo(&'static str, i32);

Expand Down

0 comments on commit cf82944

Please sign in to comment.