Skip to content

Commit

Permalink
add missing into_char_vec and conversion for Utf32String. Fixes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
starkat99 committed Apr 6, 2024
1 parent 232819e commit 2938576
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- `Utf32String::into_char_vec` and missing conversion to `Vec<char>` for `Utf32String`. Fixes [#37].

### Fixed
- `U16String::pop_char` panics with surrogate string. Fixes [#38].

Expand Down Expand Up @@ -330,6 +333,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
[#20]: https://github.com/starkat99/widestring-rs/issues/20
[#22]: https://github.com/starkat99/widestring-rs/issues/22
[#28]: https://github.com/starkat99/widestring-rs/issues/28
[#37]: https://github.com/starkat99/widestring-rs/issues/37
[#38]: https://github.com/starkat99/widestring-rs/issues/38

[@nicbn]: https://github.com/nicbn
Expand Down
20 changes: 20 additions & 0 deletions src/utfstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2523,6 +2523,19 @@ impl Utf32String {
self.inner
.splice(range, replace_with.as_slice().iter().copied());
}

/// Converts string into a [`Vec`] of [`char`]s.
///
/// This consumes the string without copying its contents.
#[allow(trivial_casts)]
#[inline]
#[must_use]
pub fn into_char_vec(self) -> Vec<char> {
let mut v = mem::ManuallyDrop::new(self.into_vec());
let (ptr, len, cap) = (v.as_mut_ptr(), v.len(), v.capacity());
// SAFETY: Self should be valid UTF-32 so chars will be in range
unsafe { Vec::from_raw_parts(ptr as *mut char, len, cap) }
}
}

impl AsMut<[char]> for Utf32String {
Expand Down Expand Up @@ -2553,6 +2566,13 @@ impl From<&[char]> for Utf32String {
}
}

impl From<Utf32String> for Vec<char> {
#[inline]
fn from(value: Utf32String) -> Self {
value.into_char_vec()
}
}

impl PartialEq<[char]> for Utf32String {
#[inline]
fn eq(&self, other: &[char]) -> bool {
Expand Down

0 comments on commit 2938576

Please sign in to comment.