Skip to main content

tikv_client/kv/
kvpair.rs

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