Skip to main content

ConnectionEmbedded

Struct ConnectionEmbedded 

Source
pub struct ConnectionEmbedded<T>
where T: Read + Write + ReadReady,
{ /* private fields */ }
Expand description

Contains an instance of T with Read, Write and ReadReady. We implement Connection for this rather than T directly to avoid issues with conflicting implementations of external types T that could in future add Read, Write and ReadReady

Implementations§

Source§

impl<T> ConnectionEmbedded<T>
where T: Read + Write + ReadReady,

Source

pub fn new(inner: T) -> Self

Create a new adapter

Source

pub fn into_inner(self) -> T

Consume the adapter, returning the inner object.

Source

pub fn inner(&self) -> &T

Borrow the inner object.

Source

pub fn inner_mut(&mut self) -> &mut T

Mutably borrow the inner object.

Trait Implementations§

Source§

impl<T> Connection for ConnectionEmbedded<T>
where T: Read + Write + ReadReady,

Source§

async fn send(&mut self, buf: &[u8]) -> Result<(), PacketWriteError>

Source§

async fn receive(&mut self, buf: &mut [u8]) -> Result<(), PacketReadError>

Source§

async fn receive_if_ready( &mut self, buf: &mut [u8], ) -> Result<bool, PacketReadError>

If no data at all is ready, then return immediately with Ok(false), leaving the underlying stream and buf unaltered. If any data is ready then receive into buffer, waiting to fill it, then return Ok(true). Note that this method can still await data, because it is only required to return Ok(false) in the case where no data at all is ready. If less data is available than buf.len(), the method may still proceed, reading the available data and then waiting for more to fill buf. This is fairly well suited to MQTT packets - first call receive_if_ready with a buf of length 1 - if this returns Ok(false) then sleep for a reasonable interval and try again. If it returns Ok(true), continue reading a whole packet using receive, using larger buffers if possible. While this may lead to an indefinite await for the rest of the packet, this should only occur in the case of a malicious server or poor connection, not in the most common case where there is a long gap between incoming packets, but once the first byte of a packet is received the rest is received quickly afterwards. This approach is used to make it easier to use a variety of underlying streams, since we only need something like embedded-async’s ReadReady trait, or tokio’s TCPStream.try_read More sophisticated approaches are definitely possible.

Auto Trait Implementations§

§

impl<T> Freeze for ConnectionEmbedded<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for ConnectionEmbedded<T>
where T: RefUnwindSafe,

§

impl<T> Send for ConnectionEmbedded<T>
where T: Send,

§

impl<T> Sync for ConnectionEmbedded<T>
where T: Sync,

§

impl<T> Unpin for ConnectionEmbedded<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for ConnectionEmbedded<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for ConnectionEmbedded<T>
where T: UnwindSafe,

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