Skip to content

Commit f3570ae

Browse files
kornelskigitbot
authored and
gitbot
committed
Add safe new to NotAllOnes
1 parent 29ef048 commit f3570ae

File tree

5 files changed

+24
-29
lines changed

5 files changed

+24
-29
lines changed

core/src/num/niche_types.rs

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ macro_rules! define_valid_range_type {
3232
};
3333

3434
impl $name {
35+
#[inline]
36+
pub const fn new(val: $int) -> Option<Self> {
37+
if (val as $uint) >= ($low as $uint) && (val as $uint) <= ($high as $uint) {
38+
// SAFETY: just checked the inclusive range
39+
Some(unsafe { $name(val) })
40+
} else {
41+
None
42+
}
43+
}
44+
3545
/// Constructs an instance of this type from the underlying integer
3646
/// primitive without checking whether its zero.
3747
///

std/src/os/fd/owned.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,11 @@ impl BorrowedFd<'_> {
6767
/// The resource pointed to by `fd` must remain open for the duration of
6868
/// the returned `BorrowedFd`, and it must not have the value `-1`.
6969
#[inline]
70+
#[track_caller]
7071
#[rustc_const_stable(feature = "io_safety", since = "1.63.0")]
7172
#[stable(feature = "io_safety", since = "1.63.0")]
7273
pub const unsafe fn borrow_raw(fd: RawFd) -> Self {
73-
assert!(fd != u32::MAX as RawFd);
74-
// SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
75-
let fd = unsafe { ValidRawFd::new_unchecked(fd) };
76-
Self { fd, _phantom: PhantomData }
74+
Self { fd: ValidRawFd::new(fd).expect("fd != -1"), _phantom: PhantomData }
7775
}
7876
}
7977

@@ -154,11 +152,9 @@ impl FromRawFd for OwnedFd {
154152
///
155153
/// [io-safety]: io#io-safety
156154
#[inline]
155+
#[track_caller]
157156
unsafe fn from_raw_fd(fd: RawFd) -> Self {
158-
assert_ne!(fd, u32::MAX as RawFd);
159-
// SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
160-
let fd = unsafe { ValidRawFd::new_unchecked(fd) };
161-
Self { fd }
157+
Self { fd: ValidRawFd::new(fd).expect("fd != -1") }
162158
}
163159
}
164160

std/src/os/solid/io.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,9 @@ impl BorrowedFd<'_> {
101101
/// the returned `BorrowedFd`, and it must not have the value
102102
/// `SOLID_NET_INVALID_FD`.
103103
#[inline]
104+
#[track_caller]
104105
pub const unsafe fn borrow_raw(fd: RawFd) -> Self {
105-
assert!(fd != -1 as RawFd);
106-
// SAFETY: we just asserted that the value is in the valid range and
107-
// isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
108-
let fd = unsafe { ValidRawFd::new_unchecked(fd) };
109-
Self { fd, _phantom: PhantomData }
106+
Self { fd: ValidRawFd::new(fd).expect("fd != -1"), _phantom: PhantomData }
110107
}
111108
}
112109

@@ -156,12 +153,9 @@ impl FromRawFd for OwnedFd {
156153
/// The resource pointed to by `fd` must be open and suitable for assuming
157154
/// ownership. The resource must not require any cleanup other than `close`.
158155
#[inline]
156+
#[track_caller]
159157
unsafe fn from_raw_fd(fd: RawFd) -> Self {
160-
assert_ne!(fd, -1 as RawFd);
161-
// SAFETY: we just asserted that the value is in the valid range and
162-
// isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
163-
let fd = unsafe { ValidRawFd::new_unchecked(fd) };
164-
Self { fd }
158+
Self { fd: ValidRawFd::new(fd).expect("fd != -1") }
165159
}
166160
}
167161

std/src/os/windows/io/socket.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,11 @@ impl BorrowedSocket<'_> {
5858
/// the returned `BorrowedSocket`, and it must not have the value
5959
/// `INVALID_SOCKET`.
6060
#[inline]
61+
#[track_caller]
6162
#[rustc_const_stable(feature = "io_safety", since = "1.63.0")]
6263
#[stable(feature = "io_safety", since = "1.63.0")]
6364
pub const unsafe fn borrow_raw(socket: RawSocket) -> Self {
64-
assert!(socket != sys::c::INVALID_SOCKET as RawSocket);
65-
let socket = unsafe { ValidRawSocket::new_unchecked(socket) };
66-
Self { socket, _phantom: PhantomData }
65+
Self { socket: ValidRawSocket::new(socket).expect("socket != -1"), _phantom: PhantomData }
6766
}
6867
}
6968

@@ -185,10 +184,9 @@ impl IntoRawSocket for OwnedSocket {
185184
#[stable(feature = "io_safety", since = "1.63.0")]
186185
impl FromRawSocket for OwnedSocket {
187186
#[inline]
187+
#[track_caller]
188188
unsafe fn from_raw_socket(socket: RawSocket) -> Self {
189-
debug_assert_ne!(socket, sys::c::INVALID_SOCKET as RawSocket);
190-
let socket = unsafe { ValidRawSocket::new_unchecked(socket) };
191-
Self { socket }
189+
Self { socket: ValidRawSocket::new(socket).expect("socket != -1") }
192190
}
193191
}
194192

std/src/sys/pal/solid/fs.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@ struct FileDesc {
2222

2323
impl FileDesc {
2424
#[inline]
25+
#[track_caller]
2526
fn new(fd: c_int) -> FileDesc {
26-
assert_ne!(fd, -1i32);
27-
// Safety: we just asserted that the value is in the valid range and
28-
// isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
29-
let fd = unsafe { CIntNotMinusOne::new_unchecked(fd) };
30-
FileDesc { fd }
27+
FileDesc { fd: CIntNotMinusOne::new(fd).expect("fd != -1") }
3128
}
3229

3330
#[inline]

0 commit comments

Comments
 (0)