document the cvt methods · rust-lang/rust@5c46aca (original) (raw)
File tree
1 file changed
lines changed
- library/std/src/sys/pal/unix
1 file changed
lines changed
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -307,10 +307,13 @@ macro_rules! impl_is_minus_one { | ||
| 307 | 307 | |
| 308 | 308 | impl_is_minus_one! { i8 i16 i32 i64 isize } |
| 309 | 309 | |
| 310 | +/// Convert native return values to Result using the *-1 means error is in `errno`* convention. | |
| 311 | +/// Non-error values are `Ok`-wrapped. | |
| 310 | 312 | pub fn cvt<T: IsMinusOne>(t: T) -> crate::io::Result<T> { |
| 311 | 313 | if t.is_minus_one() { Err(crate::io::Error::last_os_error()) } else { Ok(t) } |
| 312 | 314 | } |
| 313 | 315 | |
| 316 | +/// `-1` → look at `errno` → retry on `EINTR`. Otherwise `Ok()`-wrap the closure return value. | |
| 314 | 317 | pub fn cvt_r<T, F>(mut f: F) -> crate::io::Result<T> |
| 315 | 318 | where |
| 316 | 319 | T: IsMinusOne, |
| @@ -325,6 +328,7 @@ where | ||
| 325 | 328 | } |
| 326 | 329 | |
| 327 | 330 | #[allow(dead_code)] // Not used on all platforms. |
| 331 | +/// Zero means `Ok()`, all other values are treated as raw OS errors. Does not look at `errno`. | |
| 328 | 332 | pub fn cvt_nz(error: libc::c_int) -> crate::io::Result<()> { |
| 329 | 333 | if error == 0 { Ok(()) } else { Err(crate::io::Error::from_raw_os_error(error)) } |
| 330 | 334 | } |