Skip to content

Commit

Permalink
Implement IntoWideIter for &String, add Tests (#34)
Browse files Browse the repository at this point in the history
* Implement `IntoWideIter` for `&String`, add tests

* Update CHANGELOG.md
  • Loading branch information
adumbidiot authored Oct 25, 2022
1 parent f57c164 commit e84e223
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog
## [Unreleased]
### Added
- Added `IntoWideIter` impl for `&String`

## [0.2.0] - 2022-10-20
### Added
Expand Down
65 changes: 65 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ impl<'a> IntoWideIter for &&'a str {
}
}

impl<'a> IntoWideIter for &'a String {
type Iter = std::str::EncodeUtf16<'a>;

#[inline]
fn into_wide_iter(self) -> Self::Iter {
self.as_str().encode_utf16()
}
}

impl<'a> IntoWideIter for &'a [u16] {
type Iter = std::iter::Copied<std::slice::Iter<'a, u16>>;

Expand All @@ -110,3 +119,59 @@ impl<'a> IntoWideIter for &'a Vec<u16> {
self.iter().copied()
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn into_wide_iter_check() {
const DATA: &str = "test argument";
let expected: Vec<u16> = DATA.encode_utf16().collect();

fn check(arg: impl IntoWideIter, expected: &[u16]) {
let arg: Vec<u16> = arg.into_wide_iter().collect();
assert!(arg == expected);
}

{
let data: &str = DATA;
check(data, &expected);
}

{
let data: &&str = &DATA;
check(data, &expected);
}

// TODO: Should IntoWideIter be implemented for String?
// It's always better to pass an &str or an &String, so users should be forced to do that?
// {
// let data: String = DATA.into();
// check(data, &expected);
// }

{
let data: String = DATA.into();
let data: &String = &data;
check(data, &expected);
}

{
let data: Vec<u16> = DATA.encode_utf16().collect();
let data: &[u16] = &data;
check(data, &expected);
}

{
let data: Vec<u16> = DATA.encode_utf16().collect();
check(data, &expected);
}

{
let data: Vec<u16> = DATA.encode_utf16().collect();
let data: &Vec<u16> = &data;
check(data, &expected);
}
}
}

0 comments on commit e84e223

Please sign in to comment.