rclrs 0.7.0

A ROS 2 client library for developing robotics applications in Rust
Documentation
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use std::fmt::{self, Debug};
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};

use rosidl_runtime_rs::{Sequence, SequenceAlloc, SequenceExceedsBoundsError};

use super::check;

// We cannot always use &Sequence<T> and &mut Sequence<T> as types for accessing sequence fields.
// This is for two reasons:
// 1.  The type T might be a sub-message, or a bounded string/wstring. These types have some
//     associated metadata, namely the message structure and the string length bound, which needs
//     to be available when accessing/modifying the type T. Therefore, fields of this type are
//     accessed via their "proxy" wrappers, such as `DynamicMessageView`, which include the
//     metadata. The memory layout of the proxy type doesn't match that of T, so we cannot cast
//     Sequence<T> to Sequence<Proxy>.
// 2a. The sequence might be bounded. The API of Sequence<T> allows unchecked length changes.
// 2b. For a bounded sequence, it would be nice if the sequence type had a getter for the upper
//     bound, but Sequence<T> doesn't know the upper bound.
//
// So, following these criteria, these are the possible sequence types for each combination of
// bounded/unbounded, mutable/immutable, and native/proxy element type:
//
// seq kind  | mutability | element type | possible sequence types
// ----------+------------+--------------+--------------
// unbounded | immutable  | native       | &Sequence<T> or &[T]*
// unbounded | immutable  | proxy        | Box<T>*
// unbounded | mutable    | native       | &mut Sequence<T>*
// unbounded | mutable    | proxy        | custom type that can store proxy objects
// bounded   | immutable  | native       | (usize, Box<T>) or (usize, &Sequence<T>)*
// bounded   | immutable  | proxy        | (usize, Box<T>)*
// bounded   | mutable    | native       | custom type that enforces upper bound
// bounded   | mutable    | proxy        | custom type that enforces upper bound and can store proxy objects
//
// * or an equivalent custom type
//
// This module chooses to expose the following types for this purpose:
//
// seq kind  | mutability | element type | sequence type
// ----------+------------+--------------+--------------
// unbounded | immutable  | native       | &Sequence<T>
// unbounded | immutable  | proxy        | DynamicSequence<T> (newtype of Box<T>)
// unbounded | mutable    | native       | &mut Sequence<T>
// unbounded | mutable    | proxy        | DynamicSequenceMut<T>
// bounded   | immutable  | native       | DynamicBoundedSequence<T> (similar to Cow<[T]>)
// bounded   | immutable  | proxy        | DynamicBoundedSequence<T> (similar to Cow<[T]>)
// bounded   | mutable    | native       | DynamicBoundedSequenceMut<T> (based on DynamicSequenceMut<T>)
// bounded   | mutable    | proxy        | DynamicBoundedSequenceMut<T> (based on DynamicSequenceMut<T>)
//
// That means that DynamicBoundedSequence and DynamicBoundedSequenceMut must be able to hold
// both native and proxy objects.

// ========================= Abstracting over different proxy types =========================

// These traits abstract over types that are "proxy objects" for the actual data stored
// in a message. See also the explanation above for context.
//
// There are three types implementing these traits (dynamic versions of String, WString and messages).
// Without these traits, you'd have to e.g. write three versions of the ProxySequence struct, and
// of its implementation of the InnerSequence trait, and much more.

/// An immutable proxy object.
///
/// This trait is unsafe because memory errors will occur if size_in_memory() is
/// implemented incorrectly.
#[doc(hidden)]
pub unsafe trait Proxy<'msg> {
    // In the case of strings, this is the string length upper bound,
    // and in the case of messages, it is its structure.
    type Metadata: 'msg + Copy;
    // How many bytes does each element take up in the underlying sequence?
    fn size_in_memory(metadata: Self::Metadata) -> usize;
    // This function is unsafe because the bytes must correspond to the proxied object.
    unsafe fn new(bytes: &'msg [u8], metadata: Self::Metadata) -> Self;
}

