| 76 | impl<'a, const N: usize> Decoder<'a, ArrayString<N>> for ArrayStringDecoder<'a, N> { |
| 77 | #[inline(always)] |
| 78 | fn decode_in_place(&mut self, out: &mut MaybeUninit<ArrayString<N>>) { |
| 79 | let s: &str = self.0.decode(); |
| 80 | let array_string = out.write(ArrayString::new()); |
| 81 | |
| 82 | // Avoid copying lots of memory for 1 byte strings. |
| 83 | // TODO miri doesn't like ArrayString::as_mut_str().as_mut_ptr(), replace with ArrayString::as_mut_ptr() when available. |
| 84 | if N > 64 || cfg!(miri) { |
| 85 | // Safety: We've ensured `self.lengths.max_len() <= N` in populate. |
| 86 | unsafe { array_string.try_push_str(s).unwrap_unchecked() }; |
| 87 | return; |
| 88 | } |
| 89 | // Empty s points to no valid bytes, so we can't unsafe_wild_copy. |
| 90 | if s.is_empty() { |
| 91 | return; |
| 92 | } |
| 93 | // Safety: We just checked n != 0 and ensured `self.lengths.max_len() <= N` in populate. |
| 94 | // Also, `dst` has room for `[u8; N]` since it's an ArrayString<N>. |
| 95 | unsafe { |
| 96 | let src = s.as_ptr(); |
| 97 | let dst = array_string.as_mut_str().as_mut_ptr(); |
| 98 | let n = s.len(); |
| 99 | unsafe_wild_copy!([u8; N], src, dst, n); |
| 100 | array_string.set_len(s.len()); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | impl<'a, const N: usize> Decode<'a> for ArrayString<N> { |
| 105 | type Decoder = ArrayStringDecoder<'a, N>; |