Struct vectorscan::stream::channel::AsyncStreamWriter
source · pub struct AsyncStreamWriter<'code> {
pub inner: ScratchStreamSinkChannel<'code>,
/* private fields */
}
Available on crate features
stream
and async
and tokio-impls
only.Expand description
A wrapper over ScratchStreamSinkChannel
which implements
tokio::io::AsyncWrite
.
The reason this is separate from ScratchStreamSinkChannel
is that in
the case of vectored writes, std::io::IoSlice
must be converted into
VectoredByteSlices
. As in the synchronous
super::super::StreamWriter
, this would typically require a dynamic
memory allocation, but this struct maintains an internal buffer of
strings which is re-used for subsequent vectored writes to avoid
repeated dynamic memory allocation. Additionally, in the async case,
implementing poll methods like io::AsyncWrite::poll_write()
and
io::AsyncWrite::poll_shutdown()
requires storing
Waker
state inside the object whenever it is
polled.
#[cfg(feature = "compiler")]
fn main() -> Result<(), vectorscan::error::VectorscanError> { tokio_test::block_on(async {
use vectorscan::{expression::*, flags::*, stream::channel::*, matchers::*};
use futures_util::StreamExt;
use tokio::io::AsyncWriteExt;
use std::ops::Range;
let expr: Expression = "a+".parse()?;
let db = expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_LARGE)?;
let scratch = db.allocate_scratch()?;
let live = db.allocate_stream()?;
let mut match_fn = |_: &_| MatchResult::Continue;
let sink = ScratchStreamSinkChannel::new(live, &mut match_fn, scratch);
let mut sink = AsyncStreamWriter::new(sink);
sink.write_all(b"aardvark").await.unwrap();
sink.shutdown().await.unwrap();
let matches: Vec<Range<usize>> = sink.inner.collect_matches()
.map(|m| m.range.into())
.collect()
.await;
assert_eq!(&matches, &[0..1, 0..2, 5..6]);
Ok(())
})}
Fields§
§inner: ScratchStreamSinkChannel<'code>
Implementations§
source§impl<'code> AsyncStreamWriter<'code>
impl<'code> AsyncStreamWriter<'code>
sourcepub fn new(inner: ScratchStreamSinkChannel<'code>) -> Self
pub fn new(inner: ScratchStreamSinkChannel<'code>) -> Self
Construct a wrapper over inner
.
Trait Implementations§
source§impl<'code> AsyncWrite for AsyncStreamWriter<'code>
impl<'code> AsyncWrite for AsyncStreamWriter<'code>
source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>
Attempt to write bytes from
buf
into the object. Read moresource§fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<()>>
Attempts to flush the object, ensuring that any buffered data reach
their destination. Read more
source§fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
Initiates or attempts to shut down this writer, returning success when
the I/O connection has completely shut down. Read more
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
Determines if this writer has an efficient
poll_write_vectored
implementation. Read moreimpl<'code> Send for AsyncStreamWriter<'code>
Auto Trait Implementations§
impl<'code> !RefUnwindSafe for AsyncStreamWriter<'code>
impl<'code> !Sync for AsyncStreamWriter<'code>
impl<'code> Unpin for AsyncStreamWriter<'code>
impl<'code> !UnwindSafe for AsyncStreamWriter<'code>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more