Skip to main content

Session

Struct Session 

Source
pub struct Session<D: SerialDevice + 'static> { /* private fields */ }
Expand description

Owns a serial device and a bus, and runs the I/O + command loop.

Session is generic over the device type so tests can substitute a PTY pair (SerialPortDevice::pair) or, in the future, a fully mocked backend without dynamic dispatch overhead.

Implementations§

Source§

impl<D: SerialDevice + 'static> Session<D>

Source

pub fn new(device: D) -> Self

Builds a session with a fresh bus and cancellation token, no-op mappers on both directions.

Source

pub fn with_bus(device: D, bus: EventBus) -> Self

Builds a session attached to a caller-supplied bus. Useful when several subsystems already share a bus and the session should join the existing fan-out instead of starting its own.

Source

pub fn with_omap<M: Mapper + 'static>(self, mapper: M) -> Self

Replaces the outbound mapper applied to Event::TxBytes payloads before they reach the device.

Source

pub fn with_imap<M: Mapper + 'static>(self, mapper: M) -> Self

Replaces the inbound mapper applied to bytes read from the device before they are republished as Event::RxBytes.

Source

pub const fn with_initial_dtr(self, asserted: bool) -> Self

Tells the session what the DTR line’s actual state is on the device. Use this when the caller has already issued a set_dtr (e.g. main applying --lower-dtr right after opening the port) so the cached state stays honest and the first Command::ToggleDtr produces the right transition.

Defaults to true (asserted) — the typical OS state at open.

Source

pub const fn with_initial_rts(self, asserted: bool) -> Self

Tells the session what the RTS line’s actual state is. See with_initial_dtr for the rationale.

Source

pub const fn bus(&self) -> &EventBus

Returns a reference to the bus. Clone it before calling Session::run (which consumes self) if you need to publish or subscribe from outside the session.

Source

pub fn cancellation_token(&self) -> CancellationToken

Returns a clone of the cancellation token.

Triggering CancellationToken::cancel on any clone causes Session::run to wind down and return.

Source

pub async fn run(self) -> Result<()>

Drives the session to completion.

Subscribes to the bus, publishes Event::DeviceConnected, then loops until the cancellation token trips or a fatal I/O error terminates the device.

§Errors

Currently always returns Ok(()); the variant is reserved for startup failures introduced by later issues (e.g. mapper initialisation).

Source

pub async fn apply_config(&mut self, new: SerialConfig) -> Result<()>

Apply a new SerialConfig to the device atomically.

Applies baud_rate → data_bits → stop_bits → parity → flow_control in that fixed order. On the first failing step, best-effort-rolls back the previously-applied steps to the configuration that was live at entry, returns the Error from the failing step, and does not publish Event::ConfigChanged. On full success, publishes Event::ConfigChanged with the device’s post-apply configuration and returns Ok(()).

Fields whose new value equals the current value still go through the setter call — the backend is free to short-circuit, and keeping the apply sequence uniform avoids branchy rollback state.

This method is async for forward compatibility with backends whose setters may need to await (e.g. remote devices); the current serialport backend is synchronous so the body performs no awaits.

§Errors

Returns the first setter failure encountered. Rollback failures are best-effort and silently swallowed — the device is already in an inconsistent state by that point and surfacing a secondary error would mask the original cause.

Auto Trait Implementations§

§

impl<D> Freeze for Session<D>
where D: Freeze,

§

impl<D> !RefUnwindSafe for Session<D>

§

impl<D> Send for Session<D>

§

impl<D> !Sync for Session<D>

§

impl<D> Unpin for Session<D>

§

impl<D> UnsafeUnpin for Session<D>
where D: UnsafeUnpin,

§

impl<D> !UnwindSafe for Session<D>

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

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more