/// An mutable proxy object.
///
/// This trait is unsafe because memory errors will occur if size_in_memory() is
/// implemented incorrectly.
#[doc(hidden)]
pub unsafe trait ProxyMut<'msg> {
    // In the case of strings, this is the string length upper bound,
    // and in the case of messages, it is its structure.
    type Metadata: 'msg + Copy;
    // How many bytes does each element take up in the underlying sequence?
    fn size_in_memory(metadata: Self::Metadata) -> usize;
    // This function is unsafe because the bytes must correspond to the proxied object.
    unsafe fn new(bytes: &'msg mut [u8], metadata: Self::Metadata) -> Self;
}

// ========================= Abstracting over proxy vs direct sequences =========================

// This trait abstracts over &mut Sequence<T> vs ProxySequence<T>.
#[doc(hidden)]
pub trait InnerSequence<T: PartialEq>: PartialEq {
    fn as_slice(&self) -> &[T];
    fn as_mut_slice(&mut self) -> &mut [T];
    // "Unchecked" means that it doesn't know about the upper bound of the sequence.
    fn resize_unchecked(&mut self, resize_function: ResizeFunction, len: usize);
}

#[doc(hidden)]
pub struct ProxySequence<'msg, T: ProxyMut<'msg>> {
    // The underlying storage
    sequence: &'msg mut TypeErasedSequence,
    // The user-facing objects
    proxies: Vec<T>,
    // To recreate the proxies
    metadata: T::Metadata,
}

impl<'msg, T> InnerSequence<T> for ProxySequence<'msg, T>
where
    T: PartialEq + ProxyMut<'msg>,
{
    fn as_slice(&self) -> &[T] {
        self.proxies.as_slice()
    }

    fn as_mut_slice(&mut self) -> &mut [T] {
        self.proxies.as_mut_slice()
    }

    /// This will fini all messages in the sequence and re-initialize it from scratch.
    fn resize_unchecked(&mut self, resize_function: ResizeFunction, len: usize) {
        let is_ok =
            unsafe { resize_function(self.sequence as *mut _ as *mut std::os::raw::c_void, len) };
        assert!(is_ok);

        // Recalculate the message proxies
        self.proxies = unsafe { self.sequence.proxy_elems_mut(self.metadata) };
    }
}

impl<'msg, T> InnerSequence<T> for &'msg mut Sequence<T>
where
    T: PartialEq + SequenceAlloc,
{
    fn as_slice(&self) -> &[T] {
        // self.as_slice() would call this trait method itself
        Sequence::as_slice(self)
    }

    fn as_mut_slice(&mut self) -> &mut [T] {
        // self.as_mut_slice() would call this trait method itself
        Sequence::as_mut_slice(self)
    }

    /// This will fini all messages in the sequence and re-initialize it from scratch.
    fn resize_unchecked(&mut self, resize_function: ResizeFunction, len: usize) {
        let is_ok = unsafe { resize_function(self as *mut _ as *mut std::os::raw::c_void, len) };
        assert!(is_ok);
    }
}

impl<'msg, T> PartialEq for ProxySequence<'msg, T>
where
    T: PartialEq + ProxyMut<'msg>,
{
    fn eq(&self, other: &Self) -> bool {
        self.proxies.eq(&other.proxies)
    }
}

// This links the element type T to the inner sequence type: &mut Sequence<T> or ProxySequence<T>.
#[doc(hidden)]
pub trait DynamicSequenceElementMut<'msg>: Debug + PartialEq + Sized {
    type InnerSequence: InnerSequence<Self>;
}

// If the element type is an rosidl_runtime_rs type, the sequence type is &mut Sequence<T>
impl<'msg, T> DynamicSequenceElementMut<'msg> for T
where
    T: Debug + PartialEq + SequenceAlloc + 'static,
{
    type InnerSequence = &'msg mut Sequence<T>;
}

// ========================= The TypeErasedSequence helper =========================

/// A Sequence whose type is not statically known.
///
/// This is an internal helper struct whose layout, like rosidl_runtime_rs::Sequence,
/// matches that of the type generated by rosidl_generator_c.
#[repr(C)]
pub(crate) struct TypeErasedSequence {
    pub(super) data: *mut std::os::raw::c_void,
    pub(super) size: usize,
    pub(super) capacity: usize,
}

