Struct ros2_client::WString

source ·
pub struct WString { /* private fields */ }
Expand description

UTF-16 strings, as required by the ROS type system.

We just wrap a pre-existing library to get proper Serialize and Deserialize.

Implementations§

source§

impl WString

source

pub fn new() -> Self

Methods from Deref<Target = Utf16String>§

source

pub fn as_utfstr(&self) -> &Utf16Str

Converts a string into a string slice.

source

pub fn as_ustr(&self) -> &U16Str

Converts this string into a wide string of undefined encoding.

source

pub fn capacity(&self) -> usize

Returns this string’s capacity, in number of elements.

source

pub fn as_slice(&self) -> &[u16]

Returns a slice of this string’s contents.

source

pub fn len(&self) -> usize

Returns the length of this string in number of elements, not chars or graphemes.

In other words, it might not be what a human considers the length of the string.

source

pub fn is_empty(&self) -> bool

Returns true if this string has a length of zero, and false otherwise.

Methods from Deref<Target = Utf16Str>§

source

pub unsafe fn get_unchecked<I>(&self, index: I) -> &Utf16Str
where I: SliceIndex<[u16], Output = [u16]>,

Returns an unchecked subslice of this string slice.

This is the unchecked alternative to indexing the string slice.

§Safety

Callers of this function are responsible that these preconditions are satisfied:

  • The starting index must not exceed the ending index;
  • Indexes must be within bounds of the original slice;
  • Indexes must lie on UTF-16 sequence boundaries.

Failing that, the returned string slice may reference invalid memory or violate the invariants communicated by the type.

§Examples
let v = utf16str!("⚧️🏳️‍⚧️➡️s");
unsafe {
    assert_eq!(utf16str!("⚧️"), v.get_unchecked(..2));
    assert_eq!(utf16str!("🏳️‍⚧️"), v.get_unchecked(2..8));
    assert_eq!(utf16str!("➡️"), v.get_unchecked(8..10));
    assert_eq!(utf16str!("s"), v.get_unchecked(10..));
}
source

pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut Utf16Str
where I: SliceIndex<[u16], Output = [u16]>,

Returns a mutable, unchecked subslice of this string slice

This is the unchecked alternative to indexing the string slice.

§Safety

Callers of this function are responsible that these preconditions are satisfied:

  • The starting index must not exceed the ending index;
  • Indexes must be within bounds of the original slice;
  • Indexes must lie on UTF-16 sequence boundaries.

Failing that, the returned string slice may reference invalid memory or violate the invariants communicated by the type.

§Examples
let mut v = utf16str!("⚧️🏳️‍⚧️➡️s").to_owned();
unsafe {
    assert_eq!(utf16str!("⚧️"), v.get_unchecked_mut(..2));
    assert_eq!(utf16str!("🏳️‍⚧️"), v.get_unchecked_mut(2..8));
    assert_eq!(utf16str!("➡️"), v.get_unchecked_mut(8..10));
    assert_eq!(utf16str!("s"), v.get_unchecked_mut(10..));
}
source

pub fn len(&self) -> usize

Returns the length of self.

This length is in u16 values, not chars or graphemes. In other words, it may not be what human considers the length of the string.

§Examples
assert_eq!(utf16str!("foo").len(), 3);

let complex = utf16str!("⚧️🏳️‍⚧️➡️s");
assert_eq!(complex.len(), 11);
assert_eq!(complex.chars().count(), 10);
source

pub fn is_empty(&self) -> bool

Returns true if the string has a length of zero.

source

pub fn as_slice(&self) -> &[u16]

Converts a string to a slice of its underlying elements.

To convert the slice back into a string slice, use the from_slice function.

source

pub unsafe fn as_mut_slice(&mut self) -> &mut [u16]

Converts a mutable string to a mutable slice of its underlying elements.

§Safety

This function is unsafe because you can violate the invariants of this type when mutating the slice. The caller must ensure that the contents of the slice is valid UTF before the borrow ends and the underlying string is used.

Use of this string type whose contents have been mutated to invalid UTF is undefined behavior.

source

pub fn as_ptr(&self) -> *const u16

Converts a string slice to a raw pointer.

