This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +24
-29
lines changed Expand file tree Collapse file tree 5 files changed +24
-29
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,16 @@ macro_rules! define_valid_range_type {
32
32
};
33
33
34
34
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
+
35
45
/// Constructs an instance of this type from the underlying integer
36
46
/// primitive without checking whether its zero.
37
47
///
Original file line number Diff line number Diff line change @@ -67,13 +67,11 @@ impl BorrowedFd<'_> {
67
67
/// The resource pointed to by `fd` must remain open for the duration of
68
68
/// the returned `BorrowedFd`, and it must not have the value `-1`.
69
69
#[inline]
70
+ #[track_caller]
70
71
#[rustc_const_stable(feature = "io_safety", since = "1.63.0")]
71
72
#[stable(feature = "io_safety", since = "1.63.0")]
72
73
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 }
77
75
}
78
76
}
79
77
@@ -154,11 +152,9 @@ impl FromRawFd for OwnedFd {
154
152
///
155
153
/// [io-safety]: io#io-safety
156
154
#[inline]
155
+ #[track_caller]
157
156
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") }
162
158
}
163
159
}
164
160
Original file line number Diff line number Diff line change @@ -101,12 +101,9 @@ impl BorrowedFd<'_> {
101
101
/// the returned `BorrowedFd`, and it must not have the value
102
102
/// `SOLID_NET_INVALID_FD`.
103
103
#[inline]
104
+ #[track_caller]
104
105
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 }
110
107
}
111
108
}
112
109
@@ -156,12 +153,9 @@ impl FromRawFd for OwnedFd {
156
153
/// The resource pointed to by `fd` must be open and suitable for assuming
157
154
/// ownership. The resource must not require any cleanup other than `close`.
158
155
#[inline]
156
+ #[track_caller]
159
157
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") }
165
159
}
166
160
}
167
161
Original file line number Diff line number Diff line change @@ -58,12 +58,11 @@ impl BorrowedSocket<'_> {
58
58
/// the returned `BorrowedSocket`, and it must not have the value
59
59
/// `INVALID_SOCKET`.
60
60
#[inline]
61
+ #[track_caller]
61
62
#[rustc_const_stable(feature = "io_safety", since = "1.63.0")]
62
63
#[stable(feature = "io_safety", since = "1.63.0")]
63
64
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 }
67
66
}
68
67
}
69
68
@@ -185,10 +184,9 @@ impl IntoRawSocket for OwnedSocket {
185
184
#[stable(feature = "io_safety", since = "1.63.0")]
186
185
impl FromRawSocket for OwnedSocket {
187
186
#[inline]
187
+ #[track_caller]
188
188
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") }
192
190
}
193
191
}
194
192
Original file line number Diff line number Diff line change @@ -22,12 +22,9 @@ struct FileDesc {
22
22
23
23
impl FileDesc {
24
24
#[inline]
25
+ #[track_caller]
25
26
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") }
31
28
}
32
29
33
30
#[inline]
You can’t perform that action at this time.
0 commit comments