Skip to content

Commit 6416d5a

Browse files
committed
Avoid boxing futures
Signed-off-by: Nick Cameron <nrc@ncameron.org>
1 parent f9ddd4a commit 6416d5a

File tree

2 files changed

+35
-41
lines changed

2 files changed

+35
-41
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// Long and nested future chains can quickly result in large generic types.
44
#![type_length_limit = "16777216"]
5+
#![feature(existential_type)]
56
#![allow(clippy::redundant_closure)]
67

78
//! This crate provides a clean, ready to use client for [TiKV](https://github.com/tikv/tikv), a

src/raw/requests.rs

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22

33
use super::ColumnFamily;
44
use crate::{rpc::RpcClient, BoundRange, Error, Key, KvPair, Result, Value};
5+
use futures::future::Either;
56
use futures::prelude::*;
67
use futures::task::{Context, Poll};
78
use std::{pin::Pin, sync::Arc, u32};
89

910
const MAX_RAW_KV_SCAN_LIMIT: u32 = 10240;
1011

11-
type BoxTryFuture<Resp> = Box<dyn Future<Output = Result<Resp>> + Send>;
12-
1312
trait RequestInner: Sized {
1413
type Resp;
14+
type F: Future<Output = Result<Self::Resp>>;
1515

16-
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> BoxTryFuture<Self::Resp>;
16+
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> Self::F;
1717
}
1818

1919
enum RequestState<Inner>
2020
where
2121
Inner: RequestInner,
2222
{
2323
Uninitiated(Option<(Arc<RpcClient>, Inner, Option<ColumnFamily>)>),
24-
Initiated(BoxTryFuture<Inner::Resp>),
24+
Initiated(Inner::F),
2525
}
2626

