Skip to content

Commit 6f35e13

Browse files
authored
Remove Box::pin requirement in usage of Pageable (#668)
1 parent a26835f commit 6f35e13

File tree

8 files changed

+38
-22
lines changed

8 files changed

+38
-22
lines changed

sdk/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ serde_json = "1.0"
3232
thiserror = "1.0"
3333
url = "2.2"
3434
uuid = { version = "0.8" }
35+
pin-project = "1.0.10"
3536

3637
# Add dependency to getrandom to enable WASM support
3738
[target.'cfg(target_arch = "wasm32")'.dependencies]

sdk/core/src/pageable.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use futures::stream::unfold;
22
use futures::Stream;
3+
use pin_project::pin_project;
34

45
macro_rules! r#try {
56
($expr:expr $(,)?) => {
@@ -16,7 +17,9 @@ macro_rules! r#try {
1617
///
1718
/// Internally uses the Azure specific continuation header to
1819
/// make repeated requests to Azure yielding a new page each time.
20+
#[pin_project]
1921
pub struct Pageable<T, E> {
22+
#[pin]
2023
stream: std::pin::Pin<Box<dyn Stream<Item = Result<T, E>>>>,
2124
}
2225

@@ -54,10 +57,11 @@ impl<T, E> Stream for Pageable<T, E> {
5457
type Item = Result<T, E>;
5558

5659
fn poll_next(
57-
mut self: std::pin::Pin<&mut Self>,
60+
self: std::pin::Pin<&mut Self>,
5861
cx: &mut std::task::Context<'_>,
5962
) -> std::task::Poll<Option<Self::Item>> {
60-
std::pin::Pin::new(&mut self.stream).poll_next(cx)
63+
let this = self.project();
64+
this.stream.poll_next(cx)
6165
}
6266
}
6367

sdk/data_cosmos/examples/collection.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
3131
);
3232

3333
// The Cosmos' client exposes a lot of methods. This one lists the databases in the specified account.
34-
let databases = Box::pin(client.list_databases().into_stream())
34+
let databases = client
35+
.list_databases()
36+
.into_stream()
3537
.next()
3638
.await
3739
.unwrap()?;
@@ -59,7 +61,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
5961

6062
for db in databases.databases {
6163
let database_client = client.clone().into_database_client(db.id.clone());
62-
let collections = Box::pin(database_client.list_collections().into_stream())
64+
let collections = database_client
65+
.list_collections()
66+
.into_stream()
6367
.next()
6468
.await
6569
.unwrap()?;

sdk/data_cosmos/examples/create_delete_database.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
3333
// account. Database do not implement Display but deref to &str so you can pass it to methods
3434
// both as struct or id.
3535

36-
let mut list_databases_stream = Box::pin(client.list_databases().into_stream());
36+
let mut list_databases_stream = client.list_databases().into_stream();
3737
while let Some(list_databases_response) = list_databases_stream.next().await {
3838
println!("list_databases_response = {:#?}", list_databases_response?);
3939
}
@@ -60,8 +60,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
6060
let get_collection_response = db_collection.get_collection().into_future().await?;
6161
println!("get_collection_response == {:#?}", get_collection_response);
6262

63-
let stream = db_client.list_collections().into_stream();
64-
let mut stream = Box::pin(stream);
63+
let mut stream = db_client.list_collections().into_stream();
6564
while let Some(res) = stream.next().await {
6665
let res = res?;
6766
println!("res == {:#?}", res);

sdk/data_cosmos/examples/database_00.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
1515

1616
let client = CosmosClient::new(account, authorization_token, CosmosOptions::default());
1717

18-
let dbs = Box::pin(client.list_databases().into_stream())
18+
let dbs = client
19+
.list_databases()
20+
.into_stream()
1921
.next()
2022
.await
2123
.unwrap()?;
@@ -24,7 +26,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
2426
println!("database == {:?}", db);
2527
let database = client.clone().into_database_client(db.name().to_owned());
2628

27-
let collections = Box::pin(database.list_collections().into_stream())
29+
let collections = database
30+
.list_collections()
31+
.into_stream()
2832
.next()
2933
.await
3034
.unwrap()?;

sdk/data_cosmos/examples/database_01.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
1717
let database_client = client.into_database_client("pollo");
1818
println!("database_name == {}", database_client.database_name());
1919

20-
let collections = Box::pin(database_client.list_collections().into_stream())
20+
let collections = database_client
21+
.list_collections()
22+
.into_stream()
2123
.next()
2224
.await
2325
.unwrap()?;

sdk/data_cosmos/examples/document_00.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
5656
// an error (for example, the given key is not valid) you will receive a
5757
// specific azure_data_cosmos::Error. In this example we will look for a specific database
5858
// so we chain a filter operation.
59-
let db = Box::pin(client.list_databases().into_stream())
59+
let db = client
60+
.list_databases()
61+
.into_stream()
6062
.next()
6163
.await
6264
.unwrap()?
@@ -81,16 +83,14 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
8183
// we will create it. The collection creation is more complex and
8284
// has many options (such as indexing and so on).
8385
let collection = {
84-
let collections = Box::pin(
85-
client
86-
.clone()
87-
.into_database_client(database.id.clone())
88-
.list_collections()
89-
.into_stream(),
90-
)
91-
.next()
92-
.await
93-
.unwrap()?;
86+
let collections = client
87+
.clone()
88+
.into_database_client(database.id.clone())
89+
.list_collections()
90+
.into_stream()
91+
.next()
92+
.await
93+
.unwrap()?;
9494

9595
if let Some(collection) = collections
9696
.collections

sdk/data_cosmos/examples/user_00.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
3232
let create_user_response = user_client.create_user().into_future().await?;
3333
println!("create_user_response == {:#?}", create_user_response);
3434

35-
let users = Box::pin(database_client.list_users().into_stream())
35+
let users = database_client
36+
.list_users()
37+
.into_stream()
3638
.next()
3739
.await
3840
.unwrap()?;

0 commit comments

Comments
 (0)