pub struct StreamBody {
pub stream: Arc<Mutex<Option<BoxStream<'static, Result<Bytes, CamelError>>>>>,
pub metadata: StreamMetadata,
}Expand description
A body that wraps a lazy-evaluated stream of bytes.
§Clone Semantics
The stream is single-consumption. When cloning a Body::Stream,
all clones share the same underlying stream handle. Only the first
clone to consume the stream will succeed; subsequent attempts will
return CamelError::AlreadyConsumed.
§Example
use camel_api::{Body, StreamBody, error::CamelError};
use futures::stream;
use bytes::Bytes;
use std::sync::Arc;
use tokio::sync::Mutex;
let chunks = vec![Ok(Bytes::from("data"))];
let stream = stream::iter(chunks);
let body = Body::Stream(StreamBody {
stream: Arc::new(Mutex::new(Some(Box::pin(stream)))),
metadata: Default::default(),
});
let clone = body.clone();
// First consumption succeeds
let _ = body.into_bytes(1024).await?;
// Second consumption fails
let result = clone.into_bytes(1024).await;
assert!(matches!(result, Err(CamelError::AlreadyConsumed)));Fields§
§stream: Arc<Mutex<Option<BoxStream<'static, Result<Bytes, CamelError>>>>>The actual byte stream, wrapped in an Arc and Mutex to allow Clone for Body.
metadata: StreamMetadataMetadata associated with the stream.
Trait Implementations§
Source§impl Clone for StreamBody
impl Clone for StreamBody
Auto Trait Implementations§
impl Freeze for StreamBody
impl !RefUnwindSafe for StreamBody
impl Send for StreamBody
impl Sync for StreamBody
impl Unpin for StreamBody
impl UnsafeUnpin for StreamBody
impl !UnwindSafe for StreamBody
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