impl TypeErasedSequence {
    pub(super) unsafe fn proxy_elems<'msg, T>(&self, metadata: T::Metadata) -> Vec<T>
    where
        T: Proxy<'msg>,
    {
        let element_size = T::size_in_memory(metadata);
        if self.data.is_null() {
            return Vec::new();
        };
        let sequence_data =
            std::slice::from_raw_parts(self.data as *const u8, self.size * element_size);
        check::<T>(sequence_data);
        sequence_data
            .chunks(element_size)
            .map(|bytes| T::new(bytes, metadata))
            .collect()
    }

    pub(super) unsafe fn proxy_elems_mut<'msg, T>(&self, metadata: T::Metadata) -> Vec<T>
    where
        T: ProxyMut<'msg>,
    {
        let element_size = T::size_in_memory(metadata);
        if self.data.is_null() {
            return Vec::new();
        };
        let sequence_data =
            std::slice::from_raw_parts_mut(self.data as *mut u8, self.size * element_size);
        check::<T>(sequence_data);
        sequence_data
            .chunks_mut(element_size)
            .map(|bytes| T::new(bytes, metadata))
            .collect()
    }
}

// ==========================================================================
// ======================== Immutable sequence types ========================
// ==========================================================================

/// An unbounded sequence.
///
/// This type dereferences to `&[T]`.
#[derive(PartialEq, Eq)]
pub struct DynamicSequence<'msg, T>
where
    T: Proxy<'msg>,
{
    elements: Box<[T]>,
    // Not sure if this is strictly needed, but it's nice to be consistent
    phantom: PhantomData<&'msg u8>,
}

// BorrowedOrOwnedSlice – a specialized version of Cow.
// Cow cannot be used because it requires T to be Clone.
#[derive(PartialEq, Eq)]
enum BooSlice<'msg, T> {
    Borrowed(&'msg [T]),
    Owned(Box<[T]>),
}

/// A bounded sequence whose upper bound is only known at runtime.
#[derive(PartialEq, Eq)]
pub struct DynamicBoundedSequence<'msg, T> {
    boo: BooSlice<'msg, T>,
    upper_bound: usize,
}

// ------------------------- impl for DynamicSequence -------------------------

impl<'msg, T> Debug for DynamicSequence<'msg, T>
where
    T: Debug + Proxy<'msg>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        self.elements.iter().fmt(f)
    }
}

impl<'msg, T> Deref for DynamicSequence<'msg, T>
where
    T: Proxy<'msg>,
{
    type Target = [T];
    fn deref(&self) -> &Self::Target {
        &*self.elements
    }
}

impl<'msg, T> DynamicSequence<'msg, T>
where
    T: Proxy<'msg>,
{
    pub(super) unsafe fn new_proxy(bytes: &'msg [u8], metadata: T::Metadata) -> Self {
        let sequence = &*(bytes.as_ptr() as *const TypeErasedSequence);
        let elements = sequence.proxy_elems(metadata).into_boxed_slice();
        Self {
            elements,
            phantom: PhantomData,
        }
    }

    /// See [`Sequence::as_slice()`][1].
    ///
    /// [1]: rosidl_runtime_rs::Sequence::as_slice
    pub fn as_slice(&self) -> &[T] {
        &*self.elements
    }
}

// ------------------------- impl for DynamicBoundedSequence -------------------------

impl<'msg, T> BooSlice<'msg, T> {
    fn as_slice(&self) -> &[T] {
        match self {
            BooSlice::Borrowed(slice) => slice,
            BooSlice::Owned(boxed_slice) => &**boxed_slice,
        }
    }
}

impl<'msg, T> Debug for DynamicBoundedSequence<'msg, T>
where
    T: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        self.boo.as_slice().fmt(f)
    }
}

impl<'msg, T> Deref for DynamicBoundedSequence<'msg, T> {
    type Target = [T];
    fn deref(&self) -> &Self::Target {
        self.boo.as_slice()
    }
}

impl<'msg, T> DynamicBoundedSequence<'msg, T>
where
    T: SequenceAlloc,
{
    pub(super) unsafe fn new_primitive(bytes: &'msg [u8], upper_bound: usize) -> Self {
        let sequence = &*(bytes.as_ptr() as *const Sequence<T>);
        let slice = sequence.as_slice();
        Self {
            boo: BooSlice::Borrowed(slice),
            upper_bound,
        }
    }
}

