Skip to content

Commit bb044e6

Browse files
committed
Use .await syntax
Signed-off-by: Nick Cameron <nrc@ncameron.org>
1 parent 6353dbc commit bb044e6

File tree

7 files changed

+175
-143
lines changed

7 files changed

+175
-143
lines changed

examples/raw.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ async fn main() -> Result<()> {
2727
// When we first create a client we receive a `Connect` structure which must be resolved before
2828
// the client is actually connected and usable.
2929
let unconnnected_client = Client::new(config);
30-
let client = await!(unconnnected_client)?;
30+
let client = unconnnected_client.await?;
3131

3232
// Requests are created from the connected client. These calls return structures which
3333
// implement `Future`. This means the `Future` must be resolved before the action ever takes
3434
// place.
3535
//
3636
// Here we set the key `TiKV` to have the value `Rust` associated with it.
37-
await!(client.put(KEY, VALUE)).unwrap(); // Returns a `tikv_client::Error` on failure.
37+
client.put(KEY, VALUE).await.unwrap(); // Returns a `tikv_client::Error` on failure.
3838
println!("Put key {:?}, value {:?}.", KEY, VALUE);
3939

4040
// Unlike a standard Rust HashMap all calls take owned values. This is because under the hood
@@ -46,17 +46,20 @@ async fn main() -> Result<()> {
4646
//
4747
// It is best to pass a `Vec<u8>` in terms of explictness and speed. `String`s and a few other
4848
// types are supported as well, but it all ends up as `Vec<u8>` in the end.
49-
let value: Option<Value> = await!(client.get(KEY))?;
49+
let value: Option<Value> = client.get(KEY).await?;
5050
assert_eq!(value, Some(Value::from(VALUE)));
5151
println!("Get key {:?} returned value {:?}.", Key::from(KEY), value);
5252

5353
// You can also set the `ColumnFamily` used by the request.
5454
// This is *advanced usage* and should have some special considerations.
55-
await!(client.delete(KEY)).expect("Could not delete value");
55+
client.delete(KEY).await.expect("Could not delete value");
5656
println!("Key: {:?} deleted", Key::from(KEY));
5757

5858
// Here we check if the key has been deleted from the key-value store.
59-
let value: Option<Value> = await!(client.get(KEY)).expect("Could not get just deleted entry");
59+
let value: Option<Value> = client
60+
.get(KEY)
61+
.await
62+
.expect("Could not get just deleted entry");
6063
assert!(value.is_none());
6164

6265
// You can ask to write multiple key-values at the same time, it is much more
@@ -66,18 +69,25 @@ async fn main() -> Result<()> {
6669
KvPair::from(("k2", "v2")),
6770
KvPair::from(("k3", "v3")),
6871
];
69-
await!(client.batch_put(pairs)).expect("Could not put pairs");
72+
client.batch_put(pairs).await.expect("Could not put pairs");
7073

7174
// Same thing when you want to retrieve multiple values.
7275
let keys = vec![Key::from("k1"), Key::from("k2")];
73-
let values = await!(client.batch_get(keys.clone())).expect("Could not get values");
76+
let values = client
77+
.batch_get(keys.clone())
78+
.await
79+
.expect("Could not get values");
7480
println!("Found values: {:?} for keys: {:?}", values, keys);
7581

7682
// Scanning a range of keys is also possible giving it two bounds
7783
// it will returns all entries between these two.
7884
let start = "k1";
7985
let end = "k2";
80-
let pairs = await!(client.scan(start..=end, 10).key_only()).expect("Could not scan");
86+
let pairs = client
87+
.scan(start..=end, 10)
88+
.key_only()
89+
.await
90+
.expect("Could not scan");
8191

8292
let keys: Vec<_> = pairs.into_iter().map(|p| p.key().clone()).collect();
8393
assert_eq!(&keys, &[Key::from("k1"), Key::from("k2")]);

examples/transaction.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,28 @@ use tikv_client::{
1818

1919
async fn puts(client: &Client, pairs: impl IntoIterator<Item = impl Into<KvPair>>) {
2020
let mut txn = client.begin();
21-
await!(future::join_all(
21+
future::join_all(
2222
pairs
2323
.into_iter()
2424
.map(Into::into)
25-
.map(|p| txn.set(p.key().clone(), p.value().clone()))
26-
))
25+
.map(|p| txn.set(p.key().clone(), p.value().clone())),
26+
)
27+
.await
2728
.into_iter()
2829
.collect::<Result<Vec<()>, _>>()
2930
.expect("Could not set key value pairs");
30-
await!(txn.commit()).expect("Could not commit transaction");
31+
txn.commit().await.expect("Could not commit transaction");
3132
}
3233

3334
async fn get(client: &Client, key: Key) -> Value {
3435
let txn = client.begin();
35-
await!(txn.get(key)).expect("Could not get value")
36+
txn.get(key).await.expect("Could not get value")
3637
}
3738

3839
// Ignore a spurious warning from rustc (https://github.com/rust-lang/rust/issues/60566).
3940
#[allow(unused_mut)]
4041
async fn scan(client: &Client, range: impl RangeBounds<Key>, mut limit: usize) {
41-
await!(client
42+
client
4243
.begin()
4344
.scan(range)
4445
.into_stream()
@@ -51,18 +52,21 @@ async fn scan(client: &Client, range: impl RangeBounds<Key>, mut limit: usize) {
5152
true
5253
})
5354
})
54-
.for_each(|pair| { future::ready(println!("{:?}", pair)) }));
55+
.for_each(|pair| future::ready(println!("{:?}", pair)))
56+
.await;
5557
}
5658

