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 use bincode::serialize; 12 use serde::Serialize; 13 14 use crate::error::DataError; 15 16 pub trait EncodableKey { to_bytes(&self) -> Result<Vec<u8>, DataError>17 fn to_bytes(&self) -> Result<Vec<u8>, DataError>; 18 } 19 20 impl<T> EncodableKey for T 21 where 22 T: Serialize, 23 { to_bytes(&self) -> Result<Vec<u8>, DataError>24 fn to_bytes(&self) -> Result<Vec<u8>, DataError> { 25 serialize(self).map_err(|e| e.into()) 26 } 27 } 28