This pointer will be pointing to the first element of the string slice.

The caller must ensure that the returned pointer is never written to. If you need to mutate the contents of the string slice, use as_mut_ptr.

source

pub fn as_mut_ptr(&mut self) -> *mut u16

Converts a mutable string slice to a mutable pointer.

This pointer will be pointing to the first element of the string slice.

source

pub fn as_ustr(&self) -> &U16Str

Returns this string as a wide string slice of undefined encoding.

source

pub fn trim(&self) -> &Utf16Str

Returns a string slice with leading and trailing whitespace removed.

‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space.

source

pub fn trim_start(&self) -> &Utf16Str

Returns a string slice with leading whitespace removed.

‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space.

§Text directionality

A string is a sequence of elements. start in this context means the first position of that sequence; for a left-to-right language like English or Russian, this will be left side, and for right-to-left languages like Arabic or Hebrew, this will be the right side.

source

pub fn trim_end(&self) -> &Utf16Str

Returns a string slice with trailing whitespace removed.

‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space.

§Text directionality

A string is a sequence of elements. end in this context means the last position of that sequence; for a left-to-right language like English or Russian, this will be right side, and for right-to-left languages like Arabic or Hebrew, this will be the left side.

source

pub fn repeat(&self, n: usize) -> Utf16String

Creates a new owned string by repeating this string n times.

§Panics

This function will panic if the capacity would overflow.

source

pub fn to_string(&self) -> String

Converts to a standard UTF-8 String.

Because this string is always valid UTF-16, the conversion is lossless and non-fallible.

source

pub fn is_char_boundary(&self, index: usize) -> bool

Checks that index-th value is the value in a UTF-16 code point sequence or the end of the string.

Returns true if the value at index is not a UTF-16 surrogate value, or if the value at index is the first value of a surrogate pair (the “high” surrogate). Returns false if the value at index is the second value of a surrogate pair (a.k.a the “low” surrogate).

The start and end of the string (when index == self.len()) are considered to be boundaries.