5759
async fn dels(client: &Client, keys: impl IntoIterator<Item = Key>) {
5860
let mut txn = client.begin();
5961
txn.set_isolation_level(IsolationLevel::ReadCommitted);
60-
let _: Vec<()> = await!(stream::iter(keys.into_iter())
61-
.then(|p| txn
62-
.delete(p)
63-
.unwrap_or_else(|e| panic!("error in delete: {:?}", e)))
64-
.collect());
65-
await!(txn.commit()).expect("Could not commit transaction");
62+
let _: Vec<()> = stream::iter(keys.into_iter())
63+
.then(|p| {
64+
txn.delete(p)
65+
.unwrap_or_else(|e| panic!("error in delete: {:?}", e))
66+
})
67+
.collect()
68+
.await;
69+
txn.commit().await.expect("Could not commit transaction");
6670
}
6771

6872
#[runtime::main(runtime_tokio::Tokio)]
@@ -79,26 +83,28 @@ async fn main() {
7983
Config::new(args.pd)
8084
};
8185

82-
let txn = await!(Client::new(config)).expect("Could not connect to tikv");
86+
let txn = Client::new(config)
87+
.await
88+
.expect("Could not connect to tikv");
8389

8490
// set
8591
let key1: Key = b"key1".to_vec().into();
8692
let value1: Value = b"value1".to_vec().into();
8793
let key2: Key = b"key2".to_vec().into();
8894
let value2: Value = b"value2".to_vec().into();
89-
await!(puts(&txn, vec![(key1, value1), (key2, value2)]));
95+
puts(&txn, vec![(key1, value1), (key2, value2)]).await;
9096

9197
// get
9298
let key1: Key = b"key1".to_vec().into();
93-
let value1 = await!(get(&txn, key1.clone()));
99+
let value1 = get(&txn, key1.clone()).await;
94100
println!("{:?}", (key1, value1));
95101

96102
// scan
97103
let key1: Key = b"key1".to_vec().into();
98-
await!(scan(&txn, key1.., 10));
104+
scan(&txn, key1.., 10).await;
99105

