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 Extend/FromIterator for ArrayString #126

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/array_string.rs
Expand Up @@ -8,6 +8,7 @@ use std::str;
use std::str::FromStr;
use std::str::Utf8Error;
use std::slice;
use std::iter;

use array::{Array, ArrayExt};
use array::Index;
Expand Down Expand Up @@ -518,6 +519,52 @@ impl<A> FromStr for ArrayString<A>
}
}

/// Extend the `ArrayString` with an iterator.
///
/// Does not extract more items than there is space for. No error
/// occurs if there are more iterator elements.
///
/// Caveat: Because the iterator cannot guess how large a character
/// will be in bytes before it sees it, it will potentially leave
/// characters unextracted even if there is room for them. This is
/// because characters can take up to four bytes of space, and if
/// there are less than four bytes available, this code will stop.
impl<A> iter::Extend<char> for ArrayString<A>
where A: Array<Item=u8> + Copy
{
fn extend<T: IntoIterator<Item=char>>(&mut self, iter: T) {
let mut iter = iter.into_iter();
while self.len() < self.capacity() - 4 {
if let Some(c) = iter.next() {
self.push(c)
} else {
return
}
}
}
}

/// Create an `ArrayString` from an iterator.
///
/// Does not extract more items than there is space for. No error
/// occurs if there are more iterator elements.
///
/// Caveat: Because the iterator cannot guess how large a character
/// will be in bytes before it sees it, it will potentially leave
/// characters unextracted even if there is room for them. This is
/// because characters can take up to four bytes of space, and if
/// there are less than four bytes available, this code will stop.
impl<A> iter::FromIterator<char> for ArrayString<A>
where A: Array<Item=u8> + Copy
{
fn from_iter<T: IntoIterator<Item=char>>(iter: T) -> Self {
let mut array = ArrayString::new();
array.extend(iter);
array
}
}


#[cfg(feature="serde-1")]
/// Requires crate feature `"serde-1"`
impl<A> Serialize for ArrayString<A>
Expand Down