Skip to content

← Smart Pointers and Interior Mutability step 4 of 26

Medium Primitives

Reduce an enum’s inline size with Box

Change the Blob variant of the supplied Msg enum to hold Box<[u8; 512]> instead of an inline [u8; 512]. Update its construction while preserving the routing behavior.

pub fn route(msgs: Vec<(String, String)>) -> Vec<String>

Process each (kind, payload) pair in input order and return one output string per pair. Match kinds exactly, without trimming or changing case.

Kind Output
ping ping, regardless of payload
text text: followed by the unchanged payload
blob blob: followed by the decimal sum of the generated bytes
Anything else drop: followed by the unchanged kind

Empty input returns an empty vector. Empty text remains text:. Unknown kinds still produce a drop line; they are not omitted from the result.

Fill the blob using UTF-8 bytes

The buffer has exactly 512 bytes. For a nonempty payload, set byte i to payload.as_bytes()[i % payload.len()]. Repeat short payloads cyclically; for payloads longer than 512 bytes, use just the first 512 bytes. An empty payload produces 512 zero bytes. Sum the bytes as unsigned values.

This is a byte operation, not character repetition. A multibyte UTF-8 character can cross the end of the buffer; the buffer is not decoded as text.

("blob", "A")   -> "blob:33280"
("blob", "")    -> "blob:0"
("blob", "abc") -> "blob:50175"
("blob", "cba") -> "blob:50177"

The final two sums differ because 512 is not divisible by three. Preserve the starter's fill and sum logic.

Fix the layout warning

The starter computes the required outputs, but Clippy reports large_enum_variant. Its Blob contains a 512-byte inline array, much larger than the other variants' inline data. Every Msg value must have the same size and enough space for any variant, including Blob.

Box moves the array storage into a separate allocation and puts the owning pointer in the variant. Change Blob's field type, then construct it with Box::new(buf). The existing b.iter() call still works through dereferencing. Keep Ping and Text unchanged.

Do not treat specific enum sizes as portable constants. Alignment, target and compiler layout choices matter, and Rust's default representation does not promise a simple fixed tag-plus-payload formula. Use size_of::<Msg>() to inspect the type in a particular build. See the Rust layout reference.

Understand the tradeoff

A smaller inline type can reduce storage for collections of Msg and the amount of data involved in moving values. A Rust move does not guarantee a full-size memory copy: the compiler may optimize it away or construct a value at its final location.

Boxing a blob adds heap storage and indirection. Reading an existing Box does not allocate another buffer. Whether the overall change helps depends on the number and mix of messages, their lifetimes and the operations performed on them. A common large variant does not automatically make boxing slower, and a rare one does not prove it is faster.

This exercise asks you to practise the boxed representation and pass the lint; it does not supply a production traffic distribution or establish a speedup. See Clippy's large_enum_variant documentation. Passing requires compilation, correct outputs and Clippy with warnings denied.