Skip to content

Commit 9e8d34a

Browse files
committed
Update example to bincode 2.0
1 parent 1afb93f commit 9e8d34a

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ tempfile = "3.5.0"
3232
redb1 = { version = "=1.0.0", package = "redb" }
3333
# for backwards compatibility testing - pin at 2.0.0
3434
redb2 = { version = "=2.0.0", package = "redb" }
35-
serde = { version = "1.0", features = ["derive"] }
36-
bincode = "1.3.3"
35+
bincode = "2.0.1"
3736
walkdir = "2.5.0"
3837
byte-unit = "5.1.6"
3938
fastrand = "2.0.0"

examples/bincode_keys.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ use std::any::type_name;
22
use std::cmp::Ordering;
33
use std::fmt::Debug;
44

5-
use bincode::{deserialize, serialize};
5+
use bincode::{Decode, Encode, decode_from_slice, encode_to_vec};
66
use redb::{Database, Error, Key, Range, TableDefinition, TypeName, Value};
7-
use serde::{Deserialize, Serialize, de::DeserializeOwned};
87

9-
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
8+
#[derive(Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord)]
109
struct SomeKey {
1110
foo: String,
1211
bar: i32,
1312
}
1413

15-
#[derive(Debug, Serialize, Deserialize, PartialEq)]
14+
#[derive(Debug, Decode, Encode, PartialEq)]
1615
struct SomeValue {
1716
foo: [f64; 3],
1817
bar: bool,
@@ -64,7 +63,7 @@ pub struct Bincode<T>(pub T);
6463

6564
impl<T> Value for Bincode<T>
6665
where
67-
T: Debug + Serialize + for<'a> Deserialize<'a>,
66+
T: Debug + Encode + Decode<()>,
6867
{
6968
type SelfType<'a>
7069
= T
@@ -84,15 +83,17 @@ where
8483
where
8584
Self: 'a,
8685
{
87-
deserialize(data).unwrap()
86+
decode_from_slice(data, bincode::config::standard())
87+
.unwrap()
88+
.0
8889
}
8990

9091
fn as_bytes<'a, 'b: 'a>(value: &'a Self::SelfType<'b>) -> Self::AsBytes<'a>
9192
where
9293
Self: 'a,
9394
Self: 'b,
9495
{
95-
serialize(value).unwrap()
96+
encode_to_vec(value, bincode::config::standard()).unwrap()
9697
}
9798

9899
fn type_name() -> TypeName {
@@ -102,7 +103,7 @@ where
102103

103104
impl<T> Key for Bincode<T>
104105
where
105-
T: Debug + Serialize + DeserializeOwned + Ord,
106+
T: Debug + Decode<()> + Encode + Ord,
106107
{
107108
fn compare(data1: &[u8], data2: &[u8]) -> Ordering {
108109
Self::from_bytes(data1).cmp(&Self::from_bytes(data2))

0 commit comments

Comments
 (0)