Struct MutexUnblockHandle

Source
pub struct MutexUnblockHandle<T: 'static + ?Sized>(/* private fields */);
Expand description

Wrapper type for making an async-locked blocking::Unblock Stdio stream. Parameter is one of the io::Stdout/io::Stderr/io::Stdin types plus any blocking::Unblock or other wrappers. This holds a static reference to an internal shared mutex. Some things may need outer BufRead/BufWrite attachments to make it match the equivalent sync trait impls - this is done as much as possible to match the stdio inside the stdlib.

Unlike the std::io unlocked-handles, this does not implement the various futures_lite::io traits. You need to use MutexUnblockHandle::lock or other locking methods to get access to a handle which can be used with these traits. Unlike the Standard Library, these handles are not re-entrant, which means that attempting to lock them in some inner future while the outer lock is held will cause a deadlock.

An important note is that Unblock implements Unpin unconditionally. This makes it easy to make generic implementations that will work nicely.

Implementations§

Source§

impl<T: ?Sized> MutexUnblockHandle<T>

Source

pub async fn lock(&self) -> MutexUnblockGuard<'static, T>

Locks this handle asynchronously.

Examples found in repository?
examples/helloworld-noflush.rs (line 10)
8fn main() -> std::io::Result<()> {
9    futures_lite::future::block_on(async {
10        let mut stdout_handle = astdio::stdout().lock().await;
11        // Note how we do not need to bother flushing this - it's auto-handled by the crate
12        stdout_handle.write_all(b"Hello world!\n").await
13    })
14}
Source

pub async fn lock_into_sync(&self) -> BlockOn<MutexUnblockGuard<'static, T>>

Lock this handle, but produce something that implements the synchronous std::io traits.

Examples found in repository?
examples/helloworld-re-syncify.rs (line 9)
6fn main() -> std::io::Result<()> {
7    futures_lite::future::block_on(async {
8        use std::io::Write;
9        let mut stdout_sync_handle = astdio::stdout().lock_into_sync().await;
10        // Note how we do not need to bother flushing this - it's auto-handled by the crate, and is
11        // basically just println! here.
12        //
13        // However - in reality, this is not ideal because `BlockOn` uses
14        // futures_lite::future::block_on internally - it's better to use the async traits
15        // internally where possible if you're inside a future like this.
16        writeln!(stdout_sync_handle, "Hello world, from sync!")
17    })
18}
Source

pub fn lock_blocking(&self) -> MutexUnblockGuard<'static, T>

Lock this handle in a sync context. Do not call in an async context or risk deadlocks!.

Source

pub fn lock_blocking_into_sync(&self) -> BlockOn<MutexUnblockGuard<'static, T>>

Lock this handle in a sync context, but producing something that implements the synchronous std::io traits.

Do not call in an async context or risk deadlocks!.

Source

pub fn try_lock(&self) -> Option<MutexUnblockGuard<'static, T>>

Attempt to lock this handle if you can

Source

pub fn try_lock_into_sync( &self, ) -> Option<BlockOn<MutexUnblockGuard<'static, T>>>

Attempt to lock this handle and wrap it in something that can be used with the synchronous std::io traits.

Trait Implementations§

Source§

impl<T: Debug + 'static + ?Sized> Debug for MutexUnblockHandle<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for MutexUnblockHandle<T>
where T: ?Sized,

§

impl<T> !RefUnwindSafe for MutexUnblockHandle<T>

§

impl<T> Send for MutexUnblockHandle<T>
where T: Send + ?Sized,

§

impl<T> Sync for MutexUnblockHandle<T>
where T: Send + ?Sized,

§

impl<T> Unpin for MutexUnblockHandle<T>
where T: ?Sized,

§

impl<T> !UnwindSafe for MutexUnblockHandle<T>

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.