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