Skip to main content

IoRef

Struct IoRef 

Source
pub struct IoRef(/* private fields */);
Expand description

A cheap, cloneable handle to an Io connection.

All clones point to the same connection. Changes to its buffers, configuration, timers, errors, or shutdown state are visible through every clone. Cloning IoRef never clones the socket.

Use it to inspect the connection, access its buffers, queue output, or start graceful or immediate shutdown. These methods are also available directly on Io, which dereferences to IoRef.

Keeping an IoRef alive does not keep the connection open after its Io owner is dropped. Like Io, it stays on the local runtime thread and is neither Send nor Sync.

Implementations§

Source§

impl IoRef

Source

pub fn id(&self) -> Id

Gets the ID.

Source

pub fn tag(&self) -> &'static str

Gets the I/O tag.

Source

pub fn cfg(&self) -> &IoConfig

Gets the configuration.

Source

pub fn shared(&self) -> SharedCfg

Gets the shared configuration.

Source

pub fn is_active(&self) -> bool

Checks whether the I/O stream is active.

This becomes false as soon as the connection leaves its active state, whether it was closed locally, force-terminated, or the transport reported the peer as gone. Closing is not instantaneous, so buffered output may still be flushing and buffered input stays readable after this goes false; use is_closed to ask whether closing has finished.

Source

pub fn is_read_eof(&self) -> bool

Checks whether the transport read half reached clean EOF.

Buffered input remains available and the write half may still be used.

Source

pub fn is_closed(&self) -> bool

Checks whether the I/O stream is closed.

This becomes true once the backend released the underlying socket and transport teardown has finished, so nothing further can be read from or delivered to the peer. Every way a connection can end reaches this state, whether it closed gracefully, was force-terminated or the peer disappeared. Use is_active to also cover a close that is still in progress.

Buffered input that was already received stays readable.

Source

pub fn is_rd_backpressure(&self) -> bool

Checks whether read back-pressure is enabled.

This becomes true once unread data in the application-facing read buffer reaches the configured high watermark, which parks the transport read task.

Two different paths release it. Consuming through decode, with_buf, with_read_src or with_read_dst releases it once the buffer has fallen to at most half the high watermark. Asking for more input through Io::poll_read_more, and the methods built on it, releases it immediately however much data is still buffered.

Source

pub fn is_wr_backpressure(&self) -> bool

Checks whether write back-pressure is enabled.

This becomes true once outstanding output reaches the configured high watermark. Outstanding output includes buffered data and data that the transport owns but has not yet written to the peer. It is released once the outstanding size falls to half the high watermark.

Nothing enforces the signal: encoding continues to succeed while it is set. Producers that are not driven by a dispatcher should check this before encoding more, or the write buffer grows without bound. See encode and write_ready.

Source

pub fn is_read_filter_paused(&self) -> bool

Checks whether transport reads are paused by the filter chain.

This is true while a filter is not ready for transport reads although the io state allows them, for example a filter that waits for its own resources. No input arrives during the pause, it is not caused by the peer, so read timeouts should not run. The dispatcher is notified when the pause starts and when it ends.

Source

pub fn is_write_filter_paused(&self) -> bool

Checks whether transport writes are paused by the filter chain.

This is true while buffered output is waiting for a filter that is not ready for transport writes. Output does not drain during the pause, it is not caused by the peer, so write timeouts should not run. The dispatcher is notified when the pause starts and when it ends.

Source

pub async fn write_ready(&self) -> Result<(), Error>

Waits until the write buffer can accept more output.

Completes immediately unless write back-pressure is enabled. While it is, waits until the outstanding output falls to the release threshold (half of the high watermark), the level at which the dispatcher releases back-pressure. Any number of tasks can wait at once, the write task wakes them without involving the dispatcher.

Producers that are not driven by a dispatcher can await this before encoding more, so the write buffer does not grow without bound.

Fails once the connection is closing or closed, and with io::ErrorKind::TimedOut if the write timeout is set and expires first.

Source

pub fn close(&self)

Gracefully closes the connection.

Initiates the I/O stream shutdown process.

Source

pub fn terminate(&self)

Force-closes the connection.

The dispatcher does not wait for incomplete responses. The I/O stream is terminated without any graceful period, and whatever is still buffered is discarded.

