Skip to content

Commit 6342f16

Browse files
Introduce BorrowedOf type alias for ergonomics (#81)
1 parent 8602539 commit 6342f16

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

benches/serde.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bencher::{benchmark_group, benchmark_main, Bencher};
2-
use columnar::{Columnar, Container, Clear, FromBytes};
2+
use columnar::{Columnar, BorrowedOf, Borrow, Clear};
33
use columnar::bytes::indexed;
44
use serde::{Serialize, Deserialize};
55

@@ -70,8 +70,8 @@ fn goser_decode(b: &mut Bencher) {
7070
indexed::encode(&mut words, &container.borrow());
7171
b.bytes = 8 * words.len() as u64;
7272
b.iter(|| {
73-
let mut slices = indexed::decode(&mut words);
74-
let foo = <<Log as Columnar>::Container as Container>::Borrowed::from_bytes(&mut slices);
73+
type B<'a> = BorrowedOf<'a, Log>;
74+
let foo = indexed::decode::<B>(&words);
7575
bencher::black_box(foo);
7676
});
7777
}

examples/decode_bench.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ fn exp2_vec_u8(n: usize, iters: u64) {
7575
// Decode + access
7676
let ns_decode_access = bench_ns(iters, || {
7777
let mut slices = Indexed::decode(&store);
78-
type B<'a> = <ContainerOf<Vec<u8>> as Borrow>::Borrowed<'a>;
78+
type B<'a> = BorrowedOf<'a, Vec<u8>>;
7979
let borrowed = B::from_bytes(&mut slices);
8080
black_box(borrowed.get(idx));
8181
});
8282

8383
// Access only
8484
let slices_vec: Vec<&[u8]> = Indexed::decode(&store).collect();
8585
let mut slices_iter = slices_vec.iter().copied();
86-
type B2<'a> = <ContainerOf<Vec<u8>> as Borrow>::Borrowed<'a>;
86+
type B2<'a> = BorrowedOf<'a, Vec<u8>>;
8787
let borrowed_once = B2::from_bytes(&mut slices_iter);
8888
let ns_access_only = bench_ns(iters, || {
8989
black_box(borrowed_once.get(idx));
@@ -155,7 +155,7 @@ fn exp4_tuple1(n: usize, iters: u64) {
155155

156156
let ns = bench_ns(iters, || {
157157
let mut slices = Indexed::decode(&store);
158-
type B<'a> = <ContainerOf<(u64,)> as Borrow>::Borrowed<'a>;
158+
type B<'a> = BorrowedOf<'a, (u64,)>;
159159
let b = B::from_bytes(&mut slices);
160160
black_box(b.get(idx));
161161
});
@@ -170,7 +170,7 @@ fn exp4_tuple2(n: usize, iters: u64) {
170170

171171
let ns = bench_ns(iters, || {
172172
let mut slices = Indexed::decode(&store);
173-
type B<'a> = <ContainerOf<(u64, u64)> as Borrow>::Borrowed<'a>;
173+
type B<'a> = BorrowedOf<'a, (u64, u64)>;
174174
let b = B::from_bytes(&mut slices);
175175
let (_a, b_val) = b.get(idx);
176176
black_box(b_val);
@@ -186,7 +186,7 @@ fn exp4_tuple3(n: usize, iters: u64) {
186186

187187
let ns = bench_ns(iters, || {
188188
let mut slices = Indexed::decode(&store);
189-
type B<'a> = <ContainerOf<(u64, u64, u64)> as Borrow>::Borrowed<'a>;
189+
type B<'a> = BorrowedOf<'a, (u64, u64, u64)>;
190190
let b = B::from_bytes(&mut slices);
191191
let (_a, _b, c_val) = b.get(idx);
192192
black_box(c_val);
@@ -202,7 +202,7 @@ fn exp4_tuple5(n: usize, iters: u64) {
202202

203203
let ns = bench_ns(iters, || {
204204
let mut slices = Indexed::decode(&store);
205-
type B<'a> = <ContainerOf<(u64, u64, u64, u64, u64)> as Borrow>::Borrowed<'a>;
205+
type B<'a> = BorrowedOf<'a, (u64, u64, u64, u64, u64)>;
206206
let b = B::from_bytes(&mut slices);
207207
let (_a, _b, _c, _d, e_val) = b.get(idx);
208208
black_box(e_val);
@@ -218,7 +218,7 @@ fn exp4_tuple8(n: usize, iters: u64) {
218218

219219
let ns = bench_ns(iters, || {
220220
let mut slices = Indexed::decode(&store);
221-
type B<'a> = <ContainerOf<(u64, u64, u64, u64, u64, u64, u64, u64)> as Borrow>::Borrowed<'a>;
221+
type B<'a> = BorrowedOf<'a, (u64, u64, u64, u64, u64, u64, u64, u64)>;
222222
let b = B::from_bytes(&mut slices);
223223
let (_a, _b, _c, _d, _e, _f, _g, h_val) = b.get(idx);
224224
black_box(h_val);

src/bytes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ pub mod indexed {
281281
let mut store = Vec::new();
282282
encode(&mut store, &column.borrow());
283283

284-
type B<'a> = <ContainerOf<(u64, u64, u64)> as crate::Borrow>::Borrowed<'a>;
284+
type B<'a> = crate::BorrowedOf<'a, (u64, u64, u64)>;
285285
assert!(super::validate::<B>(&store).is_ok());
286286

287287
// Wrong slice count should fail structural validation.
@@ -299,7 +299,7 @@ pub mod indexed {
299299
let mut store = Vec::new();
300300
encode(&mut store, &column.borrow());
301301

302-
type B<'a> = <ContainerOf<(u64, String, Vec<u32>)> as crate::Borrow>::Borrowed<'a>;
302+
type B<'a> = crate::BorrowedOf<'a, (u64, String, Vec<u32>)>;
303303
assert!(super::validate::<B>(&store).is_ok());
304304
}
305305
}
@@ -479,7 +479,7 @@ mod test {
479479
let mut store = Vec::new();
480480
crate::bytes::indexed::encode(&mut store, &column.borrow());
481481
let ds = crate::bytes::indexed::DecodedStore::new(&store);
482-
type Borrowed<'a> = <ContainerOf<(u64, String, Vec<u32>)> as crate::Borrow>::Borrowed<'a>;
482+
type Borrowed<'a> = crate::BorrowedOf<'a, (u64, String, Vec<u32>)>;
483483
let reconstructed = Borrowed::from_store(&ds, &mut 0);
484484
for i in 0..50 {
485485
let (a, b, _c) = reconstructed.get(i);

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ pub trait Columnar : 'static {
8686
/// Equivalent to `<T as Columnar>::Container`.
8787
pub type ContainerOf<T> = <T as Columnar>::Container;
8888

89+
/// The borrowed container type of columnar type `T`.
90+
///
91+
/// Equivalent to `<<T as Columnar>::Container> as Borrow>::Borrowed<'a>`.
92+
pub type BorrowedOf<'a, T> = <ContainerOf<T> as Borrow>::Borrowed<'a>;
93+
8994
/// For a lifetime, the reference type of columnar type `T`.
9095
///
9196
/// Equivalent to `<ContainerOf<T> as Borrow>::Ref<'a>`.

0 commit comments

Comments
 (0)