From f5acafa0b386cee703c43ce886814c3b03098762 Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 13 Aug 2021 20:04:44 -0300 Subject: [PATCH 1/3] Add zero_filled constructor --- src/array_string.rs | 22 ++++++++++++++++++++++ tests/tests.rs | 7 ++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/array_string.rs b/src/array_string.rs index a044cb5..9bdefcf 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -129,6 +129,28 @@ impl ArrayString Ok(vec) } + /// Creates a new `ArrayString` instance fully filled with ASCII NULL characters (`\0`). Useful + /// 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 _ + } + } + } + /// Return the capacity of the `ArrayString`. /// /// ``` diff --git a/tests/tests.rs b/tests/tests.rs index 26d09ae..781c849 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -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(); @@ -786,3 +785,9 @@ fn test_arraystring_const_constructible() { } +#[test] +fn test_arraystring_has_some_sanity_checks() { + let string = ArrayString::<4>::zero_filled(); + assert_eq!(string.as_str(), "\0\0\0\0"); + assert_eq!(string.len(), 4); +} \ No newline at end of file From e98cb1c81a122c8ef1232234c28f61d851d689b3 Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 13 Aug 2021 20:06:03 -0300 Subject: [PATCH 2/3] Typo --- tests/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests.rs b/tests/tests.rs index 781c849..2f8a5ef 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -786,7 +786,7 @@ fn test_arraystring_const_constructible() { #[test] -fn test_arraystring_has_some_sanity_checks() { +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); From 1e74077b72fb813b6312cf592a1a36dbbac0df12 Mon Sep 17 00:00:00 2001 From: bluss Date: Sat, 14 Aug 2021 13:37:00 +0200 Subject: [PATCH 3/3] Doc/wording edit for zero_filled --- src/array_string.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/array_string.rs b/src/array_string.rs index 9bdefcf..639fe74 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -129,7 +129,7 @@ impl ArrayString Ok(vec) } - /// Creates a new `ArrayString` instance fully filled with ASCII NULL characters (`\0`). Useful + /// Create a new `ArrayString` value fully filled with ASCII NULL characters (`\0`). Useful /// to be used as a buffer to collect external data or as a buffer for intermediate processing. /// /// ```