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

Method encode

src/ext/arrayvec.rs:12–32  ·  view source on GitHub ↗
(&mut self, t: &ArrayString<N>)

Source from the content-addressed store, hash-verified

10impl<const N: usize> Encoder<ArrayString<N>> for StrEncoder {
11 #[inline(always)]
12 fn encode(&mut self, t: &ArrayString<N>) {
13 // Only lengths < 255 are fast to encode and avoid copying lots of memory for 1 byte strings.
14 // TODO miri doesn't like ArrayString::as_str().as_ptr(), replace with ArrayString::as_ptr() when available.
15 if N > 64 || cfg!(miri) {
16 self.encode(t.as_str());
17 return;
18 }
19
20 let s = t.as_str();
21 self.0.lengths.encode_less_than_255(s.len());
22 let primitives = self.0.elements.as_primitive().unwrap();
23 primitives.reserve(N); // TODO Buffer::reserve impl additional * N so we can remove encode_vectored impl.
24 let dst = primitives.end_ptr();
25
26 // Safety: `s.as_ptr()` points to `N` valid bytes since it's referencing an ArrayString<N>.
27 // `dst` has enough space for `[T; N]` because we've reserved `N`.
28 unsafe {
29 *(dst as *mut MaybeUninit<[u8; N]>) = *(s.as_ptr() as *const MaybeUninit<[u8; N]>);
30 primitives.set_end_ptr(dst.add(s.len()));
31 }
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.

Callers

nothing calls this directly

Calls 8

as_slice_assert_lenFunction · 0.85
encode_less_than_255Method · 0.80
end_ptrMethod · 0.80
as_ptrMethod · 0.80
set_end_ptrMethod · 0.80
lenMethod · 0.45
as_primitiveMethod · 0.45
reserveMethod · 0.45

Tested by

no test coverage detected