Skip to content

Commit e38486b

Browse files
piodulwprzytula
andcommitted
book: adjust code examples to the new deserialization API
Examples in the Book are now adjusted to the new deserialization API, and the book finally compile and the tests there pass. Note that this commit does not describe exhaustively the intended way of using the new deserialization framework; this is left for a follow-up. Co-authored-by: Wojciech Przytuła <[email protected]>
1 parent e99d697 commit e38486b

23 files changed

+138
-155
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ let uri = "127.0.0.1:9042";
2121

2222
let session: Session = SessionBuilder::new().known_node(uri).build().await?;
2323

24-
let raw_iter = session.query_iter("SELECT a, b, c FROM ks.t", &[]).await?;
25-
let mut iter = raw_iter.into_typed::<(i32, i32, String)>();
26-
while let Some((a, b, c)) = iter.try_next().await? {
24+
let query_pager = session.query_iter("SELECT a, b, c FROM ks.t", &[]).await?;
25+
let mut stream = query_pager.rows_stream::<(i32, i32, String)>()?;
26+
while let Some((a, b, c)) = stream.try_next().await? {
2727
println!("a, b, c: {}, {}, {}", a, b, c);
2828
}
2929
```

docs/source/data-types/blob.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ session
1818
.await?;
1919

2020
// Read blobs from the table
21-
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[]).await?.into_typed::<(Vec<u8>,)>();
22-
while let Some((blob_value,)) = iter.try_next().await? {
21+
let mut stream = session.query_iter("SELECT a FROM keyspace.table", &[])
22+
.await?
23+
.rows_stream::<(Vec<u8>,)>()?;
24+
while let Some((blob_value,)) = stream.try_next().await? {
2325
println!("{:?}", blob_value);
2426
}
2527
# Ok(())

docs/source/data-types/collections.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ session
1818
.await?;
1919

2020
// Read a list of ints from the table
21-
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[]).await?.into_typed::<(Vec<i32>,)>();
22-
while let Some((list_value,)) = iter.try_next().await? {
21+
let mut stream = session.query_iter("SELECT a FROM keyspace.table", &[])
22+
.await?
23+
.rows_stream::<(Vec<i32>,)>()?;
24+
while let Some((list_value,)) = stream.try_next().await? {
2325
println!("{:?}", list_value);
2426
}
2527
# Ok(())
@@ -44,10 +46,10 @@ session
4446
.await?;
4547

4648
// Read a set of ints from the table
47-
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
49+
let mut stream = session.query_iter("SELECT a FROM keyspace.table", &[])
4850
.await?
49-
.into_typed::<(Vec<i32>,)>();
50-
while let Some((set_value,)) = iter.try_next().await? {
51+
.rows_stream::<(Vec<i32>,)>()?;
52+
while let Some((set_value,)) = stream.try_next().await? {
5153
println!("{:?}", set_value);
5254
}
5355
# Ok(())
@@ -72,7 +74,7 @@ session
7274
// Read a set of ints from the table
7375
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
7476
.await?
75-
.into_typed::<(HashSet<i32>,)>();
77+
.rows_stream::<(HashSet<i32>,)>()?;
7678
while let Some((set_value,)) = iter.try_next().await? {
7779
println!("{:?}", set_value);
7880
}
@@ -98,7 +100,7 @@ session
98100
// Read a set of ints from the table
99101
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
100102
.await?
101-
.into_typed::<(BTreeSet<i32>,)>();
103+
.rows_stream::<(BTreeSet<i32>,)>()?;
102104
while let Some((set_value,)) = iter.try_next().await? {
103105
println!("{:?}", set_value);
104106
}
@@ -129,7 +131,7 @@ session
129131
// Read a map from the table
130132
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
131133
.await?
132-
.into_typed::<(HashMap<String, i32>,)>();
134+
.rows_stream::<(HashMap<String, i32>,)>()?;
133135
while let Some((map_value,)) = iter.try_next().await? {
134136
println!("{:?}", map_value);
135137
}
@@ -157,7 +159,7 @@ session
157159
// Read a map from the table
158160
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
159161
.await?
160-
.into_typed::<(BTreeMap<String, i32>,)>();
162+
.rows_stream::<(BTreeMap<String, i32>,)>()?;
161163
while let Some((map_value,)) = iter.try_next().await? {
162164
println!("{:?}", map_value);
163165
}

docs/source/data-types/counter.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ session
1818
.await?;
1919

2020
// Read counter from the table
21-
let mut iter = session.query_iter("SELECT c FROM keyspace.table", &[])
21+
let mut stream = session.query_iter("SELECT c FROM keyspace.table", &[])
2222
.await?
23-
.into_typed::<(Counter,)>();
24-
while let Some((counter_value,)) = iter.try_next().await? {
23+
.rows_stream::<(Counter,)>()?;
24+
while let Some((counter_value,)) = stream.try_next().await? {
2525
let counter_int_value: i64 = counter_value.0;
2626
println!("{}", counter_int_value);
2727
}

docs/source/data-types/date.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ session
3232
// Read raw Date from the table
3333
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
3434
.await?
35-
.into_typed::<(CqlDate,)>();
35+
.rows_stream::<(CqlDate,)>()?;
3636
while let Some((date_value,)) = iter.try_next().await? {
3737
// ...
3838
}
@@ -68,7 +68,7 @@ session
6868
// Read NaiveDate from the table
6969
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
7070
.await?
71-
.into_typed::<(NaiveDate,)>();
71+
.rows_stream::<(NaiveDate,)>()?;
7272
while let Some((date_value,)) = iter.try_next().await? {
7373
// ...
7474
}
@@ -104,7 +104,7 @@ session
104104
// Read Date from the table
105105
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
106106
.await?
107-
.into_typed::<(Date,)>();
107+
.rows_stream::<(Date,)>()?;
108108
while let Some((date_value,)) = iter.try_next().await? {
109109
// ...
110110
}

docs/source/data-types/decimal.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ session
2525
// Read a decimal from the table
2626
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
2727
.await?
28-
.into_typed::<(CqlDecimal,)>();
28+
.rows_stream::<(CqlDecimal,)>()?;
2929
while let Some((decimal_value,)) = iter.try_next().await? {
3030
println!("{:?}", decimal_value);
3131
}
@@ -57,7 +57,7 @@ session
5757
// Read a decimal from the table
5858
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
5959
.await?
60-
.into_typed::<(BigDecimal,)>();
60+
.rows_stream::<(BigDecimal,)>()?;
6161
while let Some((decimal_value,)) = iter.try_next().await? {
6262
println!("{:?}", decimal_value);
6363
}

docs/source/data-types/duration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ session
1919
// Read duration from the table
2020
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
2121
.await?
22-
.into_typed::<(CqlDuration,)>();
22+
.rows_stream::<(CqlDuration,)>()?;
2323
while let Some((duration_value,)) = iter.try_next().await? {
2424
println!("{:?}", duration_value);
2525
}

docs/source/data-types/inet.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ session
1919
// Read inet from the table
2020
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
2121
.await?
22-
.into_typed::<(IpAddr,)>();
22+
.rows_stream::<(IpAddr,)>()?;
2323
while let Some((inet_value,)) = iter.try_next().await? {
2424
println!("{:?}", inet_value);
2525
}

docs/source/data-types/primitive.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ session
2121
// Read a bool from the table
2222
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
2323
.await?
24-
.into_typed::<(bool,)>();
24+
.rows_stream::<(bool,)>()?;
2525
while let Some((bool_value,)) = iter.try_next().await? {
2626
println!("{:?}", bool_value);
2727
}
@@ -50,7 +50,7 @@ session
5050
// Read a tinyint from the table
5151
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
5252
.await?
53-
.into_typed::<(i8,)>();
53+
.rows_stream::<(i8,)>()?;
5454
while let Some((tinyint_value,)) = iter.try_next().await? {
5555
println!("{:?}", tinyint_value);
5656
}
@@ -79,7 +79,7 @@ session
7979
// Read a smallint from the table
8080
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
8181
.await?
82-
.into_typed::<(i16,)>();
82+
.rows_stream::<(i16,)>()?;
8383
while let Some((smallint_value,)) = iter.try_next().await? {
8484
println!("{}", smallint_value);
8585
}
@@ -108,7 +108,7 @@ session
108108
// Read an int from the table
109109
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
110110
.await?
111-
.into_typed::<(i32,)>();
111+
.rows_stream::<(i32,)>()?;
112112
while let Some((int_value,)) = iter.try_next().await? {
113113
println!("{}", int_value);
114114
}
@@ -137,7 +137,7 @@ session
137137
// Read a bigint from the table
138138
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
139139
.await?
140-
.into_typed::<(i64,)>();
140+
.rows_stream::<(i64,)>()?;
141141
while let Some((bigint_value,)) = iter.try_next().await? {
142142
println!("{:?}", bigint_value);
143143
}
@@ -166,7 +166,7 @@ session
166166
// Read a float from the table
167167
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
168168
.await?
169-
.into_typed::<(f32,)>();
169+
.rows_stream::<(f32,)>()?;
170170
while let Some((float_value,)) = iter.try_next().await? {
171171
println!("{:?}", float_value);
172172
}
@@ -195,7 +195,7 @@ session
195195
// Read a double from the table
196196
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
197197
.await?
198-
.into_typed::<(f64,)>();
198+
.rows_stream::<(f64,)>()?;
199199
while let Some((double_value,)) = iter.try_next().await? {
200200
println!("{:?}", double_value);
201201
}

docs/source/data-types/text.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ session
2424
// Read ascii/text/varchar from the table
2525
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
2626
.await?
27-
.into_typed::<(String,)>();
27+
.rows_stream::<(String,)>()?;
2828
while let Some((text_value,)) = iter.try_next().await? {
2929
println!("{}", text_value);
3030
}

docs/source/data-types/time.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ session
3232
// Read time from the table
3333
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
3434
.await?
35-
.into_typed::<(CqlTime,)>();
35+
.rows_stream::<(CqlTime,)>()?;
3636
while let Some((value,)) = iter.try_next().await? {
3737
// ...
3838
}
@@ -68,7 +68,7 @@ session
6868
// Read time from the table
6969
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
7070
.await?
71-
.into_typed::<(NaiveTime,)>();
71+
.rows_stream::<(NaiveTime,)>()?;
7272
while let Some((time_value,)) = iter.try_next().await? {
7373
println!("{:?}", time_value);
7474
}
@@ -102,7 +102,7 @@ session
102102
// Read time from the table
103103
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
104104
.await?
105-
.into_typed::<(Time,)>();
105+
.rows_stream::<(Time,)>()?;
106106
while let Some((time_value,)) = iter.try_next().await? {
107107
println!("{:?}", time_value);
108108
}

docs/source/data-types/timestamp.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ session
3333
// Read timestamp from the table
3434
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
3535
.await?
36-
.into_typed::<(CqlTimestamp,)>();
36+
.rows_stream::<(CqlTimestamp,)>()?;
3737
while let Some((value,)) = iter.try_next().await? {
3838
// ...
3939
}
@@ -73,7 +73,7 @@ session
7373
// Read timestamp from the table
7474
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
7575
.await?
76-
.into_typed::<(DateTime<Utc>,)>();
76+
.rows_stream::<(DateTime<Utc>,)>()?;
7777
while let Some((timestamp_value,)) = iter.try_next().await? {
7878
println!("{:?}", timestamp_value);
7979
}
@@ -114,7 +114,7 @@ session
114114
// Read timestamp from the table
115115
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
116116
.await?
117-
.into_typed::<(OffsetDateTime,)>();
117+
.rows_stream::<(OffsetDateTime,)>()?;
118118
while let Some((timestamp_value,)) = iter.try_next().await? {
119119
println!("{:?}", timestamp_value);
120120
}

docs/source/data-types/timeuuid.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ session
2424
// Read Timeuuid from the table
2525
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
2626
.await?
27-
.into_typed::<(CqlTimeuuid, )>();
27+
.rows_stream::<(CqlTimeuuid, )>()?;
2828

2929
while let Some((timeuuid,)) = iter.try_next().await? {
3030
println!("Read a value from row: {}", timeuuid);
@@ -68,7 +68,7 @@ session
6868
// Read Timeuuid from the table
6969
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
7070
.await?
71-
.into_typed::<(CqlTimeuuid, )>();
71+
.rows_stream::<(CqlTimeuuid, )>()?;
7272

7373
while let Some((timeuuid,)) = iter.try_next().await? {
7474
println!("Read a value from row: {}", timeuuid);

docs/source/data-types/tuple.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ session
1919
// Read a tuple of int and string from the table
2020
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
2121
.await?
22-
.into_typed::<((i32, String),)>();
22+
.rows_stream::<((i32, String),)>()?;
2323
while let Some((tuple_value,)) = iter.try_next().await? {
2424
let int_value: i32 = tuple_value.0;
2525
let string_value: String = tuple_value.1;

docs/source/data-types/udt.md

+12-16
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ CREATE TYPE ks.my_type (int_val int, text_val text)
1010

1111
To use this type in the driver, create a matching struct and derive:
1212
- `SerializeValue`: in order to be able to use this struct in query parameters. \
13-
This macro requires fields of UDT and struct to have matching names, but the order
14-
of the fields is not required to be the same. \
15-
Note: you can use different name using `rename` attribute - see `SerializeValue` macro documentation.
16-
- `FromUserType`: in order to be able to use this struct in query results. \
17-
This macro requires fields of UDT and struct to be in the same *ORDER*. \
18-
This mismatch between `SerializeValue` and `FromUserType` requirements is a temporary situation - in the future `FromUserType` (or the macro that replaces it) will also require matching names.
13+
- `DeserializeValue`: in order to be able to use this struct in query results. \
14+
15+
Both macros require fields of UDT and struct to have matching names, but the order
16+
of the fields is not required to be the same. \
17+
Note: you can use different name using `rename` attribute - see `SerializeValue`
18+
and `DeserializeValue` macros documentation.
1919

2020
```rust
2121
# extern crate scylla;
@@ -35,13 +35,9 @@ struct MyType {
3535
```
3636

3737
> ***Important***\
38-
> For deserialization, fields in the Rust struct must be defined in the same order as they are in the database.
39-
> When receiving values, the driver will (de)serialize fields one after another, without looking at field names.
40-
41-
> ***Important***\
42-
> For serialization, by default fields in the Rust struct must be defined with the same names as they are in the database.
43-
> The driver will serialize the fields in the order defined by the UDT, matching Rust fields by name.
44-
> You can change this behaviour using macro attributes, see `SerializeValue` macro documentation for more information.
38+
> For (de)serialization, by default fields in the Rust struct must be defined with the same names as they are in the database.
39+
> The driver will (de)serialize the fields in the order defined by the UDT, matching Rust fields by name.
40+
> You can change this behaviour using macro attributes, see `SerializeValue`/`DeserializeValue` macro documentation for more information.
4541
4642
Now it can be sent and received just like any other CQL value:
4743
```rust
@@ -51,10 +47,10 @@ Now it can be sent and received just like any other CQL value:
5147
# use std::error::Error;
5248
# async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
5349
use futures::TryStreamExt;
54-
use scylla::macros::{FromUserType, SerializeValue};
50+
use scylla::macros::{DeserializeValue, SerializeValue};
5551
use scylla::cql_to_rust::FromCqlVal;
5652

57-
#[derive(Debug, FromUserType, SerializeValue)]
53+
#[derive(Debug, DeserializeValue, SerializeValue)]
5854
struct MyType {
5955
int_val: i32,
6056
text_val: Option<String>,
@@ -73,7 +69,7 @@ session
7369
// Read MyType from the table
7470
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
7571
.await?
76-
.into_typed::<(MyType,)>();
72+
.rows_stream::<(MyType,)>()?;
7773
while let Some((my_type_value,)) = iter.try_next().await? {
7874
println!("{:?}", my_type_value);
7975
}

docs/source/data-types/uuid.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ session
2121
// Read uuid from the table
2222
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
2323
.await?
24-
.into_typed::<(Uuid,)>();
24+
.rows_stream::<(Uuid,)>()?;
2525
while let Some((uuid_value,)) = iter.try_next().await? {
2626
println!("{:?}", uuid_value);
2727
}

docs/source/data-types/varint.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ session
3232
// Read a varint from the table
3333
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
3434
.await?
35-
.into_typed::<(BigInt,)>();
35+
.rows_stream::<(BigInt,)>()?;
3636
while let Some((varint_value,)) = iter.try_next().await? {
3737
println!("{:?}", varint_value);
3838
}

0 commit comments

Comments
 (0)