2727
impl<Inner> RequestState<Inner>
@@ -61,7 +61,7 @@ where
6161
) -> Pin<&'a mut dyn Future<Output = Result<Inner::Resp>>> {
6262
unsafe {
6363
match Pin::get_unchecked_mut(self) {
64-
RequestState::Initiated(future) => Pin::new_unchecked(&mut **future),
64+
RequestState::Initiated(future) => Pin::new_unchecked(&mut *future),
6565
_ => unreachable!(),
6666
}
6767
}
@@ -109,13 +109,10 @@ struct GetInner {
109109

110110
impl RequestInner for GetInner {
111111
type Resp = Option<Value>;
112+
existential type F: Future<Output = Result<Option<Value>>>;
112113

113-
fn execute(
114-
self,
115-
client: Arc<RpcClient>,
116-
cf: Option<ColumnFamily>,
117-
) -> BoxTryFuture<Option<Value>> {
118-
Box::new(client.raw_get(self.key, cf))
114+
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> Self::F {
115+
client.raw_get(self.key, cf)
119116
}
120117
}
121118

@@ -155,13 +152,10 @@ struct BatchGetInner {
155152

156153
impl RequestInner for BatchGetInner {
157154
type Resp = Vec<KvPair>;
155+
existential type F: Future<Output = Result<Vec<KvPair>>>;
158156

159-
fn execute(
160-
self,
161-
client: Arc<RpcClient>,
162-
cf: Option<ColumnFamily>,
163-
) -> BoxTryFuture<Vec<KvPair>> {
164-
Box::new(client.raw_batch_get(self.keys, cf))
157+
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> Self::F {
158+
client.raw_batch_get(self.keys, cf)
165159
}
166160
}
167161

@@ -202,10 +196,11 @@ struct PutInner {
202196

203197
impl RequestInner for PutInner {
204198
type Resp = ();
199+
existential type F: Future<Output = Result<()>>;
205200

206-
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> BoxTryFuture<()> {
201+
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> Self::F {
207202
let (key, value) = (self.key, self.value);
208-
Box::new(client.raw_put(key, value, cf))
203+
client.raw_put(key, value, cf)
209204
}
210205
}
211206

@@ -244,9 +239,10 @@ struct BatchPutInner {
244239

245240
impl RequestInner for BatchPutInner {
246241
type Resp = ();
242+
existential type F: Future<Output = Result<()>>;
247243

248-
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> BoxTryFuture<()> {
249-
Box::new(client.raw_batch_put(self.pairs, cf))
244+
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> Self::F {
245+
client.raw_batch_put(self.pairs, cf)
250246
}
251247
}
252248

@@ -285,9 +281,10 @@ struct DeleteInner {
285281

286282
impl RequestInner for DeleteInner {
287283
type Resp = ();
284+
existential type F: Future<Output = Result<()>>;
288285

289-
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> BoxTryFuture<()> {
290-
Box::new(client.raw_delete(self.key, cf))
286+
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> Self::F {
287+
client.raw_delete(self.key, cf)
291288
}
292289
}
293290

@@ -326,9 +323,10 @@ struct BatchDeleteInner {
326323

327324
impl RequestInner for BatchDeleteInner {
328325
type Resp = ();
326+
existential type F: Future<Output = Result<()>>;
329327

330-
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> BoxTryFuture<()> {
331-
Box::new(client.raw_batch_delete(self.keys, cf))
328+
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> Self::F {
329+
client.raw_batch_delete(self.keys, cf)
332330
}
333331
}
334332

@@ -386,19 +384,16 @@ impl ScanInner {
386384

387385
impl RequestInner for ScanInner {
388386
type Resp = Vec<KvPair>;
387+
existential type F: Future<Output = Result<Vec<KvPair>>>;
389388

390-
fn execute(
391-
self,
392-
client: Arc<RpcClient>,
393-
cf: Option<ColumnFamily>,
394-
) -> BoxTryFuture<Vec<KvPair>> {
389+
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> Self::F {
395390
if self.limit > MAX_RAW_KV_SCAN_LIMIT {
396-
Box::new(future::err(Error::max_scan_limit_exceeded(
391+
Either::Right(future::err(Error::max_scan_limit_exceeded(
397392
self.limit,
398393
MAX_RAW_KV_SCAN_LIMIT,
399394
)))
400395
} else {
401-
Box::new(client.raw_scan(self.range, self.limit, self.key_only, cf))
396+
Either::Left(client.raw_scan(self.range, self.limit, self.key_only, cf))
402397
}
403398
}
404399
}
@@ -457,19 +452,16 @@ impl BatchScanInner {
457452

458453
impl RequestInner for BatchScanInner {
459454
type Resp = Vec<KvPair>;
455+
existential type F: Future<Output = Result<Vec<KvPair>>>;
460456

461-
fn execute(
462-
self,
463-
client: Arc<RpcClient>,
464-
cf: Option<ColumnFamily>,
465-
) -> BoxTryFuture<Vec<KvPair>> {
457+
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> Self::F {
466458
if self.each_limit > MAX_RAW_KV_SCAN_LIMIT {
467-
Box::new(future::err(Error::max_scan_limit_exceeded(
459+
Either::Right(future::err(Error::max_scan_limit_exceeded(
468460
self.each_limit,
469461
MAX_RAW_KV_SCAN_LIMIT,
470462
)))
471463
} else {
472-
Box::new(client.raw_batch_scan(self.ranges, self.each_limit, self.key_only, cf))
464+
Either::Left(client.raw_batch_scan(self.ranges, self.each_limit, self.key_only, cf))
473465
}
474466
}
475467
}
@@ -510,8 +502,9 @@ pub struct DeleteRangeInner {
510502

511503
impl RequestInner for DeleteRangeInner {
512504
type Resp = ();
505+
existential type F: Future<Output = Result<()>>;
513506

514-
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> BoxTryFuture<()> {
515-
Box::new(client.raw_delete_range(self.range, cf))
507+
fn execute(self, client: Arc<RpcClient>, cf: Option<ColumnFamily>) -> Self::F {
508+
client.raw_delete_range(self.range, cf)
516509
}
517510
}

0 commit comments

Comments
 (0)