refactor: fix nightly cargo clippy

Signed-off-by: ekexium <ekexium@gmail.com>
This commit is contained in:
ekexium 2021-01-11 16:15:49 +08:00
parent 3392e6341b
commit 578be6cff2
7 changed files with 65 additions and 63 deletions

View File

@ -5,7 +5,7 @@
mod common; mod common;
use crate::common::parse_args; use crate::common::parse_args;
use tikv_client::{Config, Key, KvPair, RawClient as Client, Result, ToOwnedRange, Value}; use tikv_client::{Config, IntoOwnedRange, Key, KvPair, RawClient as Client, Result, Value};
const KEY: &str = "TiKV"; const KEY: &str = "TiKV";
const VALUE: &str = "Rust"; const VALUE: &str = "Rust";
@ -86,7 +86,7 @@ async fn main() -> Result<()> {
let start = "k1"; let start = "k1";
let end = "k2"; let end = "k2";
let pairs = client let pairs = client
.scan((start..=end).to_owned(), 10) .scan((start..=end).into_owned(), 10)
.await .await
.expect("Could not scan"); .expect("Could not scan");

View File

@ -83,41 +83,41 @@ impl BoundRange {
/// ///
/// # Examples /// # Examples
/// ```rust /// ```rust
/// use tikv_client::{BoundRange, Key, ToOwnedRange}; /// use tikv_client::{BoundRange, Key, IntoOwnedRange};
/// // Exclusive /// // Exclusive
/// let range = "a".."z"; /// let range = "a".."z";
/// assert_eq!( /// assert_eq!(
/// BoundRange::from(range.to_owned()).into_keys(), /// BoundRange::from(range.into_owned()).into_keys(),
/// (Key::from("a".to_owned()), Some(Key::from("z".to_owned()))), /// (Key::from("a".to_owned()), Some(Key::from("z".to_owned()))),
/// ); /// );
/// // Inclusive /// // Inclusive
/// let range = "a"..="z"; /// let range = "a"..="z";
/// assert_eq!( /// assert_eq!(
/// BoundRange::from(range.to_owned()).into_keys(), /// BoundRange::from(range.into_owned()).into_keys(),
/// (Key::from("a".to_owned()), Some(Key::from("z\0".to_owned()))), /// (Key::from("a".to_owned()), Some(Key::from("z\0".to_owned()))),
/// ); /// );
/// // Open right /// // Open right
/// let range = "a".to_owned()..; /// let range = "a".to_owned()..;
/// assert_eq!( /// assert_eq!(
/// BoundRange::from(range.to_owned()).into_keys(), /// BoundRange::from(range).into_keys(),
/// (Key::from("a".to_owned()), None), /// (Key::from("a".to_owned()), None),
/// ); /// );
/// // Left open right exclusive /// // Left open right exclusive
/// let range = .."z"; /// let range = .."z";
/// assert_eq!( /// assert_eq!(
/// BoundRange::from(range.to_owned()).into_keys(), /// BoundRange::from(range.into_owned()).into_keys(),
/// (Key::from("".to_owned()), Some(Key::from("z".to_owned()))), /// (Key::from("".to_owned()), Some(Key::from("z".to_owned()))),
/// ); /// );
/// // Left open right inclusive /// // Left open right inclusive
/// let range = ..="z"; /// let range = ..="z";
/// assert_eq!( /// assert_eq!(
/// BoundRange::from(range.to_owned()).into_keys(), /// BoundRange::from(range.into_owned()).into_keys(),
/// (Key::from("".to_owned()), Some(Key::from("z\0".to_owned()))), /// (Key::from("".to_owned()), Some(Key::from("z\0".to_owned()))),
/// ); /// );
/// // Full range /// // Full range
/// let range = ..; /// let range = ..;
/// assert_eq!( /// assert_eq!(
/// BoundRange::from(range.to_owned()).into_keys(), /// BoundRange::from(range).into_keys(),
/// (Key::from("".to_owned()), None), /// (Key::from("".to_owned()), None),
/// ); /// );
// ``` // ```
@ -249,9 +249,9 @@ impl<T: Into<Key> + Eq> From<(Bound<T>, Bound<T>)> for BoundRange {
} }
} }
impl Into<kvrpcpb::KeyRange> for BoundRange { impl From<BoundRange> for kvrpcpb::KeyRange {
fn into(self) -> kvrpcpb::KeyRange { fn from(bound_range: BoundRange) -> Self {
let (start, end) = self.into_keys(); let (start, end) = bound_range.into_keys();
let mut range = kvrpcpb::KeyRange::default(); let mut range = kvrpcpb::KeyRange::default();
range.set_start_key(start.into()); range.set_start_key(start.into());
range.set_end_key(end.unwrap_or_default().into()); range.set_end_key(end.unwrap_or_default().into());
@ -271,36 +271,36 @@ impl From<kvrpcpb::KeyRange> for BoundRange {
/// ///
/// # Examples /// # Examples
/// ```rust /// ```rust
/// # use tikv_client::{ToOwnedRange, BoundRange}; /// # use tikv_client::{IntoOwnedRange, BoundRange};
/// # use std::ops::*; /// # use std::ops::*;
/// let r1: Range<&str> = "s".."e"; /// let r1: Range<&str> = "s".."e";
/// let r1: BoundRange = r1.to_owned(); /// let r1: BoundRange = r1.into_owned();
/// ///
/// let r2: RangeFrom<&str> = "start"..; /// let r2: RangeFrom<&str> = "start"..;
/// let r2: BoundRange = r2.to_owned(); /// let r2: BoundRange = r2.into_owned();
/// ///
/// let r3: RangeInclusive<&str> = "s"..="e"; /// let r3: RangeInclusive<&str> = "s"..="e";
/// let r3: BoundRange = r3.to_owned(); /// let r3: BoundRange = r3.into_owned();
/// ///
/// let r4: RangeTo<&str> = .."z"; /// let r4: RangeTo<&str> = .."z";
/// let r4: BoundRange = r4.to_owned(); /// let r4: BoundRange = r4.into_owned();
/// ///
/// let k1: Vec<u8> = "start".to_owned().into_bytes(); /// let k1: Vec<u8> = "start".to_owned().into_bytes();
/// let k2: Vec<u8> = "end".to_owned().into_bytes(); /// let k2: Vec<u8> = "end".to_owned().into_bytes();
/// let r4: BoundRange = (&k1, &k2).to_owned(); /// let r4: BoundRange = (&k1, &k2).into_owned();
/// let r5: BoundRange = (&k1, None).to_owned(); /// let r5: BoundRange = (&k1, None).into_owned();
/// let r6: BoundRange = (&k1, Some(&k2)).to_owned(); /// let r6: BoundRange = (&k1, Some(&k2)).into_owned();
/// ``` /// ```
pub trait ToOwnedRange { pub trait IntoOwnedRange {
/// Transform a borrowed range of some form into an owned `BoundRange`. /// Transform a borrowed range of some form into an owned `BoundRange`.
fn to_owned(self) -> BoundRange; fn into_owned(self) -> BoundRange;
} }
#[test] #[test]
fn test_to_owned() {} fn test_to_owned() {}
impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> ToOwnedRange for Range<&U> { impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> IntoOwnedRange for Range<&U> {
fn to_owned(self) -> BoundRange { fn into_owned(self) -> BoundRange {
From::from(Range { From::from(Range {
start: self.start.to_owned(), start: self.start.to_owned(),
end: self.end.to_owned(), end: self.end.to_owned(),
@ -308,53 +308,55 @@ impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> ToOwnedRange for
} }
} }
impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> ToOwnedRange for RangeFrom<&U> { impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> IntoOwnedRange for RangeFrom<&U> {
fn to_owned(self) -> BoundRange { fn into_owned(self) -> BoundRange {
From::from(RangeFrom { From::from(RangeFrom {
start: self.start.to_owned(), start: self.start.to_owned(),
}) })
} }
} }
impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> ToOwnedRange for RangeTo<&U> { impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> IntoOwnedRange for RangeTo<&U> {
fn to_owned(self) -> BoundRange { fn into_owned(self) -> BoundRange {
From::from(RangeTo { From::from(RangeTo {
end: self.end.to_owned(), end: self.end.to_owned(),
}) })
} }
} }
impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> ToOwnedRange for RangeInclusive<&U> { impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> IntoOwnedRange
fn to_owned(self) -> BoundRange { for RangeInclusive<&U>
{
fn into_owned(self) -> BoundRange {
let (from, to) = self.into_inner(); let (from, to) = self.into_inner();
From::from(RangeInclusive::new(from.to_owned(), to.to_owned())) From::from(RangeInclusive::new(from.to_owned(), to.to_owned()))
} }
} }
impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> ToOwnedRange impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> IntoOwnedRange
for RangeToInclusive<&U> for RangeToInclusive<&U>
{ {
fn to_owned(self) -> BoundRange { fn into_owned(self) -> BoundRange {
From::from(RangeToInclusive { From::from(RangeToInclusive {
end: self.end.to_owned(), end: self.end.to_owned(),
}) })
} }
} }
impl ToOwnedRange for RangeFull { impl IntoOwnedRange for RangeFull {
fn to_owned(self) -> BoundRange { fn into_owned(self) -> BoundRange {
From::from(self) From::from(self)
} }
} }
impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> ToOwnedRange for (&U, Option<&U>) { impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> IntoOwnedRange for (&U, Option<&U>) {
fn to_owned(self) -> BoundRange { fn into_owned(self) -> BoundRange {
From::from((self.0.to_owned(), self.1.map(|u| u.to_owned()))) From::from((self.0.to_owned(), self.1.map(|u| u.to_owned())))
} }
} }
impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> ToOwnedRange for (&U, &U) { impl<T: Into<Key> + Borrow<U>, U: ToOwned<Owned = T> + ?Sized> IntoOwnedRange for (&U, &U) {
fn to_owned(self) -> BoundRange { fn into_owned(self) -> BoundRange {
From::from((self.0.to_owned(), self.1.to_owned())) From::from((self.0.to_owned(), self.1.to_owned()))
} }
} }

View File

@ -140,15 +140,15 @@ impl From<String> for Key {
} }
} }
impl Into<Vec<u8>> for Key { impl From<Key> for Vec<u8> {
fn into(self) -> Vec<u8> { fn from(key: Key) -> Self {
self.0 key.0
} }
} }
impl<'a> Into<&'a [u8]> for &'a Key { impl<'a> From<&'a Key> for &'a [u8] {
fn into(self) -> &'a [u8] { fn from(key: &'a Key) -> Self {
&self.0 &key.0
} }
} }

View File

@ -90,9 +90,9 @@ where
} }
} }
impl Into<(Key, Value)> for KvPair { impl From<KvPair> for (Key, Value) {
fn into(self) -> (Key, Value) { fn from(pair: KvPair) -> Self {
(self.0, self.1) (pair.0, pair.1)
} }
} }
@ -102,10 +102,10 @@ impl From<kvrpcpb::KvPair> for KvPair {
} }
} }
impl Into<kvrpcpb::KvPair> for KvPair { impl From<KvPair> for kvrpcpb::KvPair {
fn into(self) -> kvrpcpb::KvPair { fn from(pair: KvPair) -> Self {
let mut result = kvrpcpb::KvPair::default(); let mut result = kvrpcpb::KvPair::default();
let (key, value) = self.into(); let (key, value) = pair.into();
result.set_key(key.into()); result.set_key(key.into());
result.set_value(value); result.set_value(value);
result result

View File

@ -7,7 +7,7 @@ mod key;
mod kvpair; mod kvpair;
mod value; mod value;
pub use bound_range::{BoundRange, ToOwnedRange}; pub use bound_range::{BoundRange, IntoOwnedRange};
pub use key::Key; pub use key::Key;
pub use kvpair::KvPair; pub use kvpair::KvPair;
pub use value::Value; pub use value::Value;

View File

@ -101,7 +101,7 @@ extern crate log;
#[doc(inline)] #[doc(inline)]
pub use crate::backoff::Backoff; pub use crate::backoff::Backoff;
#[doc(inline)] #[doc(inline)]
pub use crate::kv::{BoundRange, Key, KvPair, ToOwnedRange, Value}; pub use crate::kv::{BoundRange, IntoOwnedRange, Key, KvPair, Value};
#[doc(inline)] #[doc(inline)]
pub use crate::raw::{Client as RawClient, ColumnFamily}; pub use crate::raw::{Client as RawClient, ColumnFamily};
#[doc(inline)] #[doc(inline)]

View File

@ -170,7 +170,7 @@ impl Client {
/// ///
/// # Examples /// # Examples
/// ```rust,no_run /// ```rust,no_run
/// # use tikv_client::{Result, KvPair, Key, Value, Config, RawClient, ToOwnedRange}; /// # use tikv_client::{Result, KvPair, Key, Value, Config, RawClient, IntoOwnedRange};
/// # use futures::prelude::*; /// # use futures::prelude::*;
/// # futures::executor::block_on(async { /// # futures::executor::block_on(async {
/// # let client = RawClient::new(vec!["192.168.0.100"]).await.unwrap(); /// # let client = RawClient::new(vec!["192.168.0.100"]).await.unwrap();
@ -242,12 +242,12 @@ impl Client {
/// ///
/// # Examples /// # Examples
/// ```rust,no_run /// ```rust,no_run
/// # use tikv_client::{Key, Config, RawClient, ToOwnedRange}; /// # use tikv_client::{Key, Config, RawClient, IntoOwnedRange};
/// # use futures::prelude::*; /// # use futures::prelude::*;
/// # futures::executor::block_on(async { /// # futures::executor::block_on(async {
/// # let client = RawClient::new(vec!["192.168.0.100"]).await.unwrap(); /// # let client = RawClient::new(vec!["192.168.0.100"]).await.unwrap();
/// let inclusive_range = "TiKV"..="TiDB"; /// let inclusive_range = "TiKV"..="TiDB";
/// let req = client.delete_range(inclusive_range.to_owned()); /// let req = client.delete_range(inclusive_range.into_owned());
/// let result: () = req.await.unwrap(); /// let result: () = req.await.unwrap();
/// # }); /// # });
/// ``` /// ```
@ -267,12 +267,12 @@ impl Client {
/// ///
/// # Examples /// # Examples
/// ```rust,no_run /// ```rust,no_run
/// # use tikv_client::{KvPair, Config, RawClient, ToOwnedRange}; /// # use tikv_client::{KvPair, Config, RawClient, IntoOwnedRange};
/// # use futures::prelude::*; /// # use futures::prelude::*;
/// # futures::executor::block_on(async { /// # futures::executor::block_on(async {
/// # let client = RawClient::new(vec!["192.168.0.100"]).await.unwrap(); /// # let client = RawClient::new(vec!["192.168.0.100"]).await.unwrap();
/// let inclusive_range = "TiKV"..="TiDB"; /// let inclusive_range = "TiKV"..="TiDB";
/// let req = client.scan(inclusive_range.to_owned(), 2); /// let req = client.scan(inclusive_range.into_owned(), 2);
/// let result: Vec<KvPair> = req.await.unwrap(); /// let result: Vec<KvPair> = req.await.unwrap();
/// # }); /// # });
/// ``` /// ```
@ -290,12 +290,12 @@ impl Client {
/// ///
/// # Examples /// # Examples
/// ```rust,no_run /// ```rust,no_run
/// # use tikv_client::{Key, Config, RawClient, ToOwnedRange}; /// # use tikv_client::{Key, Config, RawClient, IntoOwnedRange};
/// # use futures::prelude::*; /// # use futures::prelude::*;
/// # futures::executor::block_on(async { /// # futures::executor::block_on(async {
/// # let client = RawClient::new(vec!["192.168.0.100"]).await.unwrap(); /// # let client = RawClient::new(vec!["192.168.0.100"]).await.unwrap();
/// let inclusive_range = "TiKV"..="TiDB"; /// let inclusive_range = "TiKV"..="TiDB";
/// let req = client.scan_keys(inclusive_range.to_owned(), 2); /// let req = client.scan_keys(inclusive_range.into_owned(), 2);
/// let result: Vec<Key> = req.await.unwrap(); /// let result: Vec<Key> = req.await.unwrap();
/// # }); /// # });
/// ``` /// ```
@ -320,13 +320,13 @@ impl Client {
/// ///
/// # Examples /// # Examples
/// ```rust,no_run /// ```rust,no_run
/// # use tikv_client::{Key, Config, RawClient, ToOwnedRange}; /// # use tikv_client::{Key, Config, RawClient, IntoOwnedRange};
/// # use futures::prelude::*; /// # use futures::prelude::*;
/// # futures::executor::block_on(async { /// # futures::executor::block_on(async {
/// # let client = RawClient::new(vec!["192.168.0.100"]).await.unwrap(); /// # let client = RawClient::new(vec!["192.168.0.100"]).await.unwrap();
/// let inclusive_range1 = "TiDB"..="TiKV"; /// let inclusive_range1 = "TiDB"..="TiKV";
/// let inclusive_range2 = "TiKV"..="TiSpark"; /// let inclusive_range2 = "TiKV"..="TiSpark";
/// let iterable = vec![inclusive_range1.to_owned(), inclusive_range2.to_owned()]; /// let iterable = vec![inclusive_range1.into_owned(), inclusive_range2.into_owned()];
/// let req = client.batch_scan(iterable, 2); /// let req = client.batch_scan(iterable, 2);
/// let result = req.await; /// let result = req.await;
/// # }); /// # });
@ -351,13 +351,13 @@ impl Client {
/// ///
/// # Examples /// # Examples
/// ```rust,no_run /// ```rust,no_run
/// # use tikv_client::{Key, Config, RawClient, ToOwnedRange}; /// # use tikv_client::{Key, Config, RawClient, IntoOwnedRange};
/// # use futures::prelude::*; /// # use futures::prelude::*;
/// # futures::executor::block_on(async { /// # futures::executor::block_on(async {
/// # let client = RawClient::new(vec!["192.168.0.100"]).await.unwrap(); /// # let client = RawClient::new(vec!["192.168.0.100"]).await.unwrap();
/// let inclusive_range1 = "TiDB"..="TiKV"; /// let inclusive_range1 = "TiDB"..="TiKV";
/// let inclusive_range2 = "TiKV"..="TiSpark"; /// let inclusive_range2 = "TiKV"..="TiSpark";
/// let iterable = vec![inclusive_range1.to_owned(), inclusive_range2.to_owned()]; /// let iterable = vec![inclusive_range1.into_owned(), inclusive_range2.into_owned()];
/// let req = client.batch_scan(iterable, 2); /// let req = client.batch_scan(iterable, 2);
/// let result = req.await; /// let result = req.await;
/// # }); /// # });