pub enum Error {
Unix {
source: Errno,
},
UnixIoctl(i32),
FS {
path: String,
source: Error,
},
IO {
source: Error,
},
InvalidQueue(usize),
InvalidNumQueues,
InvalidName {
name: String,
max_size: usize,
},
}
Expand description
Represents store errors based on user configuration or general operations.
Variants§
Unix
Internal raw unix error.
UnixIoctl(i32)
Invalid ioctl return code encountered.
FS
Internal file I/O errors encountered.
Fields
IO
Internal generic I/O errors encountered.
InvalidQueue(usize)
The specified queue descriptor is invalid.
InvalidNumQueues
The specified number of queues was less than or equal to 0.
InvalidName
The specified device name is invalid.
Implementations§
Source§impl Error
impl Error
Sourcepub fn into_io(self) -> Error
pub fn into_io(self) -> Error
Consume this error and return the equivalent std::io::Error.
Examples found in repository?
examples/mio.rs (line 16)
14fn main() -> io::Result<()> {
15 // Create a new synchronous Tun named `rip%d` with NUM_QUEUES internal queues.
16 let mut sync = Tun::new("rip%d", NUM_QUEUES).map_err(|err| err.into_io())?;
17
18 // Print out the OS given name of this device.
19 println!("[INFO] => Created new virtual device: {}", sync.name());
20
21 // Create a new Poll instance so that we can listen for events on our Tun Queues.
22 let mut poll = Poll::new()?;
23
24 // Drain the Tun instance of its internal Queue instances, taking ownership and allowing
25 // for full lifecycle management. This allows us to feed the Queue's into the mio registry
26 // and handle the Token > Queue mapping.
27 let mut queues: HashMap<Token, Queue> = HashMap::with_capacity(NUM_QUEUES);
28 for (idx, mut queue) in sync.drain(..).enumerate() {
29 // Ensure the Queue's underlying file descriptor is in non-blocking mode.
30 queue.set_non_blocking(true).map_err(|err| err.into_io())?;
31
32 // Create this queue's token, register, and then map this queue to that
33 // generated token.
34 let token = Token(idx);
35 poll.registry()
36 .register(&mut queue, token, Interest::READABLE)?;
37 queues.insert(token, queue);
38 }
39
40 // Create the event storage and start the event loop.
41 let mut events = Events::with_capacity(NUM_QUEUES);
42
43 // Create a buffer the same size as the MTU as the Tun device, which by default
44 // is 1500 Bytes.
45 let mut buffer: [u8; 1500] = [0x00; 1500];
46 loop {
47 // Poll Mio for events, blocking until we get an event or 500ms elapses.
48 poll.poll(&mut events, Some(Duration::from_millis(500)))?;
49
50 // Lets short circuit if we don't have any events.
51 if events.is_empty() {
52 continue;
53 }
54
55 // Process each event.
56 for event in events.iter() {
57 // For the purposes of this example we are only interested in read insterests.
58 if event.is_readable() {
59 // Grab the queue from our map so we can use it to read the next packet.
60 let queue = queues.get(&event.token()).unwrap();
61
62 // Actually call recv on the Queue itself, which will return the number
63 // of bytes actually read into the supplied buffer.
64 let read = match queue.recv(&mut buffer) {
65 Ok(read) => read,
66 // If its a WouldBlock simply short circuit, and re-poll the Queues.
67 Err(err) if err.kind() == ErrorKind::WouldBlock => continue,
68 Err(err) => {
69 // If we error simply log the issue, and continue on business as usual.
70 println!("[ERROR][Queue: {:?}] => {}", event.token(), err);
71 continue;
72 }
73 };
74
75 // Print the packet data as a byte slice, noting its size and which queue the
76 // packet came from.
77 println!(
78 "[INFO][Queue: {:?}] => Packet data ({}B): {:?}",
79 event.token(),
80 read,
81 &buffer[..read]
82 );
83 }
84 }
85 }
86}
Trait Implementations§
Source§impl Error for Error
impl Error for Error
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
Auto Trait Implementations§
impl Freeze for Error
impl !RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl !UnwindSafe for Error
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more