async_memcached/
value_serializer.rs

1use std::str;
2
3mod private {
4    pub trait AsMemcachedValue {
5        fn as_bytes(&self) -> std::borrow::Cow<'_, [u8]>;
6    }
7}
8
9/// A trait for serializing multiple types of values in to appropriate memcached input values for the set and add commands.
10pub trait AsMemcachedValue: private::AsMemcachedValue {}
11
12impl<T: private::AsMemcachedValue> AsMemcachedValue for T {}
13
14impl private::AsMemcachedValue for &[u8] {
15    fn as_bytes(&self) -> std::borrow::Cow<'_, [u8]> {
16        std::borrow::Cow::Borrowed(self)
17    }
18}
19
20impl private::AsMemcachedValue for &str {
21    fn as_bytes(&self) -> std::borrow::Cow<'_, [u8]> {
22        std::borrow::Cow::Borrowed(str::as_bytes(self))
23    }
24}
25
26impl private::AsMemcachedValue for &String {
27    fn as_bytes(&self) -> std::borrow::Cow<'_, [u8]> {
28        std::borrow::Cow::Borrowed(str::as_bytes(self))
29    }
30}
31
32macro_rules! impl_to_memcached_value_for_uint {
33    ($ty:ident) => {
34        impl private::AsMemcachedValue for $ty {
35            fn as_bytes(&self) -> std::borrow::Cow<'_, [u8]> {
36                std::borrow::Cow::Owned(self.to_string().into_bytes())
37            }
38        }
39    };
40}
41
42impl_to_memcached_value_for_uint!(u8);
43impl_to_memcached_value_for_uint!(u16);
44impl_to_memcached_value_for_uint!(u32);
45impl_to_memcached_value_for_uint!(u64);
46impl_to_memcached_value_for_uint!(usize);