Skip to main content

FileStream

Struct FileStream 

Source
pub struct FileStream { /* private fields */ }
Expand description

A stream that reads a file in chunks.

This stream reads files incrementally, yielding chunks of configurable size. It integrates with asupersync’s Cx for:

  • Cancellation detection (client disconnect)
  • Checkpoint calls between chunks for cooperative yielding

§Memory Efficiency

Only one chunk is buffered at a time, making this suitable for streaming large files without excessive memory usage.

§Example

use fastapi_http::streaming::{FileStream, StreamConfig};

let config = StreamConfig::default().with_chunk_size(32 * 1024);
let stream = FileStream::open("video.mp4", cx.clone(), config).await?;

Implementations§

Source§

impl FileStream

Source

pub fn open<P: AsRef<Path>>( path: P, cx: Cx, config: StreamConfig, ) -> Result<Self>

Open a file for streaming.

§Arguments
  • path - Path to the file to stream
  • cx - Capability context for cancellation
  • config - Streaming configuration
§Errors

Returns an error if the file cannot be opened.

Source

pub fn open_range<P: AsRef<Path>>( path: P, start: u64, length: u64, cx: Cx, config: StreamConfig, ) -> Result<Self>

Open a file for streaming with a byte range.

Useful for HTTP Range requests.

§Arguments
  • path - Path to the file
  • start - Start byte offset
  • length - Number of bytes to stream
  • cx - Capability context
  • config - Streaming configuration
§Errors

Returns an error if the file cannot be opened or seeked.

Source

pub fn remaining(&self) -> u64

Get the remaining bytes to be read.

Source

pub fn is_complete(&self) -> bool

Check if the stream is complete.

Trait Implementations§

Source§

impl Stream for FileStream

Source§

type Item = Vec<u8>

The type of values yielded by the stream.
Source§

fn poll_next( self: Pin<&mut Self>, _ctx: &mut Context<'_>, ) -> Poll<Option<Self::Item>>

Attempt to pull out the next value of this stream. Read more
Source§

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the stream. Read more
Source§

impl Send for FileStream

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<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: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<S> StreamExt for S
where S: Stream + ?Sized,

Source§

fn next(&mut self) -> Next<'_, Self>
where Self: Unpin,

Returns the next item from the stream.
Source§

fn map<T, F>(self, f: F) -> Map<Self, F>
where Self: Sized, F: FnMut(Self::Item) -> T,

Transforms each item using a closure.
Source§

fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
where Self: Sized, F: FnMut(Self::Item) -> Fut, Fut: Future,

Transforms each item using an async closure.
Source§

fn chain<S2>(self, other: S2) -> Chain<Self, S2>
where Self: Sized, S2: Stream<Item = Self::Item>,

Chains this stream with another stream.
Source§

fn zip<S2>(self, other: S2) -> Zip<Self, S2>
where Self: Sized, S2: Stream,

Zips this stream with another stream, yielding pairs.
Source§

fn filter<P>(self, predicate: P) -> Filter<Self, P>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Yields only items that match the predicate.
Source§

fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
where Self: Sized, F: FnMut(Self::Item) -> Option<T>,

Filters and transforms items in one step.
Source§

fn take(self, n: usize) -> Take<Self>
where Self: Sized,

Takes the first n items.
Source§

fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Takes items while the predicate is true.
Source§

fn skip(self, n: usize) -> Skip<Self>
where Self: Sized,

Skips the first n items.
Source§

fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Skips items while the predicate is true.
Source§

fn enumerate(self) -> Enumerate<Self>
where Self: Sized,

Enumerates items with their index.
Source§

fn fuse(self) -> Fuse<Self>
where Self: Sized,

Fuses the stream to handle None gracefully.
Source§

fn inspect<F>(self, f: F) -> Inspect<Self, F>
where Self: Sized, F: FnMut(&Self::Item),

Inspects items without modifying the stream.
Source§

fn buffered(self, n: usize) -> Buffered<Self>
where Self: Sized, Self::Item: Future,

Buffers up to n futures, preserving output order.
Source§

fn buffer_unordered(self, n: usize) -> BufferUnordered<Self>
where Self: Sized, Self::Item: Future,

Buffers up to n futures, yielding results as they complete.
Source§

fn collect<C>(self) -> Collect<Self, C>
where Self: Sized, C: Default + Extend<Self::Item>,

Collects all items into a collection.
Source§

fn chunks(self, size: usize) -> Chunks<Self>
where Self: Sized,

Collects items into fixed-size chunks.
Source§

fn ready_chunks(self, size: usize) -> ReadyChunks<Self>
where Self: Sized,

Yields immediately available items up to a maximum chunk size.
Source§

fn fold<Acc, F>(self, init: Acc, f: F) -> Fold<Self, F, Acc>
where Self: Sized, F: FnMut(Acc, Self::Item) -> Acc,

Folds all items into a single value.
Source§

fn for_each<F>(self, f: F) -> ForEach<Self, F>
where Self: Sized, F: FnMut(Self::Item),

Executes a closure for each item.
Source§

fn for_each_async<F, Fut>(self, f: F) -> ForEachAsync<Self, F, Fut>
where Self: Sized, F: FnMut(Self::Item) -> Fut, Fut: Future<Output = ()>,

Executes an async closure for each item.
Source§

fn count(self) -> Count<Self>
where Self: Sized,

Counts the number of items in the stream.
Source§

fn any<P>(self, predicate: P) -> Any<Self, P>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Checks if any item matches the predicate.
Source§

fn all<P>(self, predicate: P) -> All<Self, P>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Checks if all items match the predicate.
Source§

fn try_collect<T, E, C>(self) -> TryCollect<Self, C>
where Self: Sized + Stream<Item = Result<T, E>>, C: Default + Extend<T>,

Collects items from a stream of Results, short-circuiting on error.
Source§

fn try_fold<T, E, Acc, F>(self, init: Acc, f: F) -> TryFold<Self, F, Acc>
where Self: Sized + Stream<Item = Result<T, E>>, F: FnMut(Acc, T) -> Result<Acc, E>,

Folds a stream of Results, short-circuiting on error.
Source§

fn try_for_each<F, E>(self, f: F) -> TryForEach<Self, F>
where Self: Sized, F: FnMut(Self::Item) -> Result<(), E>,

Executes a fallible closure for each item, short-circuiting on error.
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.
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
Source§

impl<T> ResponseProduces<T> for T