1 /* 2 * Copyright (C) 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 use tipc::{Handle, Serializer}; 18 19 #[derive(Debug)] 20 pub struct TestSerializationError; 21 22 /// A simple serializer that copies input data and allocates. 23 /// Not suitable for production. 24 #[derive(Default)] 25 pub struct TestSerializer { 26 pub buffers: Vec<u8>, 27 pub handles: Vec<Option<Handle>>, 28 } 29 30 impl<'a> Serializer<'a> for TestSerializer { 31 type Ok = (); 32 type Error = TestSerializationError; 33 serialize_bytes(&mut self, bytes: &'a [u8]) -> Result<(), TestSerializationError>34 fn serialize_bytes(&mut self, bytes: &'a [u8]) -> Result<(), TestSerializationError> { 35 self.buffers.extend_from_slice(bytes); 36 Ok(()) 37 } 38 serialize_handle(&mut self, handle: &'a Handle) -> Result<(), TestSerializationError>39 fn serialize_handle(&mut self, handle: &'a Handle) -> Result<(), TestSerializationError> { 40 let h = handle.try_clone().or(Err(TestSerializationError))?; 41 self.handles.push(Some(h)); 42 Ok(()) 43 } 44 } 45