Returns false if index is greater than self.len()`.

§Examples
let s = utf16str!("Sparkle 💖 Heart");
assert!(s.is_char_boundary(0));

// high surrogate of `💖`
assert!(s.is_char_boundary(8));
// low surrogate of `💖`
assert!(!s.is_char_boundary(9));

assert!(s.is_char_boundary(s.len()));
source

pub fn get<I>(&self, index: I) -> Option<&Utf16Str>
where I: RangeBounds<usize> + SliceIndex<[u16], Output = [u16]>,

Returns a subslice of this string.

This is the non-panicking alternative to indexing the string. Returns None whenever equivalent indexing operation would panic.

§Examples
let v = utf16str!("⚧️🏳️‍⚧️➡️s");

assert_eq!(Some(utf16str!("⚧️")), v.get(..2));
assert_eq!(Some(utf16str!("🏳️‍⚧️")), v.get(2..8));
assert_eq!(Some(utf16str!("➡️")), v.get(8..10));
assert_eq!(Some(utf16str!("s")), v.get(10..));

assert!(v.get(3..4).is_none());
source

pub fn get_mut<I>(&mut self, index: I) -> Option<&mut Utf16Str>
where I: RangeBounds<usize> + SliceIndex<[u16], Output = [u16]>,

Returns a mutable subslice of this string.

This is the non-panicking alternative to indexing the string. Returns None whenever equivalent indexing operation would panic.

§Examples
let mut v = utf16str!("⚧️🏳️‍⚧️➡️s").to_owned();

assert_eq!(utf16str!("⚧️"), v.get_mut(..2).unwrap());
assert_eq!(utf16str!("🏳️‍⚧️"), v.get_mut(2..8).unwrap());
assert_eq!(utf16str!("➡️"), v.get_mut(8..10).unwrap());
assert_eq!(utf16str!("s"), v.get_mut(10..).unwrap());

assert!(v.get_mut(3..4).is_none());
source

pub fn split_at(&self, mid: usize) -> (&Utf16Str, &Utf16Str)

Divide one string slice into two at an index.

The argument, mid, should be an offset from the start of the string. It must also be on the boundary of a UTF-16 code point.

The two slices returned go from the start of the string slice to mid, and from mid to the end of the string slice.

To get mutable string slices instead, see the split_at_mut method.

§Panics

Panics if mid is not on a UTF-16 code point boundary, or if it is past the end of the last code point of the string slice.

§Examples
let s = utf16str!("Per Martin-Löf");

let (first, last) = s.split_at(3);

assert_eq!("Per", first);
assert_eq!(" Martin-Löf", last);
source

pub fn split_at_mut(&mut self, mid: usize) -> (&mut Utf16Str, &mut Utf16Str)

Divide one mutable string slice into two at an index.

The argument, mid, should be an offset from the start of the string. It must also be on the boundary of a UTF-16 code point.

The two slices returned go from the start of the string slice to mid, and from mid to the end of the string slice.

To get immutable string slices instead, see the split_at method.

§Panics

Panics if mid is not on a UTF-16 code point boundary, or if it is past the end of the last code point of the string slice.

§Examples
let mut s = utf16str!("Per Martin-Löf").to_owned();

let (first, last) = s.split_at_mut(3);

assert_eq!("Per", first);
assert_eq!(" Martin-Löf", last);
source

pub fn chars(&self) -> CharsUtf16<'_>

Returns an iterator over the chars of a string slice.

As this string slice consists of valid UTF-16, we can iterate through a string slice by char. This method returns such an iterator.

It’s important to remember that char represents a Unicode Scalar Value, and might not match your idea of what a ‘character’ is. Iteration over grapheme clusters may be what you actually want. This functionality is not provided by this crate.

source

pub fn char_indices(&self) -> CharIndicesUtf16<'_>

Returns an iterator over the chars of a string slice and their positions.

As this string slice consists of valid UTF-16, we can iterate through a string slice by char. This method returns an iterator of both these chars as well as their offsets.

The iterator yields tuples. The position is first, the char is second.

source

pub fn code_units(&self) -> CodeUnits<'_>

An iterator over the u16 code units of a string slice.

As a UTF-16 string slice consists of a sequence of u16 code units, we can iterate through a string slice by each code unit. This method returns such an iterator.

source

pub fn encode_utf8(&self) -> EncodeUtf8<CharsUtf16<'_>>

Returns an iterator of bytes over the string encoded as UTF-8.

source

pub fn encode_utf32(&self) -> EncodeUtf32<CharsUtf16<'_>>

Returns an iterator of u32 over the sting encoded as UTF-32.

source

pub fn escape_debug(&self) -> EscapeDebug<CharsUtf16<'_>>

Returns an iterator that escapes each char in self with char::escape_debug.

source

pub fn escape_default(&self) -> EscapeDefault<CharsUtf16<'_>>

Returns an iterator that escapes each char in self with char::escape_default.

source

pub fn escape_unicode(&self) -> EscapeUnicode<CharsUtf16<'_>>

Returns an iterator that escapes each char in self with char::escape_unicode.

source

pub fn to_lowercase(&self) -> Utf16String

Returns the lowercase equivalent of this string slice, as a new Utf16String.

‘Lowercase’ is defined according to the terms of the Unicode Derived Core Property Lowercase.

Since some characters can expand into multiple characters when changing the case, this function returns a Utf16String instead of modifying the parameter in-place.

source

pub fn to_uppercase(&self) -> Utf16String

Returns the uppercase equivalent of this string slice, as a new Utf16String.

‘Uppercase’ is defined according to the terms of the Unicode Derived Core Property Uppercase.

Since some characters can expand into multiple characters when changing the case, this function returns a Utf16String instead of modifying the parameter in-place.

Trait Implementations§

source§

impl Clone for WString

source§

fn clone(&self) -> WString

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for WString

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for WString

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for WString

source§

fn deserialize<D>(deserializer: D) -> Result<WString, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl From<Utf16String> for WString

source§

fn from(inner: Utf16String) -> Self

Converts to this type from the input type.
source§

impl From<WString> for Utf16String

source§

fn from(w: WString) -> Utf16String

Converts to this type from the input type.
source§

impl Serialize for WString

source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
source§

impl Deref for WString

§

type Target = Utf16String

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,