The transport aborts the connection instead of closing it gracefully, so the peer most likely observes an RST rather than a clean end of stream, and output that has not been acknowledged yet is lost. That is what keeps a truncated response distinguishable from a complete one, but it also means this must not be used to end a connection normally. Use close for that.

Source

pub fn query<T>(&self) -> QueryItem<T>
where T: 'static,

Queries filter-specific data.

Source

pub fn encode<U>( &self, item: <U as Encoder>::Item, codec: &U, ) -> Result<(), <U as Encoder>::Error>
where U: Encoder,

Encodes an item into the write buffer.

This method reports codec errors only. Any io::Error produced while buffering is discarded: if the connection is already closing or closed the item is not encoded, and a transport or filter error raised by an eager backend write is dropped. Such errors remain observable later through crate::Io::poll_flush or crate::Io::poll_recv. Use encode_slice or encode_bytes when they must be observed at the call site.

§Back-pressure is advisory

Encoding never blocks and never refuses. Once buffered output reaches the configured high watermark this arms write back-pressure and wakes the dispatch task, but the item is still buffered and Ok is still returned. A caller that keeps encoding without consulting is_wr_backpressure, or awaiting Io::poll_status_update or Io::poll_flush, will grow the write buffer without bound, because a slow peer cannot slow the producer down on its own. Honouring the signal is the caller’s responsibility.

Source

pub fn encode_slice(&self, src: &[u8]) -> Result<(), Error>

Encodes the slice into the write buffer.

If this triggers an eager backend write, any transport or filter error from that write is returned immediately.

Write back-pressure is advisory here too; see encode.

Source

pub fn encode_bytes<B>(&self, src: B) -> Result<(), Error>
where BytePage: From<B>,

Writes bytes to the write buffer.

If this triggers an eager backend write, any transport or filter error from that write is returned immediately.

Write back-pressure is advisory here too; see encode.

Source

pub fn decode<U>( &self, codec: &U, ) -> Result<Option<<U as Decoder>::Item>, <U as Decoder>::Error>
where U: Decoder,

Attempts to decode a frame from the read buffer.

Once the transport reached eof this uses Decoder::decode_eof instead of Decoder::decode.

This mutates the read state: it clears read readiness, and consuming enough bytes may release read backpressure. It also cancels a pause installed by Io::poll_read_pause and wakes the transport read task.

Decoded frames that share the read buffer’s allocation keep the whole buffer alive, see IoConfig::set_read_size.

Source

pub fn decode_item<U>( &self, codec: &U, ) -> Result<Decoded<<U as Decoder>::Item>, <U as Decoder>::Error>
where U: Decoder,

Attempts to decode a frame from the read buffer.

Decoded::consumed reports the bytes taken by this attempt and Decoded::remains the bytes left in the application-facing read buffer. Once the transport reached eof this uses Decoder::decode_eof instead of Decoder::decode.

Like decode, this mutates the read state: it clears read readiness, may release read backpressure, and cancels a pause installed by Io::poll_read_pause.

Source

pub fn send_buf(&self) -> Result<(), Error>

Sends the write buffer to the I/O layer.

Requires the underlying runtime to implement .write(); otherwise, no action is taken.

Source

