Enum Error

Source
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.

Fields

§source: Errno

The internal unix error encountered.

§

UnixIoctl(i32)

Invalid ioctl return code encountered.

§

FS

Internal file I/O errors encountered.

Fields

§path: String

The path associated with this IO error.

§source: Error

The underlying IO error kind.

§

IO

Internal generic I/O errors encountered.

Fields

§source: Error

The internal IO error that occured.

§

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.

Fields

§name: String

The supplied name.

§max_size: usize

The max size of the name.

Implementations§

Source§

impl Error

Source

pub fn errno() -> Self

Returns the last errno observed on unix systems.

Source

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 Debug for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Error

Source§

fn fmt(&self, __formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for Error

Source§

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

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<Errno> for Error

Source§

fn from(e: Errno) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for Error

Source§

fn from(source: Error) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Error

Source§

fn from(i: i32) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for Error

Source§

fn from(i: usize) -> Self

Converts to this type from the input type.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.