Skip to content

Commit

Permalink
Merge pull request #158 from thomastaylor312/fix/sized_stream
Browse files Browse the repository at this point in the history
  • Loading branch information
thomastaylor312 authored Aug 15, 2024
2 parents 2775180 + 86b4f5f commit 2f692fc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ license = "Apache-2.0"
name = "oci-client"
readme = "README.md"
repository = "https://github.com/oras-project/rust-oci-client"
version = "0.12.0"
version = "0.12.1"

[badges]
maintenance = { status = "actively-developed" }
Expand Down
12 changes: 12 additions & 0 deletions src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::task::Poll;

use futures_util::stream::{BoxStream, Stream};
use futures_util::TryStreamExt;

use crate::digest::Digester;
use crate::errors::DigestError;
Expand All @@ -14,6 +15,17 @@ pub struct SizedStream {
pub stream: BoxStream<'static, Result<bytes::Bytes, std::io::Error>>,
}

impl Stream for SizedStream {
type Item = Result<bytes::Bytes, std::io::Error>;

fn poll_next(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Option<Self::Item>> {
self.stream.try_poll_next_unpin(cx)
}
}

/// The response of a partial blob request
pub enum BlobResponse {
/// The response is a full blob (for example when partial requests aren't supported)
Expand Down
29 changes: 27 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,8 +1089,33 @@ impl Client {

/// Stream a single layer from an OCI registry.
///
/// This is a streaming version of [`Client::pull_blob`].
/// Returns [`Stream`](futures_util::Stream).
/// This is a streaming version of [`Client::pull_blob`]. Returns [`SizedStream`], which
/// implements [`Stream`](futures_util::Stream) or can be used directly to get the content
/// length of the response
///
/// # Example
/// ```rust
/// use std::future::Future;
/// use std::io::Error;
///
/// use futures_util::TryStreamExt;
/// use oci_client::{Client, Reference};
/// use oci_client::client::ClientConfig;
/// use oci_client::manifest::OciDescriptor;
///
/// async {
/// let client = Client::new(Default::default());
/// let imgRef: Reference = "busybox:latest".parse().unwrap();
/// let desc = OciDescriptor { digest: "sha256:deadbeef".to_owned(), ..Default::default() };
/// let mut stream = client.pull_blob_stream(&imgRef, &desc).await.unwrap();
/// // Check the optional content length
/// let content_length = stream.content_length.unwrap_or_default();
/// // Use as a stream
/// stream.try_next().await.unwrap().unwrap();
/// // Use the underlying stream
/// let mut stream = stream.stream;
/// };
/// ```
pub async fn pull_blob_stream(
&self,
image: &Reference,
Expand Down

0 comments on commit 2f692fc

Please sign in to comment.