|
| 1 | +//! Fixing errors related to the `Buffer` trait. |
| 2 | +//! |
| 3 | +//! This example demonstrates some error messages that can come from using the |
| 4 | +//! `Buffer` trait and how to fix them. |
| 5 | +
|
| 6 | +fn main() { |
| 7 | + error_buffer_wrapper(); |
| 8 | + error_retry_closure(); |
| 9 | + error_retry_indirect_closure(); |
| 10 | + error_empty_slice(); |
| 11 | + error_array_by_value(); |
| 12 | +} |
| 13 | + |
| 14 | +fn error_buffer_wrapper() { |
| 15 | + use rustix::buffer::Buffer; |
| 16 | + |
| 17 | + fn read<B: Buffer<u8>>(_: B) {} |
| 18 | + |
| 19 | + // This is reduced from src/fs/inotify.rs line 177. |
| 20 | + struct Wrapper<'a>(&'a mut [u8]); |
| 21 | + impl<'a> Wrapper<'a> { |
| 22 | + fn read(&mut self) { |
| 23 | + // Ideally we'd write this, but it gets: |
| 24 | + // "cannot move out of `self` which is behind a mutable reference". |
| 25 | + /* |
| 26 | + read(self.0); |
| 27 | + */ |
| 28 | + |
| 29 | + // The fix: add `&mut *`. |
| 30 | + read(&mut *self.0); |
| 31 | + } |
| 32 | + } |
| 33 | + let mut buf = vec![0_u8; 3]; |
| 34 | + let mut wrapper = Wrapper(&mut buf); |
| 35 | + wrapper.read(); |
| 36 | +} |
| 37 | + |
| 38 | +fn error_retry_closure() { |
| 39 | + use rustix::buffer::Buffer; |
| 40 | + use rustix::io; |
| 41 | + use rustix::io::retry_on_intr; |
| 42 | + |
| 43 | + fn b<B: Buffer<u8>>(_: B) -> io::Result<()> { |
| 44 | + Ok(()) |
| 45 | + } |
| 46 | + |
| 47 | + let mut event_buf = [0; 4]; |
| 48 | + |
| 49 | + // Ideally we'd write this, but it gets: |
| 50 | + // "cannot move out of `event_buf`, a captured variable in an `FnMut` closure". |
| 51 | + /* |
| 52 | + let event_buf_slice = event_buf.as_mut_slice(); |
| 53 | + retry_on_intr(|| b(event_buf_slice)).unwrap(); |
| 54 | + */ |
| 55 | + |
| 56 | + // The fix: Add `&mut *`. |
| 57 | + let event_buf_slice = event_buf.as_mut_slice(); |
| 58 | + retry_on_intr(|| b(&mut *event_buf_slice)).unwrap(); |
| 59 | +} |
| 60 | + |
| 61 | +fn error_retry_indirect_closure() { |
| 62 | + use rustix::buffer::Buffer; |
| 63 | + use rustix::io; |
| 64 | + let flag = true; |
| 65 | + |
| 66 | + // This is reduced from the xattr crate, src/sys/linux_macos.rs line 119. |
| 67 | + |
| 68 | + // Ideally we'd write this, but it gets: |
| 69 | + // "borrowed data escapes outside of closure". |
| 70 | + /* |
| 71 | + let func = if flag { |
| 72 | + f |
| 73 | + } else { |
| 74 | + g |
| 75 | + }; |
| 76 | + let _vec = allocate_loop(|buf| func(buf)).unwrap(); |
| 77 | + */ |
| 78 | + |
| 79 | + // The fix: Move `func` to inside the closure. |
| 80 | + let _vec = allocate_loop(|buf| { |
| 81 | + let func = if flag { f } else { g }; |
| 82 | + func(buf) |
| 83 | + }) |
| 84 | + .unwrap(); |
| 85 | + |
| 86 | + fn allocate_loop<F: FnMut(&mut [u8]) -> io::Result<usize>>(mut f: F) -> io::Result<Vec<u8>> { |
| 87 | + let mut vec: Vec<u8> = Vec::new(); |
| 88 | + loop { |
| 89 | + let ret = f(&mut [])?; |
| 90 | + vec.resize(ret, 0); |
| 91 | + |
| 92 | + match f(&mut vec) { |
| 93 | + Ok(size) => { |
| 94 | + vec.truncate(size); |
| 95 | + vec.shrink_to_fit(); |
| 96 | + return Ok(vec); |
| 97 | + } |
| 98 | + Err(err) => return Err(err), |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + fn f<B: Buffer<u8>>(_: B) -> Result<usize, io::Errno> { |
| 103 | + Ok(0) |
| 104 | + } |
| 105 | + fn g<B: Buffer<u8>>(_: B) -> Result<usize, io::Errno> { |
| 106 | + Ok(0) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +fn error_empty_slice() { |
| 111 | + use rustix::buffer::Buffer; |
| 112 | + |
| 113 | + fn read<B: Buffer<u8>>(_: B) {} |
| 114 | + |
| 115 | + // Functions that take `&mut [u8]` can be passed `&mut []`, but with |
| 116 | + // `Buffer` passing `&mut []` gets: |
| 117 | + // "type annotations needed". |
| 118 | + /* |
| 119 | + read(&mut []); |
| 120 | + */ |
| 121 | + |
| 122 | + // The fix: make the element type explicit. |
| 123 | + read(&mut [0_u8; 0]); |
| 124 | +} |
| 125 | + |
| 126 | +fn error_array_by_value() { |
| 127 | + use rustix::buffer::Buffer; |
| 128 | + |
| 129 | + fn read<B: Buffer<u8>>(b: B) -> B::Output { |
| 130 | + unsafe { b.assume_init(0) } |
| 131 | + } |
| 132 | + |
| 133 | + // This code is erroneously attempts to pass a buffer by value, but it |
| 134 | + // confusingly gets two error messages: |
| 135 | + // "the trait bound `[{integer}; 3]: Buffer<u8>` is not satisfied", and |
| 136 | + // "the trait bound `[{integer}; 3]: buffer::private::Sealed<u8>` is not satisfied". |
| 137 | + /* |
| 138 | + let mut buf = [0, 0, 0]; |
| 139 | + read(buf); |
| 140 | + */ |
| 141 | + |
| 142 | + // The fix: pass the buffer by reference. |
| 143 | + let mut buf = [0, 0, 0]; |
| 144 | + read(&mut buf); |
| 145 | +} |
0 commit comments