Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement IntoWideIter for &String, add Tests #34

Merged
merged 2 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
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
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);
}
}
}