impl<'msg, T> DynamicBoundedSequence<'msg, T>
where
    T: Proxy<'msg>,
{
    pub(super) unsafe fn new_proxy(
        bytes: &'msg [u8],
        upper_bound: usize,
        metadata: T::Metadata,
    ) -> Self {
        let sequence = &*(bytes.as_ptr() as *const TypeErasedSequence);
        Self {
            boo: BooSlice::Owned(sequence.proxy_elems(metadata).into_boxed_slice()),
            upper_bound,
        }
    }
}

impl<'msg, T: SequenceAlloc> DynamicBoundedSequence<'msg, T> {
    /// See [`Sequence::as_slice()`][1].
    ///
    /// [1]: rosidl_runtime_rs::Sequence::as_slice
    pub fn as_slice(&self) -> &[T] {
        self.boo.as_slice()
    }

    /// Returns the maximum length of this sequence.
    pub fn upper_bound(&self) -> usize {
        self.upper_bound
    }
}

// ==========================================================================
// ========================= Mutable sequence types =========================
// ==========================================================================

// The resize function from the type support library does not preserve the elements.
// It just calls fini + init.
pub(super) type ResizeFunction =
    unsafe extern "C" fn(arg1: *mut std::os::raw::c_void, size: usize) -> bool;

/// An unbounded sequence.
///
/// This type dereferences to `&[T]` and `&mut [T]`.
#[derive(PartialEq)]
pub struct DynamicSequenceMut<'msg, T: DynamicSequenceElementMut<'msg>> {
    // This is either &mut Sequence<T> or ProxySequence<T>
    sequence: T::InnerSequence,
    resize_function: ResizeFunction,
}

/// A bounded sequence whose upper bound is only known at runtime.
///
/// This is conceptually the same as a [`BoundedSequence<T>`][1].
///
/// This type dereferences to `&[T]` and `&mut [T]`.
///
/// [1]: rosidl_runtime_rs::BoundedSequence
#[derive(PartialEq)]
pub struct DynamicBoundedSequenceMut<'msg, T: DynamicSequenceElementMut<'msg>> {
    inner: DynamicSequenceMut<'msg, T>,
    upper_bound: usize,
}

// ------------------------- impl for DynamicSequenceMut -------------------------

impl<'msg, T> Debug for DynamicSequenceMut<'msg, T>
where
    T: DynamicSequenceElementMut<'msg>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        self.sequence.as_slice().fmt(f)
    }
}

impl<'msg, T> Deref for DynamicSequenceMut<'msg, T>
where
    T: DynamicSequenceElementMut<'msg>,
{
    type Target = [T];
    fn deref(&self) -> &Self::Target {
        self.sequence.as_slice()
    }
}

impl<'msg, T> DerefMut for DynamicSequenceMut<'msg, T>
where
    T: DynamicSequenceElementMut<'msg>,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.sequence.as_mut_slice()
    }
}

impl<'msg, T> DynamicSequenceMut<'msg, T>
where
    T: SequenceAlloc
        + DynamicSequenceElementMut<'msg, InnerSequence = &'msg mut Sequence<T>>
        + 'static,
{
    pub(super) unsafe fn new_primitive(
        bytes: &'msg mut [u8],
        resize_function: ResizeFunction,
    ) -> Self {
        let sequence = &mut *(bytes.as_mut_ptr() as *mut Sequence<T>);
        Self {
            sequence,
            resize_function,
        }
    }
}

impl<'msg, T> DynamicSequenceMut<'msg, T>
where
    T: ProxyMut<'msg> + DynamicSequenceElementMut<'msg, InnerSequence = ProxySequence<'msg, T>>,
{
    pub(super) unsafe fn new_proxy(
        bytes: &'msg mut [u8],
        metadata: T::Metadata,
        resize_function: ResizeFunction,
    ) -> Self {
        // SAFETY: TypeErasedSequence has the same layout as any
        // rosidl-generated C sequence type, and the lifetime is correct too.
        let sequence = &mut *(bytes.as_mut_ptr() as *mut TypeErasedSequence);
        let proxies = sequence.proxy_elems_mut(metadata);
        let sequence = ProxySequence {
            sequence,
            proxies,
            metadata,
        };
        Self {
            sequence,
            resize_function,
        }
    }
}

