Skip to content

Commit cec2d71

Browse files
roypatShadowCurse
authored andcommitted
chore: appease clippy
Arguably, go a bit further than what clippy wants, and replace all NonZero*::new_unchecked() with calls to .new().unwrap(). The expression is const, so has no runtime impact, and uses less unsafe. Signed-off-by: Patrick Roy <[email protected]>
1 parent 1392a2a commit cec2d71

File tree

5 files changed

+14
-18
lines changed

5 files changed

+14
-18
lines changed

src/bitmap/backend/atomic_bitmap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl Bitmap for AtomicBitmap {
184184
impl Default for AtomicBitmap {
185185
fn default() -> Self {
186186
// SAFETY: Safe as `0x1000` is non-zero.
187-
AtomicBitmap::new(0, unsafe { NonZeroUsize::new_unchecked(0x1000) })
187+
AtomicBitmap::new(0, const { NonZeroUsize::new(0x1000).unwrap() })
188188
}
189189
}
190190

@@ -221,7 +221,7 @@ mod tests {
221221
use crate::bitmap::tests::test_bitmap;
222222

223223
#[allow(clippy::undocumented_unsafe_blocks)]
224-
const DEFAULT_PAGE_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(128) };
224+
const DEFAULT_PAGE_SIZE: NonZeroUsize = NonZeroUsize::new(128).unwrap();
225225

226226
#[test]
227227
fn test_bitmap_basic() {

src/bitmap/backend/atomic_bitmap_arc.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ mod tests {
7878
#[test]
7979
fn test_bitmap_impl() {
8080
// SAFETY: `128` is non-zero.
81-
let b = AtomicBitmapArc::new(AtomicBitmap::new(0x800, unsafe {
82-
NonZeroUsize::new_unchecked(128)
83-
}));
81+
let b = AtomicBitmapArc::new(AtomicBitmap::new(0x800, NonZeroUsize::new(128).unwrap()));
8482
test_bitmap(&b);
8583
}
8684
}

src/mmap/unix.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl<B: NewBitmap> MmapRegion<B> {
247247
///
248248
/// # Arguments
249249
/// * `file_offset` - The mapping will be created at offset `file_offset.start` in the file
250-
/// referred to by `file_offset.file`.
250+
/// referred to by `file_offset.file`.
251251
/// * `size` - The size of the memory region in bytes.
252252
pub fn from_file(file_offset: FileOffset, size: usize) -> Result<Self> {
253253
MmapRegionBuilder::new_with_bitmap(size, B::with_len(size))
@@ -261,12 +261,12 @@ impl<B: NewBitmap> MmapRegion<B> {
261261
///
262262
/// # Arguments
263263
/// * `file_offset` - if provided, the method will create a file mapping at offset
264-
/// `file_offset.start` in the file referred to by `file_offset.file`.
264+
/// `file_offset.start` in the file referred to by `file_offset.file`.
265265
/// * `size` - The size of the memory region in bytes.
266266
/// * `prot` - The desired memory protection of the mapping.
267267
/// * `flags` - This argument determines whether updates to the mapping are visible to other
268-
/// processes mapping the same region, and whether updates are carried through to
269-
/// the underlying file.
268+
/// processes mapping the same region, and whether updates are carried through to
269+
/// the underlying file.
270270
pub fn build(
271271
file_offset: Option<FileOffset>,
272272
size: usize,
@@ -293,7 +293,7 @@ impl<B: NewBitmap> MmapRegion<B> {
293293
/// * `size` - The size of the memory region in bytes.
294294
/// * `prot` - Must correspond to the memory protection attributes of the existing mapping.
295295
/// * `flags` - Must correspond to the flags that were passed to `mmap` for the creation of
296-
/// the existing mapping.
296+
/// the existing mapping.
297297
///
298298
/// # Safety
299299
///
@@ -587,9 +587,7 @@ mod tests {
587587
assert!(r.owned());
588588

589589
let region_size = 0x10_0000;
590-
let bitmap = AtomicBitmap::new(region_size, unsafe {
591-
std::num::NonZeroUsize::new_unchecked(0x1000)
592-
});
590+
let bitmap = AtomicBitmap::new(region_size, std::num::NonZeroUsize::new(0x1000).unwrap());
593591
let builder = MmapRegionBuilder::new_with_bitmap(region_size, bitmap)
594592
.with_hugetlbfs(true)
595593
.with_mmap_prot(libc::PROT_READ | libc::PROT_WRITE);

src/region.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ impl<R: GuestMemoryRegion> GuestRegionCollection<R> {
219219
/// # Arguments
220220
///
221221
/// * `regions` - The vector of regions.
222-
/// The regions shouldn't overlap, and they should be sorted
223-
/// by the starting address.
222+
/// The regions shouldn't overlap, and they should be sorted
223+
/// by the starting address.
224224
pub fn from_regions(
225225
mut regions: Vec<R>,
226226
) -> std::result::Result<Self, GuestRegionCollectionError> {
@@ -237,8 +237,8 @@ impl<R: GuestMemoryRegion> GuestRegionCollection<R> {
237237
/// # Arguments
238238
///
239239
/// * `regions` - The vector of `Arc` regions.
240-
/// The regions shouldn't overlap and they should be sorted
241-
/// by the starting address.
240+
/// The regions shouldn't overlap and they should be sorted
241+
/// by the starting address.
242242
pub fn from_arc_regions(
243243
regions: Vec<Arc<R>>,
244244
) -> std::result::Result<Self, GuestRegionCollectionError> {

src/volatile_memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ mod tests {
14691469
use crate::bitmap::{AtomicBitmap, RefSlice};
14701470

14711471
#[cfg(feature = "backend-bitmap")]
1472-
const DEFAULT_PAGE_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(0x1000) };
1472+
const DEFAULT_PAGE_SIZE: NonZeroUsize = NonZeroUsize::new(0x1000).unwrap();
14731473

14741474
#[test]
14751475
fn test_compute_end_offset() {

0 commit comments

Comments
 (0)