-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathdynamic_bounded_string.rs
More file actions
258 lines (220 loc) · 7.98 KB
/
Copy pathdynamic_bounded_string.rs
File metadata and controls
258 lines (220 loc) · 7.98 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use std::{
convert::AsMut,
fmt::{self, Display},
num::NonZeroUsize,
ops::{Deref, DerefMut},
};
use rosidl_runtime_rs::StringExceedsBoundsError;
use super::{DynamicSequenceElementMut, Proxy, ProxyMut, ProxySequence};
/// A bounded String whose upper bound is only known at runtime.
///
/// It derefs to a [`rosidl_runtime_rs::String`], which allows conversion
/// into a regular string, and more.
#[derive(Debug, Hash, PartialOrd, PartialEq, Eq, Ord)]
pub struct DynamicBoundedString<'msg> {
pub(super) inner: &'msg rosidl_runtime_rs::String,
pub(super) upper_bound: NonZeroUsize,
}
/// A bounded WString whose upper bound is only known at runtime.
///
/// It derefs to a [`rosidl_runtime_rs::WString`], which allows conversion
/// into a regular string, and more.
#[derive(Debug, Hash, PartialOrd, PartialEq, Eq, Ord)]
pub struct DynamicBoundedWString<'msg> {
pub(super) inner: &'msg rosidl_runtime_rs::WString,
pub(super) upper_bound: NonZeroUsize,
}
/// A bounded String whose upper bound is only known at runtime.
///
/// Like its immutable counterpart [`DynamicBoundedString`], it derefs to a
/// [`rosidl_runtime_rs::String`], but not mutably. This is to make sure that
/// methods which change the string's length can not be accessed, so that the
/// length never exceeds the upper bound.
/// Instead, this type provides its own mutation methods, which check the length,
/// an an [`AsMut`][1] instance.
///
/// [1]: std::convert::AsMut
#[derive(Debug, Hash, PartialOrd, PartialEq, Eq, Ord)]
pub struct DynamicBoundedStringMut<'msg> {
pub(super) inner: &'msg mut rosidl_runtime_rs::String,
pub(super) upper_bound: NonZeroUsize,
}
/// A bounded WString whose upper bound is only known at runtime.
///
/// Like its immutable counterpart [`DynamicBoundedWString`], it derefs to a
/// [`rosidl_runtime_rs::WString`], but not mutably. This is to make sure that
/// methods which change the string's length can not be accessed, so that the
/// length never exceeds the upper bound.
/// Instead, this type provides its own mutation methods, which check the length,
/// an an [`AsMut`][1] instance.
///
/// [1]: std::convert::AsMut
#[derive(Debug, Hash, PartialOrd, PartialEq, Eq, Ord)]
pub struct DynamicBoundedWStringMut<'msg> {
pub(super) inner: &'msg mut rosidl_runtime_rs::WString,
pub(super) upper_bound: NonZeroUsize,
}
// ========================= impl for DynamicBounded(W)String =========================
impl<'msg> Deref for DynamicBoundedString<'msg> {
type Target = rosidl_runtime_rs::String;
fn deref(&self) -> &Self::Target {
self.inner
}
}
impl<'msg> Display for DynamicBoundedString<'msg> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
self.inner.fmt(f)
}
}
unsafe impl<'msg> Proxy<'msg> for DynamicBoundedString<'msg> {
type Metadata = NonZeroUsize; // String upper bound
fn size_in_memory(_: NonZeroUsize) -> usize {
std::mem::size_of::<rosidl_runtime_rs::String>()
}
unsafe fn new(bytes: &'msg [u8], string_upper_bound: NonZeroUsize) -> Self {
let inner = &*(bytes.as_ptr() as *const rosidl_runtime_rs::String);
Self {
inner,
upper_bound: string_upper_bound,
}
}
}
impl<'msg> DynamicBoundedString<'msg> {
/// Returns the maximum length of this string.
pub fn upper_bound(&self) -> NonZeroUsize {
self.upper_bound
}
}
impl<'msg> DynamicBoundedWString<'msg> {
/// Returns the maximum length of this string.
pub fn upper_bound(&self) -> NonZeroUsize {
self.upper_bound
}
}
impl<'msg> Deref for DynamicBoundedWString<'msg> {
type Target = rosidl_runtime_rs::WString;
fn deref(&self) -> &Self::Target {
self.inner
}
}
impl<'msg> Display for DynamicBoundedWString<'msg> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
self.inner.fmt(f)
}
}
unsafe impl<'msg> Proxy<'msg> for DynamicBoundedWString<'msg> {
type Metadata = NonZeroUsize; // String upper bound
fn size_in_memory(_: NonZeroUsize) -> usize {
std::mem::size_of::<rosidl_runtime_rs::WString>()
}
unsafe fn new(bytes: &'msg [u8], string_upper_bound: NonZeroUsize) -> Self {
let inner = &*(bytes.as_ptr() as *const rosidl_runtime_rs::WString);
Self {
inner,
upper_bound: string_upper_bound,
}
}
}
// ========================= impl for DynamicBounded(W)StringMut =========================
impl<'msg> AsMut<[std::os::raw::c_char]> for DynamicBoundedStringMut<'msg> {
fn as_mut(&mut self) -> &mut [std::os::raw::c_char] {
self.inner.deref_mut()
}
}
impl<'msg> Deref for DynamicBoundedStringMut<'msg> {
type Target = rosidl_runtime_rs::String;
fn deref(&self) -> &Self::Target {
self.inner
}
}
impl<'msg> Display for DynamicBoundedStringMut<'msg> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
self.inner.fmt(f)
}
}
impl<'msg> DynamicSequenceElementMut<'msg> for DynamicBoundedStringMut<'msg> {
type InnerSequence = ProxySequence<'msg, Self>;
}
unsafe impl<'msg> ProxyMut<'msg> for DynamicBoundedStringMut<'msg> {
type Metadata = NonZeroUsize; // String upper bound
fn size_in_memory(_: NonZeroUsize) -> usize {
std::mem::size_of::<rosidl_runtime_rs::String>()
}
unsafe fn new(bytes: &'msg mut [u8], string_upper_bound: NonZeroUsize) -> Self {
let inner = &mut *(bytes.as_mut_ptr() as *mut rosidl_runtime_rs::String);
Self {
inner,
upper_bound: string_upper_bound,
}
}
}
impl<'msg> DynamicBoundedStringMut<'msg> {
/// Returns the maximum length of this string.
pub fn upper_bound(&self) -> NonZeroUsize {
self.upper_bound
}
/// If the given string is not too long, assign it to self.
pub fn try_assign(&mut self, s: &str) -> Result<(), StringExceedsBoundsError> {
let length = s.chars().count();
if length <= self.upper_bound.into() {
*self.inner = rosidl_runtime_rs::String::from(s);
Ok(())
} else {
Err(StringExceedsBoundsError {
len: length,
upper_bound: self.upper_bound.into(),
})
}
}
}
impl<'msg> AsMut<[std::os::raw::c_ushort]> for DynamicBoundedWStringMut<'msg> {
fn as_mut(&mut self) -> &mut [std::os::raw::c_ushort] {
self.inner.deref_mut()
}
}
impl<'msg> Deref for DynamicBoundedWStringMut<'msg> {
type Target = rosidl_runtime_rs::WString;
fn deref(&self) -> &Self::Target {
self.inner
}
}
impl<'msg> Display for DynamicBoundedWStringMut<'msg> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
self.inner.fmt(f)
}
}
impl<'msg> DynamicSequenceElementMut<'msg> for DynamicBoundedWStringMut<'msg> {
type InnerSequence = ProxySequence<'msg, Self>;
}
unsafe impl<'msg> ProxyMut<'msg> for DynamicBoundedWStringMut<'msg> {
type Metadata = NonZeroUsize; // String upper bound
fn size_in_memory(_: NonZeroUsize) -> usize {
std::mem::size_of::<rosidl_runtime_rs::WString>()
}
unsafe fn new(bytes: &'msg mut [u8], string_upper_bound: NonZeroUsize) -> Self {
let inner = &mut *(bytes.as_mut_ptr() as *mut rosidl_runtime_rs::WString);
Self {
inner,
upper_bound: string_upper_bound,
}
}
}
impl<'msg> DynamicBoundedWStringMut<'msg> {
/// Returns the maximum length of this string.
pub fn upper_bound(&self) -> NonZeroUsize {
self.upper_bound
}
/// If the given string is not too long, assign it to self.
pub fn try_assign(&mut self, s: &str) -> Result<(), StringExceedsBoundsError> {
let length = s.chars().count();
if length <= self.upper_bound.into() {
*self.inner = rosidl_runtime_rs::WString::from(s);
Ok(())
} else {
Err(StringExceedsBoundsError {
len: length,
upper_bound: self.upper_bound.into(),
})
}
}
}