argdata/values/
binary.rs

1use fd;
2use std::io;
3use Argdata;
4use ReadError;
5use Value;
6
7#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
8pub struct Binary<'d> {
9	value: &'d [u8],
10}
11
12/// Create an argdata value representing a binary blob.
13pub fn binary<'d>(value: &'d [u8]) -> Binary<'d> {
14	Binary { value }
15}
16
17impl<'d> Binary<'d> {
18	pub fn bytes(&self) -> &'d [u8] {
19		self.value
20	}
21}
22
23impl<'d> Argdata<'d> for Binary<'d> {
24	fn read<'a>(&'a self) -> Result<Value<'a, 'd>, ReadError>
25	where
26		'd: 'a,
27	{
28		Ok(Value::Binary(self.bytes()))
29	}
30
31	fn serialized_length(&self) -> usize {
32		self.bytes().len() + 1
33	}
34
35	fn serialize(&self, writer: &mut io::Write, _: Option<&mut fd::FdMapping>) -> io::Result<()> {
36		writer.write_all(&[1])?;
37		writer.write_all(self.bytes())?;
38		Ok(())
39	}
40}