1 | use std::io; |
2 | |
3 | pub fn extract_noattr(result: io::Result<Vec<u8>>) -> io::Result<Option<Vec<u8>>> { |
4 | result.map(Some).or_else(|e: Error| { |
5 | if e.raw_os_error() == Some(crate::sys::ENOATTR) { |
6 | Ok(None) |
7 | } else { |
8 | Err(e) |
9 | } |
10 | }) |
11 | } |
12 | |
13 | #[allow (dead_code)] |
14 | pub fn allocate_loop<E, F: FnMut(&mut [u8]) -> Result<usize, E>>(mut f: F) -> io::Result<Vec<u8>> |
15 | where |
16 | io::Error: From<E>, |
17 | { |
18 | const ERANGE: i32 = rustix::io::Errno::RANGE.raw_os_error(); |
19 | |
20 | let mut vec: Vec<u8> = Vec::new(); |
21 | loop { |
22 | let ret = f(&mut [])?; |
23 | vec.resize(ret, 0); |
24 | |
25 | match f(&mut vec) { |
26 | Ok(size) => { |
27 | vec.truncate(size); |
28 | vec.shrink_to_fit(); |
29 | return Ok(vec); |
30 | } |
31 | |
32 | Err(e) => { |
33 | let err: io::Error = e.into(); |
34 | if err.raw_os_error() == Some(ERANGE) { |
35 | continue; |
36 | } else { |
37 | return Err(err); |
38 | } |
39 | } |
40 | } |
41 | } |
42 | } |
43 | |