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>

source

pub fn new(inner: ScratchStreamSinkChannel<'code>) -> Self

Construct a wrapper over inner.

Trait Implementations§

source§

impl<'code> AsyncWrite for AsyncStreamWriter<'code>

source§

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 more
source§

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<()>>

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

Determines if this writer has an efficient poll_write_vectored implementation. Read more
source§

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize>>

Like poll_write, except that it writes from a slice of buffers. Read more
source§

impl<'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> 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>,

§

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>,

§

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.