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;
#[derive(Default, Clone, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
pub struct KvPair(pub Key, pub Value);
impl KvPair {
#[inline]
pub fn new(key: impl Into<Key>, value: impl Into<Value>) -> Self {
KvPair(key.into(), value.into())
}
#[inline]
pub fn key(&self) -> &Key {
&self.0
}
#[inline]
pub fn value(&self) -> &Value {
&self.1
}
#[inline]
pub fn into_key(self) -> Key {
self.0
}
#[inline]
pub fn into_value(self) -> Value {
self.1
}
#[inline]
pub fn key_mut(&mut self) -> &mut Key {
&mut self.0
}
#[inline]
pub fn value_mut(&mut self) -> &mut Value {
&mut self.1
}
#[inline]
pub fn set_key(&mut self, k: impl Into<Key>) {
self.0 = k.into();
}
#[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)),
}
}
}