impl<'msg, T> DynamicSequenceMut<'msg, T>
where
    T: DynamicSequenceElementMut<'msg>,
{
    /// See [`Sequence::as_slice()`][1].
    ///
    /// [1]: rosidl_runtime_rs::Sequence::as_slice
    pub fn as_slice(&self) -> &[T] {
        self.sequence.as_slice()
    }

    /// See [`Sequence::as_mut_slice()`][1].
    ///
    /// [1]: rosidl_runtime_rs::Sequence::as_mut_slice
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        self.sequence.as_mut_slice()
    }

    /// Resets this sequence to an empty sequence.
    pub fn clear(&mut self) {
        self.reset(0);
    }

    /// Resets this sequence to a new sequence of `len` elements with default values.
    pub fn reset(&mut self, len: usize) {
        self.sequence.resize_unchecked(self.resize_function, len)
    }
}

// ------------------------- impl for DynamicBoundedSequenceMut -------------------------

impl<'msg, T> Debug for DynamicBoundedSequenceMut<'msg, T>
where
    T: DynamicSequenceElementMut<'msg>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        self.inner.fmt(f)
    }
}

impl<'msg, T: DynamicSequenceElementMut<'msg>> Deref for DynamicBoundedSequenceMut<'msg, T> {
    type Target = [T];
    fn deref(&self) -> &Self::Target {
        self.inner.deref()
    }
}

impl<'msg, T: DynamicSequenceElementMut<'msg>> DerefMut for DynamicBoundedSequenceMut<'msg, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.inner.deref_mut()
    }
}

impl<'msg, T> DynamicBoundedSequenceMut<'msg, T>
where
    T: SequenceAlloc
        + DynamicSequenceElementMut<'msg, InnerSequence = &'msg mut Sequence<T>>
        + 'static,
{
    pub(super) unsafe fn new_primitive(
        bytes: &'msg mut [u8],
        upper_bound: usize,
        resize_function: ResizeFunction,
    ) -> Self {
        let inner = DynamicSequenceMut::new_primitive(bytes, resize_function);
        Self { inner, upper_bound }
    }
}

impl<'msg, T> DynamicBoundedSequenceMut<'msg, T>
where
    T: ProxyMut<'msg> + DynamicSequenceElementMut<'msg, InnerSequence = ProxySequence<'msg, T>>,
{
    pub(super) unsafe fn new_proxy(
        bytes: &'msg mut [u8],
        metadata: T::Metadata,
        upper_bound: usize,
        resize_function: ResizeFunction,
    ) -> Self {
        let inner = DynamicSequenceMut::new_proxy(bytes, metadata, resize_function);
        Self { inner, upper_bound }
    }
}

impl<'msg, T: DynamicSequenceElementMut<'msg>> DynamicBoundedSequenceMut<'msg, T> {
    /// See [`Sequence::as_slice()`][1].
    ///
    /// [1]: rosidl_runtime_rs::Sequence::as_slice
    pub fn as_slice(&self) -> &[T] {
        self.inner.as_slice()
    }

    /// See [`Sequence::as_mut_slice()`][1].
    ///
    /// [1]: rosidl_runtime_rs::Sequence::as_mut_slice
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        self.inner.as_mut_slice()
    }

    /// Returns the maximum length of this sequence.
    pub fn upper_bound(&self) -> usize {
        self.upper_bound
    }

    /// Resets this sequence to an empty sequence.
    pub fn clear(&mut self) {
        self.inner.clear();
    }

    /// Tries to reset this sequence to a new sequence of `len` elements with default values.
    ///
    /// This is only successful if `len` is less than or equal to the [upper bound][1], otherwise
    /// the sequence is unmodified.
    ///
    /// [1]: Self::upper_bound
    pub fn try_reset(&mut self, len: usize) -> Result<(), SequenceExceedsBoundsError> {
        if len > self.upper_bound {
            Err(SequenceExceedsBoundsError {
                len,
                upper_bound: self.upper_bound,
            })
        } else {
            self.inner.reset(len);
            Ok(())
        }
    }
}