Skip to content

Commit 6cdb012

Browse files
committed
Use derive(new) in a few more places
Signed-off-by: Nick Cameron <nrc@ncameron.org>
1 parent 26cf1da commit 6cdb012

File tree

6 files changed

+19
-87
lines changed

6 files changed

+19
-87
lines changed

src/kv.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
22

3+
use derive_new::new;
34
use std::cmp::{Eq, PartialEq};
45
use std::convert::TryFrom;
56
use std::ops::{Bound, Deref, DerefMut, Range, RangeFrom, RangeInclusive};
@@ -55,15 +56,10 @@ impl<'a> fmt::Display for HexRepr<'a> {
5556
/// **But, you should not need to worry about all this:** Many functions which accept a `Key`
5657
/// accept an `Into<Key>`, which means all of the above types can be passed directly to those
5758
/// functions.
58-
#[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
59+
#[derive(new, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
5960
pub struct Key(Vec<u8>);
6061

6162
impl Key {
62-
#[inline]
63-
pub fn new(value: Vec<u8>) -> Self {
64-
Key(value)
65-
}
66-
6763
#[inline]
6864
pub(crate) fn into_inner(self) -> Vec<u8> {
6965
self.0
@@ -199,15 +195,10 @@ impl fmt::Debug for Key {
199195
/// **But, you should not need to worry about all this:** Many functions which accept a `Value`
200196
/// accept an `Into<Value>`, which means all of the above types can be passed directly to those
201197
/// functions.
202-
#[derive(Default, Clone, Eq, PartialEq, Hash)]
198+
#[derive(new, Default, Clone, Eq, PartialEq, Hash)]
203199
pub struct Value(Vec<u8>);
204200

205201
impl Value {
206-
#[inline]
207-
pub fn new(value: Vec<u8>) -> Self {
208-
Value(value)
209-
}
210-
211202
#[inline]
212203
pub(crate) fn into_inner(self) -> Vec<u8> {
213204
self.0

src/raw/client.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use super::ColumnFamily;
44
use crate::{rpc::RpcClient, BoundRange, Config, Error, Key, KvPair, Result, Value};
55

6+
use derive_new::new;
67
use futures::future::Either;
78
use futures::prelude::*;
89
use futures::task::{Context, Poll};
@@ -356,16 +357,11 @@ impl Client {
356357
/// let client: Client = connect.await.unwrap();
357358
/// # });
358359
/// ```
360+
#[derive(new)]
359361
pub struct Connect {
360362
config: Config,
361363
}
362364

363-
impl Connect {
364-
fn new(config: Config) -> Self {
365-
Connect { config }
366-
}
367-
}
368-
369365
impl Future for Connect {
370366
type Output = Result<Client>;
371367

src/rpc/pd/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// TODO: Remove this when txn is done.
44
#![allow(dead_code)]
55

6+
use derive_new::new;
67
pub use kvproto::metapb::{Peer, Store};
78
use kvproto::{kvrpcpb, metapb};
89

@@ -25,17 +26,13 @@ pub struct RegionVerId {
2526
pub ver: u64,
2627
}
2728

28-
#[derive(Clone, Default, Debug, PartialEq)]
29+
#[derive(new, Clone, Default, Debug, PartialEq)]
2930
pub struct Region {
3031
pub region: metapb::Region,
3132
pub leader: Option<Peer>,
3233
}
3334

3435
impl Region {
35-
pub fn new(region: metapb::Region, leader: Option<metapb::Peer>) -> Self {
36-
Region { region, leader }
37-
}
38-
3936
pub fn switch_peer(&mut self, _to: StoreId) -> Result<()> {
4037
unimplemented!()
4138
}

src/transaction/client.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use super::{Snapshot, Timestamp, Transaction};
44
use crate::{Config, Error};
55

6+
use derive_new::new;
67
use futures::prelude::*;
78
use futures::task::{Context, Poll};
89
use std::pin::Pin;
@@ -115,16 +116,11 @@ impl Client {
115116
/// let client: Client = connect.await.unwrap();
116117
/// # });
117118
/// ```
119+
#[derive(new)]
118120
pub struct Connect {
119121
config: Config,
120122
}
121123

122-
impl Connect {
123-
fn new(config: Config) -> Self {
124-
Connect { config }
125-
}
126-
}
127-
128124
impl Future for Connect {
129125
type Output = Result<Client, Error>;
130126

src/transaction/requests.rs

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use super::Transaction;
44
use crate::{Error, Key, KvPair, Value};
55

6+
use derive_new::new;
67
use futures::prelude::*;
78
use futures::task::{Context, Poll};
89
use std::pin::Pin;
@@ -24,16 +25,11 @@ impl Stream for Scanner {
2425
///
2526
/// Once resolved this request will result in the fetching of the value associated with the given
2627
/// key.
28+
#[derive(new)]
2729
pub struct Get {
2830
key: Key,
2931
}
3032

31-
impl Get {
32-
pub fn new(key: Key) -> Self {
33-
Get { key }
34-
}
35-
}
36-
3733
impl Future for Get {
3834
type Output = Result<Value, Error>;
3935

@@ -47,16 +43,11 @@ impl Future for Get {
4743
///
4844
/// Once resolved this request will result in the fetching of the values associated with the given
4945
/// keys.
46+
#[derive(new)]
5047
pub struct BatchGet {
5148
keys: Vec<Key>,
5249
}
5350

54-
impl BatchGet {
55-
pub fn new(keys: Vec<Key>) -> Self {
56-
BatchGet { keys }
57-
}
58-
}
59-
6051
impl Future for BatchGet {
6152
type Output = Result<Vec<KvPair>, Error>;
6253

@@ -69,16 +60,11 @@ impl Future for BatchGet {
6960
/// An unresolved [`Transaction::commit`](Transaction::commit) request.
7061
///
7162
/// Once resolved this request will result in the committing of the transaction.
63+
#[derive(new)]
7264
pub struct Commit {
7365
txn: Transaction,
7466
}
7567

76-
impl Commit {
77-
pub fn new(txn: Transaction) -> Self {
78-
Commit { txn }
79-
}
80-
}
81-
8268
impl Future for Commit {
8369
type Output = Result<(), Error>;
8470

@@ -91,16 +77,11 @@ impl Future for Commit {
9177
/// An unresolved [`Transaction::rollback`](Transaction::rollback) request.
9278
///
9379
/// Once resolved this request will result in the rolling back of the transaction.
80+
#[derive(new)]
9481
pub struct Rollback {
9582
txn: Transaction,
9683
}
9784

98-
impl Rollback {
99-
pub fn new(txn: Transaction) -> Self {
100-
Rollback { txn }
101-
}
102-
}
103-
10485
impl Future for Rollback {
10586
type Output = Result<(), Error>;
10687

@@ -113,16 +94,11 @@ impl Future for Rollback {
11394
/// An unresolved [`Transaction::lock_keys`](Transaction::lock_keys) request.
11495
///
11596
/// Once resolved this request will result in the locking of the given keys.
97+
#[derive(new)]
11698
pub struct LockKeys {
11799
keys: Vec<Key>,
118100
}
119101

120-
impl LockKeys {
121-
pub fn new(keys: Vec<Key>) -> Self {
122-
LockKeys { keys }
123-
}
124-
}
125-
126102
impl Future for LockKeys {
127103
type Output = Result<(), Error>;
128104

@@ -136,17 +112,12 @@ impl Future for LockKeys {
136112
///
137113
/// Once resolved this request will result in the setting of the value associated with the given
138114
/// key.
115+
#[derive(new)]
139116
pub struct Set {
140117
key: Key,
141118
value: Value,
142119
}
143120

144-
impl Set {
145-
pub fn new(key: Key, value: Value) -> Self {
146-
Set { key, value }
147-
}
148-
}
149-
150121
impl Future for Set {
151122
type Output = Result<(), Error>;
152123

@@ -160,16 +131,11 @@ impl Future for Set {
160131
/// An unresolved [`Transaction::delete`](Transaction::delete) request.
161132
///
162133
/// Once resolved this request will result in the deletion of the given key.
134+
#[derive(new)]
163135
pub struct Delete {
164136
key: Key,
165137
}
166138

167-
impl Delete {
168-
pub fn new(key: Key) -> Self {
169-
Delete { key }
170-
}
171-
}
172-
173139
impl Future for Delete {
174140
type Output = Result<(), Error>;
175141

src/transaction/transaction.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use super::{BatchGet, Commit, Delete, Get, LockKeys, Rollback, Scanner, Set, Timestamp};
44
use crate::{Key, Value};
55

6+
use derive_new::new;
67
use std::ops::RangeBounds;
78

89
/// A undo-able set of actions on the dataset.
@@ -11,27 +12,12 @@ use std::ops::RangeBounds;
1112
/// particular timestamp obtained from the placement driver.
1213
///
1314
/// Once a transaction is commited, a new commit timestamp is obtained from the placement driver.
15+
#[derive(new)]
1416
pub struct Transaction {
1517
snapshot: Snapshot,
1618
}
1719

1820
impl Transaction {
19-
/// Create a new transaction operating on the given snapshot.
20-
///
21-
/// ```rust,no_run
22-
/// # #![feature(async_await)]
23-
/// use tikv_client::{Config, transaction::Client};
24-
/// use futures::prelude::*;
25-
/// # futures::executor::block_on(async {
26-
/// let connect = Client::connect(Config::default());
27-
/// let client = connect.await.unwrap();
28-
/// let txn = client.begin();
29-
/// # });
30-
/// ```
31-
pub fn new(snapshot: Snapshot) -> Self {
32-
Self { snapshot }
33-
}
34-
3521
/// Commit the actions of the transaction.
3622
///
3723
/// Once committed, it is no longer possible to `rollback` the actions in the transaction.

0 commit comments

Comments
 (0)