pub fn with_buf<F, R>(&self, f: F) -> Result<R, Error>
where F: FnOnce(&mut FilterBuf<'_>) -> R,

Provides temporary access to the outermost filter buffers.

Filter callbacks run before and after f, and any produced write data is scheduled for delivery after the closure returns. Errors from an eager backend write are returned to the caller.

The destination exposed by FilterBuf::with_read_buffers is the application-facing read destination, so consuming enough of it releases read backpressure and cancels an installed read pause.

Buffer access is not reentrant. The closure must not use this connection to access an overlapping read or write buffer, or change the filter chain.

Source

pub fn with_read_dst<F, R>(&self, f: F) -> R
where F: FnOnce(&mut BytesMut) -> R,

Provides mutable access to the application-facing read destination.

This holds the decoded bytes the application consumes; see with_read_src for the transport-facing source.

This mutates the read state whether or not f consumes anything. While read back-pressure is active nothing is released until the buffer has fallen to at most half the high watermark, so until then read readiness and any installed read pause are left in place. Once it has, or when back-pressure was not active, read readiness is cleared and a pause installed by Io::poll_read_pause is cancelled, waking the transport read task.

Use crate::Io::poll_read_more rather than this method to check whether data is available.

The closure must not access this connection’s application-facing read destination again. Nested access is unsupported and may terminate the connection or lose nested buffer changes.

Source

pub fn read_dst_size(&self) -> usize

Returns the size of the application-facing read destination.

Unlike with_read_dst this does not change the read state, read readiness, read back-pressure and an installed read pause are left in place, and no buffer is allocated. Returns 0 when called from inside with_read_dst.

Source

pub fn with_write_src<F, R>(&self, f: F) -> Result<R, Error>
where F: FnOnce(&mut BytePages) -> R,

Provides mutable access to the application-facing write source.

This holds the bytes the application produces; see with_write_dst for the transport-facing destination.

Returns an error without invoking f if the connection is closing or closed. Data appended by f is scheduled for delivery. If that starts an eager backend write, its transport or filter error is returned.

§Panics

Panics if the closure accesses the same application-facing write buffer again.

Source

pub fn with_read_src<F, R>(&self, f: F) -> R
where F: FnOnce(&mut BytesMut) -> R,

Provides mutable access to the transport-facing read source.

This is the buffer the transport fills; it is the counterpart of the application-facing destination exposed by with_read_dst. Primarily intended for transport and filter implementations.

Without a filter installed this is the same buffer as the application-facing destination, so consuming enough of it releases read backpressure and cancels an installed read pause. Unlike with_read_dst it never clears read readiness.

The closure must not access this connection’s transport-facing read source again. Nested access is unsupported and may terminate the connection or lose nested buffer changes.

Source

pub fn with_write_dst<F, R>(&self, f: F) -> R
where F: FnOnce(&mut BytePages) -> R,

Provides mutable access to the transport-facing write destination.

This is the buffer the transport drains; it is the counterpart of the application-facing source exposed by with_write_src. Primarily intended for transport and filter implementations.

§Panics

Panics if the closure accesses the same transport-facing write buffer again.

Source

pub fn notify_dispatcher(&self)

Wakeup dispatcher

Source

pub fn notify_timeout(&self)

Wakeup dispatcher and send Timeout error

Source

pub fn timer_handle(&self) -> TimerHandle

Returns the currently registered dispatcher timer handle.

TimerHandle::ZERO is returned when no timer is registered.

Source

pub fn start_timer(&self, timeout: Seconds) -> TimerHandle

Starts or updates the dispatcher timer.

The timer uses second-granularity deadlines. When it expires, poll_status_update reports IoStatusUpdate::Timeout.

A zero timeout cancels the current timer but does not consume a timeout notification that has already been delivered. Use stop_timer when leaving a protocol phase to also clear such a notification.

Source

pub fn stop_timer(&self)

Stops the timer and clears any pending timeout notification.

Source

pub fn on_disconnect(&self) -> Waiter<'static> ⓘ

Returns a future that resolves when the complete I/O stream disconnects.

A clean peer read EOF does not resolve this future because the write half remains usable. It resolves once the transport backend reports that teardown has finished, which happens after local shutdown or force termination. terminate requests that teardown but does not itself resolve the future.

Source

pub fn wake(&self, tag: usize)

Wakes all Waiter waiters of the tag.

Tags reserved for internal use are ignored.

Source

pub fn waiter(&self, tag: usize) -> Waiter<'_> ⓘ

Creates a Waiter for the tag.

The waiter registers on its first poll.

§Panics

Panics in debug builds if the tag is reserved for internal use, usize::MAX - 1 and usize::MAX - 2 are reserved.

Trait Implementations§

Source§

impl<F> AsRef<IoRef> for Io<F>

Source§

fn as_ref(&self) -> &IoRef

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for IoRef

Source§

fn clone(&self) -> IoRef

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for IoRef

Source§

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

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

impl Eq for IoRef

Source§

impl Hash for IoRef

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for IoRef

Source§

fn eq(&self, other: &IoRef) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for IoRef

§

impl !Send for IoRef

§

impl !Sync for IoRef

§

impl !UnwindSafe for IoRef

§

impl Freeze for IoRef

§

impl Unpin for IoRef

§

impl UnsafeUnpin for IoRef

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.