100106
// delete
101107
let key1: Key = b"key1".to_vec().into();
102108
let key2: Key = b"key2".to_vec().into();
103-
await!(dels(&txn, vec![key1, key2]));
109+
dels(&txn, vec![key1, key2]).await;
104110
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
//! ([raw](raw/struct.Client.html), [transactional](transaction/struct.Client.html)).
8282
//!
8383
//! ```rust
84-
//! # #![feature(async_await, await_macro)]
84+
//! # #![feature(async_await)]
8585
//! # use tikv_client::{*, raw::*};
8686
//! # use futures::prelude::*;
8787
//!
@@ -96,7 +96,7 @@
9696
//! let connect = Client::new(config);
9797
//!
9898
//! // Resolve the connection into a client.
99-
//! let client = await!(connect.into_future());
99+
//! let client = connect.into_future().await;
100100
//! # });
101101
//! ```
102102
//!

src/raw.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ impl Client {
2929
/// Create a new [`Client`](struct.Client.html) once the [`Connect`](struct.Connect.html) resolves.
3030
///
3131
/// ```rust,no_run
32-
/// # #![feature(async_await, await_macro)]
32+
/// # #![feature(async_await)]
3333
/// # use tikv_client::{Config, raw::Client};
3434
/// # use futures::prelude::*;
3535
/// # futures::executor::block_on(async {
3636
/// let connect = Client::new(Config::default());
37-
/// let client = await!(connect).unwrap();
37+
/// let client = connect.await.unwrap();
3838
/// # });
3939
/// ```
4040
#[cfg_attr(feature = "cargo-clippy", allow(clippy::new_ret_no_self))]
@@ -53,15 +53,15 @@ impl Client {
5353
/// given key.
5454
///
5555
/// ```rust,no_run
56-
/// # #![feature(async_await, await_macro)]
56+
/// # #![feature(async_await)]
5757
/// # use tikv_client::{Value, Config, raw::Client};
5858
/// # use futures::prelude::*;
5959
/// # futures::executor::block_on(async {
6060
/// # let connecting_client = Client::new(Config::new(vec!["192.168.0.100", "192.168.0.101"]));
61-
/// # let connected_client = await!(connecting_client).unwrap();
61+
/// # let connected_client = connecting_client.await.unwrap();
6262
/// let key = "TiKV";
6363
/// let req = connected_client.get(key);
64-
/// let result: Option<Value> = await!(req).unwrap();
64+
/// let result: Option<Value> = req.await.unwrap();
6565
/// # });
6666
/// ```
6767
pub fn get(&self, key: impl Into<Key>) -> Get {
@@ -74,15 +74,15 @@ impl Client {
7474
/// given keys.
7575
///
7676
/// ```rust,no_run
77-
/// # #![feature(async_await, await_macro)]
77+
/// # #![feature(async_await)]
7878
/// # use tikv_client::{KvPair, Config, raw::Client};
7979
/// # use futures::prelude::*;
8080
/// # futures::executor::block_on(async {
8181
/// # let connecting_client = Client::new(Config::new(vec!["192.168.0.100", "192.168.0.101"]));
82-
/// # let connected_client = await!(connecting_client).unwrap();
82+
/// # let connected_client = connecting_client.await.unwrap();
8383
/// let keys = vec!["TiKV", "TiDB"];
8484
/// let req = connected_client.batch_get(keys);
85-
/// let result: Vec<KvPair> = await!(req).unwrap();
85+
/// let result: Vec<KvPair> = req.await.unwrap();
8686
/// # });
8787
/// ```
8888
pub fn batch_get(&self, keys: impl IntoIterator<Item = impl Into<Key>>) -> BatchGet {
@@ -97,16 +97,16 @@ impl Client {
9797
/// Once resolved this request will result in the setting of the value associated with the given key.
9898
///
9999
/// ```rust,no_run
100-
/// # #![feature(async_await, await_macro)]
100+
/// # #![feature(async_await)]
101101
/// # use tikv_client::{Key, Value, Config, raw::Client};
102102
/// # use futures::prelude::*;
103103
/// # futures::executor::block_on(async {
104104
/// # let connecting_client = Client::new(Config::new(vec!["192.168.0.100", "192.168.0.101"]));
105-
/// # let connected_client = await!(connecting_client).unwrap();
105+
/// # let connected_client = connecting_client.await.unwrap();
106106
/// let key = "TiKV";
107107
/// let val = "TiKV";
108108
/// let req = connected_client.put(key, val);
109-
/// let result: () = await!(req).unwrap();
109+
/// let result: () = req.await.unwrap();
110110
/// # });
111111
/// ```
112112
pub fn put(&self, key: impl Into<Key>, value: impl Into<Value>) -> Put {
@@ -118,17 +118,17 @@ impl Client {
118118
/// Once resolved this request will result in the setting of the value associated with the given key.
119119
///
120120
/// ```rust,no_run
121-
/// # #![feature(async_await, await_macro)]
121+
/// # #![feature(async_await)]
122122
/// # use tikv_client::{Error, Result, KvPair, Key, Value, Config, raw::Client};
123123
/// # use futures::prelude::*;
124124
/// # futures::executor::block_on(async {
125125
/// # let connecting_client = Client::new(Config::new(vec!["192.168.0.100", "192.168.0.101"]));
126-
/// # let connected_client = await!(connecting_client).unwrap();
126+
/// # let connected_client = connecting_client.await.unwrap();
127127
/// let kvpair1 = ("PD", "Go");
128128
/// let kvpair2 = ("TiKV", "Rust");
129129
/// let iterable = vec![kvpair1, kvpair2];
130130
/// let req = connected_client.batch_put(iterable);
131-
/// let result: () = await!(req).unwrap();
131+
/// let result: () = req.await.unwrap();
132132
/// # });
133133
/// ```
134134
pub fn batch_put(&self, pairs: impl IntoIterator<Item = impl Into<KvPair>>) -> BatchPut {
@@ -143,15 +143,15 @@ impl Client {
143143
/// Once resolved this request will result in the deletion of the given key.
144144
///
145145
/// ```rust,no_run
146-
/// # #![feature(async_await, await_macro)]
146+
/// # #![feature(async_await)]
147147
/// # use tikv_client::{Key, Config, raw::Client};
148148
/// # use futures::prelude::*;
149149
/// # futures::executor::block_on(async {
150150
/// # let connecting_client = Client::new(Config::new(vec!["192.168.0.100", "192.168.0.101"]));
151-
/// # let connected_client = await!(connecting_client).unwrap();
151+
/// # let connected_client = connecting_client.await.unwrap();
152152
/// let key = "TiKV";
153153
/// let req = connected_client.delete(key);
154-
/// let result: () = await!(req).unwrap();
154+
/// let result: () = req.await.unwrap();
155155
/// # });
156156
/// ```
157157
pub fn delete(&self, key: impl Into<Key>) -> Delete {
@@ -163,15 +163,15 @@ impl Client {
163163
/// Once resolved this request will result in the deletion of the given keys.
164164
///
165165
/// ```rust,no_run
166-
/// # #![feature(async_await, await_macro)]
166+
/// # #![feature(async_await)]
167167
/// # use tikv_client::{Config, raw::Client};
168168
/// # use futures::prelude::*;
169169
/// # futures::executor::block_on(async {
170170
/// # let connecting_client = Client::new(Config::new(vec!["192.168.0.100", "192.168.0.101"]));
171-
/// # let connected_client = await!(connecting_client).unwrap();
171+
/// # let connected_client = connecting_client.await.unwrap();
172172
/// let keys = vec!["TiKV", "TiDB"];
173173
/// let req = connected_client.batch_delete(keys);
174-
/// let result: () = await!(req).unwrap();
174+
/// let result: () = req.await.unwrap();
175175
/// # });
176176
/// ```
177177
pub fn batch_delete(&self, keys: impl IntoIterator<Item = impl Into<Key>>) -> BatchDelete {
@@ -186,15 +186,15 @@ impl Client {
186186
/// Once resolved this request will result in a scanner over the given keys.
187187
///
188188
/// ```rust,no_run
189-
/// # #![feature(async_await, await_macro)]
189+
/// # #![feature(async_await)]
190190
/// # use tikv_client::{KvPair, Config, raw::Client};
191191
/// # use futures::prelude::*;
192192
/// # futures::executor::block_on(async {
193193
/// # let connecting_client = Client::new(Config::new(vec!["192.168.0.100", "192.168.0.101"]));
194-
/// # let connected_client = await!(connecting_client).unwrap();
194+
/// # let connected_client = connecting_client.await.unwrap();
195195
/// let inclusive_range = "TiKV"..="TiDB";
196196
/// let req = connected_client.scan(inclusive_range, 2);
197-
/// let result: Vec<KvPair> = await!(req).unwrap();
197+
/// let result: Vec<KvPair> = req.await.unwrap();
198198
/// # });
199199
/// ```
200200
pub fn scan(&self, range: impl KeyRange, limit: u32) -> Scan {
@@ -206,17 +206,17 @@ impl Client {
206206
/// Once resolved this request will result in a set of scanners over the given keys.
207207
///
208208
/// ```rust,no_run
209-
/// # #![feature(async_await, await_macro)]
209+
/// # #![feature(async_await)]
210210
/// # use tikv_client::{Key, Config, raw::Client};
211211
/// # use futures::prelude::*;
212212
/// # futures::executor::block_on(async {
213213
/// # let connecting_client = Client::new(Config::new(vec!["192.168.0.100", "192.168.0.101"]));
214-
/// # let connected_client = await!(connecting_client).unwrap();
214+
/// # let connected_client = connecting_client.await.unwrap();
215215
/// let inclusive_range1 = "TiDB"..="TiKV";
216216
/// let inclusive_range2 = "TiKV"..="TiSpark";
217217
/// let iterable = vec![inclusive_range1, inclusive_range2];
218218
/// let req = connected_client.batch_scan(iterable, 2);
219-
/// let result = await!(req);
219+
/// let result = req.await;
220220
/// # });
221221
/// ```
222222
pub fn batch_scan(
@@ -238,15 +238,15 @@ impl Client {
238238
/// Once resolved this request will result in the deletion of all keys over the given range.
239239
///
240240
/// ```rust,no_run
241-
/// # #![feature(async_await, await_macro)]
241+
/// # #![feature(async_await)]
242242
/// # use tikv_client::{Key, Config, raw::Client};
243243
/// # use futures::prelude::*;
244244
/// # futures::executor::block_on(async {
245245
/// # let connecting_client = Client::new(Config::new(vec!["192.168.0.100", "192.168.0.101"]));
246-
/// # let connected_client = await!(connecting_client).unwrap();
246+
/// # let connected_client = connecting_client.await.unwrap();
247247
/// let inclusive_range = "TiKV"..="TiDB";
248248
/// let req = connected_client.delete_range(inclusive_range);
249-
/// let result: () = await!(req).unwrap();
249+
/// let result: () = req.await.unwrap();
250250
/// # });
251251
/// ```
252252
pub fn delete_range(&self, range: impl KeyRange) -> DeleteRange {
@@ -259,13 +259,13 @@ impl Client {
259259
/// Once resolved it will result in a connected [`Client`](struct.Client.html).
260260
///
261261
/// ```rust,no_run
262-
/// # #![feature(async_await, await_macro)]
262+
/// # #![feature(async_await)]
263263
/// use tikv_client::{Config, raw::{Client, Connect}};
264264
/// use futures::prelude::*;
265265
///
266266
/// # futures::executor::block_on(async {
267267
/// let connect: Connect = Client::new(Config::default());
268-
/// let client: Client = await!(connect).unwrap();
268+
/// let client: Client = connect.await.unwrap();
269269
/// # });
270270
/// ```
271271
pub struct Connect {

0 commit comments

Comments
 (0)