MCPcopy Create free account
hub / github.com/SoftbearStudios/bitcode / encode_vectored

Method encode_vectored

src/ext/arrayvec.rs:34–57  ·  view source on GitHub ↗
(&mut self, i: impl Iterator<Item = &'a ArrayString<N>> + Clone)

Source from the content-addressed store, hash-verified

32 }
33 #[inline(never)]
34 fn encode_vectored<'a>(&mut self, i: impl Iterator<Item = &'a ArrayString<N>> + Clone) {
35 // Only lengths < 255 are fast to encode and avoid copying lots of memory for 1 byte strings.
36 // TODO miri doesn't like ArrayString::as_str().as_ptr(), replace with ArrayString::as_ptr() when available.
37 if N > 64 || cfg!(miri) {
38 self.encode_vectored(i.map(|t| t.as_str()));
39 return;
40 }
41
42 // This encode_vectored impl is same as encode impl, but pulls the reserve out of the loop.
43 let primitives = self.0.elements.as_primitive().unwrap();
44 primitives.reserve(i.size_hint().1.unwrap() * N);
45 let mut dst = primitives.end_ptr();
46 for t in i {
47 let s = t.as_str();
48 self.0.lengths.encode_less_than_255(s.len());
49 // Safety: `s.as_ptr()` points to `N` valid bytes since it's referencing an ArrayString<N>.
50 // `dst` has enough space for `[T; N]` because we've reserved `size_hint * N`.
51 unsafe {
52 *(dst as *mut MaybeUninit<[u8; N]>) = *(s.as_ptr() as *const MaybeUninit<[u8; N]>);
53 dst = dst.add(s.len());
54 }
55 }
56 primitives.set_end_ptr(dst);
57 }
58}
59impl<const N: usize> Encode for ArrayString<N> {
60 type Encoder = StrEncoder;

Callers

nothing calls this directly

Calls 8

size_hintMethod · 0.80
end_ptrMethod · 0.80
encode_less_than_255Method · 0.80
as_ptrMethod · 0.80
set_end_ptrMethod · 0.80
as_primitiveMethod · 0.45
reserveMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected