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

Add zero_filled constructor #197

Merged
merged 3 commits into from Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions src/array_string.rs
Expand Up @@ -129,6 +129,28 @@ impl<const CAP: usize> ArrayString<CAP>
Ok(vec)
}

/// Creates a new `ArrayString` instance fully filled with ASCII NULL characters (`\0`). Useful
bluss marked this conversation as resolved.
Show resolved Hide resolved
/// to be used as a buffer to collect external data or as a buffer for intermediate processing.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let string = ArrayString::<16>::zero_filled();
/// assert_eq!(string.len(), 16);
/// ```
#[inline]
pub fn zero_filled() -> Self {
assert_capacity_limit!(CAP);
// SAFETY: `assert_capacity_limit` asserts that `len` won't overflow and
// `zeroed` fully fills the array with nulls.
unsafe {
ArrayString {
xs: MaybeUninit::zeroed().assume_init(),
len: CAP as _
}
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can and should copy the code in fn new verbatim, but instead of uninit, we use MaybeUninit::zeroed and len: CAP and it should be good

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!


/// Return the capacity of the `ArrayString`.
///
/// ```
Expand Down
7 changes: 6 additions & 1 deletion tests/tests.rs
Expand Up @@ -773,7 +773,6 @@ fn test_arrayvec_const_constructible() {
assert_eq!(var[..], [vec![3, 5, 8]]);
}


#[test]
fn test_arraystring_const_constructible() {
const AS: ArrayString<10> = ArrayString::new_const();
Expand All @@ -786,3 +785,9 @@ fn test_arraystring_const_constructible() {
}


#[test]
fn test_arraystring_zero_filled_has_some_sanity_checks() {
let string = ArrayString::<4>::zero_filled();
assert_eq!(string.as_str(), "\0\0\0\0");
assert_eq!(string.len(), 4);
}