tikv_client/kv/kvpair.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
use std::fmt;
use std::str;
#[cfg(test)]
use proptest_derive::Arbitrary;
use super::HexRepr;
use super::Key;
use super::Value;
use crate::proto::kvrpcpb;
/// A key/value pair.
///
/// # Examples
/// ```rust
/// # use tikv_client::{Key, Value, KvPair};
/// let key = "key".to_owned();
/// let value = "value".to_owned();
/// let constructed = KvPair::new(key.clone(), value.clone());
/// let from_tuple = KvPair::from((key, value));
/// assert_eq!(constructed, from_tuple);
/// ```
///
/// Many functions which accept a `KvPair` accept an `Into<KvPair>`, which means all of the above
/// types (Like a `(Key, Value)`) can be passed directly to those functions.
#[derive(Default, Clone, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
pub struct KvPair(pub Key, pub Value);
impl KvPair {
/// Create a new `KvPair`.
#[inline]
pub fn new(key: impl Into<Key>, value: impl Into<Value>) -> Self {
KvPair(key.into(), value.into())
}
/// Immutably borrow the `Key` part of the `KvPair`.
#[inline]
pub fn key(&self) -> &Key {
&self.0
}
/// Immutably borrow the `Value` part of the `KvPair`.
#[inline]
pub fn value(&self) -> &Value {
&self.1
}
/// Consume `self` and return the `Key` part.
#[inline]
pub fn into_key(self) -> Key {
self.0
}
/// Consume `self` and return the `Value` part.
#[inline]
pub fn into_value(self) -> Value {
self.1
}
/// Mutably borrow the `Key` part of the `KvPair`.
#[inline]
pub fn key_mut(&mut self) -> &mut Key {
&mut self.0
}
/// Mutably borrow the `Value` part of the `KvPair`.
#[inline]
pub fn value_mut(&mut self) -> &mut Value {
&mut self.1
}
/// Set the `Key` part of the `KvPair`.
#[inline]
pub fn set_key(&mut self, k: impl Into<Key>) {
self.0 = k.into();
}
/// Set the `Value` part of the `KvPair`.
#[inline]
pub fn set_value(&mut self, v: impl Into<Value>) {
self.1 = v.into();
}
}
impl<K, V> From<(K, V)> for KvPair
where
K: Into<Key>,
V: Into<Value>,
{
fn from((k, v): (K, V)) -> Self {
KvPair(k.into(), v.into())
}
}
impl From<KvPair> for (Key, Value) {
fn from(pair: KvPair) -> Self {
(pair.0, pair.1)
}
}
impl From<KvPair> for Key {
fn from(pair: KvPair) -> Self {
pair.0
}
}
impl From<kvrpcpb::KvPair> for KvPair {
fn from(pair: kvrpcpb::KvPair) -> Self {
KvPair(Key::from(pair.key), pair.value)
}
}
impl From<KvPair> for kvrpcpb::KvPair {
fn from(pair: KvPair) -> Self {
let mut result = kvrpcpb::KvPair::default();
let (key, value) = pair.into();
result.key = key.into();
result.value = value;
result
}
}
impl AsRef<Key> for KvPair {
fn as_ref(&self) -> &Key {
&self.0
}
}
impl AsRef<Value> for KvPair {
fn as_ref(&self) -> &Value {
&self.1
}
}
impl fmt::Debug for KvPair {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let KvPair(key, value) = self;
match str::from_utf8(value) {
Ok(s) => write!(f, "KvPair({}, {:?})", HexRepr(&key.0), s),
Err(_) => write!(f, "KvPair({}, {})", HexRepr(&key.0), HexRepr(value)),
}
}
}