Crates.io | async-transmit |
lib.rs | async-transmit |
version | 1.0.0 |
source | src |
created_at | 2021-03-10 17:54:50.924565 |
updated_at | 2024-11-08 03:01:28.8495 |
description | Trait for transmitting data to peers asynchronously. |
homepage | |
repository | https://github.com/lambdalisue/rs-async-transmit |
max_upload_size | |
id | 366861 |
size | 32,780 |
async-transmit
crate provides Transmit
trait which allows value to be transmit asynchronously.
This crate relies on async-trait, the original definition of the Transmit
trait is:
use async_trait::async_trait;
#[async_trait]
pub trait Transmit {
type Item;
type Error;
async fn transmit(&mut self, item: Self::Item) -> Result<(), Self::Error>
where
Self::Item: 'async_trait;
}
So use #[async_trait]
to when implement Transmit
like:
use async_transmit::*;
use async_trait::async_trait;
struct VoidTransmitter<I, E> {
phantom: std::marker::PhantomData<(I, E)>,
}
#[async_trait]
impl<I, E> Transmit for VoidTransmitter<I, E>
where
I: Send,
E: Send,
{
type Item = I;
type Error = E;
async fn transmit(&mut self, item: Self::Item) -> Result<(), Self::Error>
where
I: 'async_trait,
{
// Do Nothing
Ok(())
}
}
If you'd like to play with async_std::channel::Sender
or async_channel::Sender
,
use with-async-channel
feature like:
[dependencies.async-transmit]
version = "0.1.0"
features = ["with-async-channel"]
Then you can use transmit()
method through Transmit
trait on the sender like:
use async_transmit::*;
let (mut s, r) = async_channel::unbounded::<&'static str>();
s.transmit("Hello").await?;
s.transmit("World").await?;
drop(s);
assert_eq!(Some("Hello"), r.recv().await.ok());
assert_eq!(Some("World"), r.recv().await.ok());
assert_eq!(None, r.recv().await.ok());
If you'd like to play with tokio::sync::mpsc::Sender
or tokio::sync::mpsc::UnboundedSender
,
use with-tokio
feature like:
[dependencies.async-transmit]
version = "0.1.0"
features = ["with-tokio"]
Then you can use transmit()
method through Transmit
trait on the sender like:
use async_transmit::*;
let (mut s, mut r) = tokio::sync::mpsc::unbounded_channel::<&'static str>();
s.transmit("Hello").await?;
s.transmit("World").await?;
drop(s);
assert_eq!(Some("Hello"), r.recv().await);
assert_eq!(Some("World"), r.recv().await);
assert_eq!(None, r.recv().await);
If you'd like to play with futures::sink::Sink
, use with-sink
feature like:
[dependencies.async-transmit]
version = "0.1.0"
features = ["with-sink"]
Then you can use async_transmit::from_sink()
to create a wrapper object which implements Transmit
trait like:
use async_transmit::*;
use futures::prelude::*;
let (s, mut r) = futures::channel::mpsc::unbounded::<&'static str>();
let mut s = from_sink(s);
s.transmit("Hello").await?;
s.transmit("World").await?;
drop(s);
assert_eq!(Some("Hello"), r.next().await);
assert_eq!(Some("World"), r.next().await);
assert_eq!(None, r.next().await);
The code follows MIT license written in LICENSE. Contributors need to agree that any modifications sent in this repository follow the license.