1 // Copyright 2018-2019 Mozilla 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not use 4 // this file except in compliance with the License. You may obtain a copy of the 5 // License at http://www.apache.org/licenses/LICENSE-2.0 6 // Unless required by applicable law or agreed to in writing, software distributed 7 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 8 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 9 // specific language governing permissions and limitations under the License. 10 11 mod encodables; 12 mod primitives; 13 14 use std::marker::PhantomData; 15 16 use crate::error::DataError; 17 18 pub use encodables::*; 19 pub use primitives::*; 20 21 pub(crate) struct Key<K> { 22 bytes: Vec<u8>, 23 phantom: PhantomData<K>, 24 } 25 26 impl<K> AsRef<[u8]> for Key<K> 27 where 28 K: EncodableKey, 29 { as_ref(&self) -> &[u8]30 fn as_ref(&self) -> &[u8] { 31 self.bytes.as_ref() 32 } 33 } 34 35 impl<K> Key<K> 36 where 37 K: EncodableKey, 38 { 39 #[allow(clippy::new_ret_no_self)] new(k: &K) -> Result<Key<K>, DataError>40 pub fn new(k: &K) -> Result<Key<K>, DataError> { 41 Ok(Key { 42 bytes: k.to_bytes()?, 43 phantom: PhantomData, 44 }) 45 } 46 } 47