Struct glommio::io::DmaStreamReader

source ·
pub struct DmaStreamReader { /* private fields */ }
Expand description

Provides linear access to a DmaFile. The DmaFile is a convenient way to manage a file through Direct I/O, but its interface is conductive to random access, as a position must always be specified.

In situations where the file must be scanned linearly - either because a large chunk is to be read or because we are scanning the whole file, it may be more convenient to use a linear scan API.

The DmaStreamReader implements AsyncRead, which can offer the user with a convenient way of issuing reads. However note that this mandates a copy between the Dma Buffer used to read the file contents and the user-specified buffer.

To avoid that copy the DmaStreamReader provides the get_buffer_aligned method which exposes the buffer as a byte slice. Different situations will call for different APIs to be used.

Implementations§

source§

impl DmaStreamReader

source

pub async fn close(self) -> Result<(), ()>

Closes this DmaStreamReader.

It is illegal to close the DmaStreamReader more than once.

§Examples
use glommio::{
    io::{DmaFile, DmaStreamReaderBuilder},
    LocalExecutor,
};

let ex = LocalExecutor::default();
ex.run(async {
    let file = DmaFile::open("myfile.txt").await.unwrap();
    let mut reader = DmaStreamReaderBuilder::new(file).build();
    reader.close().await.unwrap();
});
source

pub fn skip(&mut self, bytes: u64)

Skip reading bytes from this DmaStreamReader.

The file cursor is advanced by the provided bytes. As this is a linear access API, once those bytes are skipped they are gone. If you need to read those bytes, just not now, it is better to read them and save them in some temporary buffer.

§Examples
use glommio::{
    io::{DmaFile, DmaStreamReaderBuilder},
    LocalExecutor,
};

let ex = LocalExecutor::default();
ex.run(async {
    let file = DmaFile::open("myfile.txt").await.unwrap();
    let mut reader = DmaStreamReaderBuilder::new(file).build();
    assert_eq!(reader.current_pos(), 0);
    reader.skip(8);
    assert_eq!(reader.current_pos(), 8);
    reader.close().await.unwrap();
});
source

pub fn current_pos(&self) -> u64

Acquires the current position of this DmaStreamReader.

§Examples
use glommio::{
    io::{DmaFile, DmaStreamReaderBuilder},
    LocalExecutor,
};

let ex = LocalExecutor::default();
ex.run(async {
    let file = DmaFile::open("myfile.txt").await.unwrap();
    let mut reader = DmaStreamReaderBuilder::new(file).build();
    assert_eq!(reader.current_pos(), 0);
    reader.skip(8);
    assert_eq!(reader.current_pos(), 8);
    reader.close().await.unwrap();
});
source

pub async fn get_buffer_aligned(&mut self, len: u64) -> Result<ReadResult, ()>

Allows access to the buffer that holds the current position with no extra copy.

This function returns a ReadResult. It contains all the bytes read, which can be less than the requested amount. Users are expected to call this in a loop like they would AsyncRead::poll_read.

The buffer is not released until the returned ReadResult goes out of scope. So if you plan to keep this alive for a long time this is probably the wrong API.

If EOF is hit while reading with this method, the number of bytes in the returned buffer will be less than number requested and the remaining bytes will be 0.

§Examples
use glommio::{
    io::{DmaFile, DmaStreamReaderBuilder},
    LocalExecutor,
};

let ex = LocalExecutor::default();
ex.run(async {
    let file = DmaFile::open("myfile.txt").await.unwrap();
    let mut reader = DmaStreamReaderBuilder::new(file).build();
    assert_eq!(reader.current_pos(), 0);
    let result = reader.get_buffer_aligned(512).await.unwrap();
    assert_eq!(result.len(), 512);
    println!("First 512 bytes: {:?}", &*result);
    reader.close().await.unwrap();
});
source

pub fn poll_get_buffer_aligned( &mut self, cx: &Context<'_>, len: u64 ) -> Poll<Result<ReadResult, ()>>

A variant of get_buffer_aligned that can be called from a poll function context.

Trait Implementations§

source§

impl AsyncRead for DmaStreamReader

source§

fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8] ) -> Poll<Result<usize>>

Attempt to read from the AsyncRead into buf. Read more
source§

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

Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more
source§

impl Debug for DmaStreamReader

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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<R> AsyncReadExt for R
where R: AsyncRead + ?Sized,

source§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>
where Self: Unpin,

Reads some bytes from the byte stream. Read more
source§

fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>] ) -> ReadVectoredFuture<'a, Self>
where Self: Unpin,

Like read(), except it reads into a slice of buffers. Read more
source§

fn read_to_end<'a>( &'a mut self, buf: &'a mut Vec<u8> ) -> ReadToEndFuture<'a, Self>
where Self: Unpin,

Reads the entire contents and appends them to a Vec. Read more
source§

fn read_to_string<'a>( &'a mut self, buf: &'a mut String ) -> ReadToStringFuture<'a, Self>
where Self: Unpin,

Reads the entire contents and appends them to a String. Read more
source§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>
where Self: Unpin,

Reads the exact number of bytes required to fill buf. Read more
source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Converts this AsyncRead into a Stream of bytes. Read more
source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: AsyncRead, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
source§

fn boxed_reader<'a>(self) -> Pin<Box<dyn AsyncRead + Send + 'a>>
where Self: Sized + Send + 'a,

Boxes the reader and changes its type to dyn AsyncRead + Send + 'a. 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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more