TransportError

Struct TransportError 

pub struct TransportError { /* private fields */ }
Expand description

Transport layer error.

This is a type representing failure in transport layer of SecureSessionTransport, namely its send_data and receive_data methods.

§Examples

TransportError can conveniently wrap any other error using ? operator. You can also explicitly construct an error with a descriptive string.

use std::io::{Read, Write};
use std::net::TcpStream;

use themis::secure_session::{SecureSessionTransport, TransportError};

struct SocketTransport {
    socket: TcpStream,
}

impl SecureSessionTransport for SocketTransport {
    fn send_data(&mut self, data: &[u8]) -> Result<usize, TransportError> {
        if data.len() >= 256 {
            return Err(TransportError::new(format!("too long data: {} bytes", data.len())));
        }

        let len_buffer = [data.len() as u8];
        self.socket.write_all(&len_buffer)?;

        self.socket.write_all(data)?;
        Ok(data.len())
    }

    fn receive_data(&mut self, data: &mut [u8]) -> Result<usize, TransportError> {
        let mut len_buffer = [0];
        self.socket.read_exact(&mut len_buffer)?;

        let len = len_buffer[0] as usize;
        if data.len() < len {
            return Err(TransportError::new("buffer too short"));
        }

        self.socket.read_exact(&mut data[0..len])?;
        Ok(len)
    }

    // Other methods omitted
}

Implementations§

§

impl TransportError

pub fn new(description: impl Into<String>) -> TransportError

Returns a new error with a human-readable description.

pub fn unspecified() -> TransportError

Returns an unspecified error.

Trait Implementations§

§

impl Debug for TransportError

§

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

Formats the value using the given formatter. Read more
§

impl Display for TransportError

§

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

Formats the value using the given formatter. Read more
§

impl<T> From<T> for TransportError
where T: Error + Send + Sync + 'static,

§

fn from(error: T) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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.