protocol/types/
string.rs

1use crate::{hint, types, util, Parcel, Error, Settings};
2use std::io::prelude::*;
3use std;
4
5// The default implementation treats the string as a normal char array.
6impl Parcel for std::string::String
7{
8    const TYPE_NAME: &'static str = "String";
9
10    fn read_field(read: &mut dyn Read,
11                  settings: &Settings,
12                  hints: &mut hint::Hints) -> Result<Self, Error> {
13        let bytes: Vec<u8> = util::read_list(read, settings, hints)?;
14
15        Ok(std::string::String::from_utf8(bytes)?)
16    }
17
18    fn write_field(&self, write: &mut dyn Write,
19                   settings: &Settings,
20                   hints: &mut hint::Hints) -> Result<(), Error> {
21        let bytes: Vec<u8> = self.bytes().collect();
22        util::write_list(&bytes, write, settings, hints)
23    }
24}
25
26/// A string with a custom size prefix integer type.
27/// `S` - The size prefix type.
28#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
29pub struct String<S: types::Integer = u32>
30{
31    pub value: std::string::String,
32    _a: std::marker::PhantomData<S>,
33}
34
35impl<S: types::Integer> String<S>
36{
37    pub fn new(s: std::string::String) -> Self {
38        String {
39            value: s,
40            _a: std::marker::PhantomData,
41        }
42    }
43}
44
45impl<S: types::Integer> Parcel for String<S>
46{
47    const TYPE_NAME: &'static str = "protocol::String<S>";
48
49    fn read_field(read: &mut dyn Read,
50            settings: &Settings,
51            hints: &mut hint::Hints) -> Result<Self, Error> {
52        let bytes = types::Vec::<S, u8>::read_field(read, settings, hints)?;
53
54        Ok(String::new(std::string::String::from_utf8(bytes.elements)?))
55    }
56
57    fn write_field(&self, write: &mut dyn Write,
58                   settings: &Settings,
59                   hints: &mut hint::Hints) -> Result<(), Error> {
60        let array: types::Vec<S, u8> = types::Vec::new(self.value.bytes().collect());
61        array.write_field(write, settings, hints)
62    }
63}
64
65impl<S: types::Integer> std::fmt::Debug for String<S> {
66    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
67        self.value.fmt(fmt)
68    }
69}
70
71impl<S: types::Integer> std::fmt::Display for String<S> {
72    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
73        self.value.fmt(fmt)
74    }
75}
76
77impl<S> std::ops::Deref for String<S>
78    where S: types::Integer {
79    type Target = str;
80
81    fn deref(&self) -> &str { &self.value }
82}
83
84impl<S> std::ops::DerefMut for String<S>
85    where S: types::Integer {
86    fn deref_mut(&mut self) -> &mut str { &mut self.value }
87}
88
89impl<S> AsRef<str> for String<S>
90    where S: types::Integer {
91    fn as_ref(&self) -> &str { &self.value }
92}
93
94impl<S> AsMut<str> for String<S>
95    where S: types::Integer {
96    fn as_mut(&mut self) -> &mut str { &mut self.value }
97}