Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions rclrs/src/dynamic_message/field_access/dynamic_sequence.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::fmt::{self, Debug};
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::{
fmt::{self, Debug},
marker::PhantomData,
ops::{Deref, DerefMut},
};

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

Expand Down Expand Up @@ -298,6 +300,21 @@ where
pub fn as_slice(&self) -> &[T] {
&*self.elements
}

/// Returns the number of elements in this sequence.
pub fn len(&self) -> usize {
self.elements.len()
}

/// Returns true if this sequence contains no elements.
pub fn is_empty(&self) -> bool {
self.elements.is_empty()
}

/// Iterate over the elements of this sequence.
pub fn iter(&self) -> std::slice::Iter<'_, T> {
self.elements.iter()
}
}

// ------------------------- impl for DynamicBoundedSequence -------------------------
Expand Down Expand Up @@ -370,6 +387,21 @@ impl<'msg, T: SequenceAlloc> DynamicBoundedSequence<'msg, T> {
pub fn upper_bound(&self) -> usize {
self.upper_bound
}

/// Returns the number of elements in this sequence.
pub fn len(&self) -> usize {
self.boo.as_slice().len()
}

/// Returns true if this sequence contains no elements.
pub fn is_empty(&self) -> bool {
self.boo.as_slice().is_empty()
}

/// Iterate over the elements of this sequence.
pub fn iter(&self) -> std::slice::Iter<'_, T> {
self.boo.as_slice().iter()
}
}

// ==========================================================================
Expand Down Expand Up @@ -504,6 +536,16 @@ where
pub fn reset(&mut self, len: usize) {
self.sequence.resize_unchecked(self.resize_function, len)
}

/// Returns the number of elements in this sequence.
pub fn len(&self) -> usize {
self.sequence.as_slice().len()
}

/// Returns true if this sequence contains no elements.
pub fn is_empty(&self) -> bool {
self.sequence.as_slice().is_empty()
}
}

// ------------------------- impl for DynamicBoundedSequenceMut -------------------------
Expand Down Expand Up @@ -586,6 +628,16 @@ impl<'msg, T: DynamicSequenceElementMut<'msg>> DynamicBoundedSequenceMut<'msg, T
self.inner.clear();
}

/// Returns the number of elements in this sequence.
pub fn len(&self) -> usize {
self.inner.len()
}

/// Returns true if this sequence contains no elements.
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}

/// 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
Expand Down
Loading