@@ -535,39 +535,42 @@ void ByteBuffer<Codec>::SetElement(size_t position, const value_type& element) {
535535 codec_.Encode (element, write_span);
536536 return ;
537537 }
538+
539+ // Variable-sized elements - `else` is needed because it's a compile-time check.
540+ else {
541+ const size_t element_size = static_cast <size_t >(element.size ());
538542
539- const size_t element_size = static_cast <size_t >(element.size ());
540-
541- // Defensive check for unlikely extremely large element size that exceeds uint32.
542- if (element_size > static_cast <size_t >(std::numeric_limits<uint32_t >::max ())) {
543- throw InvalidInputException (" Variable-size element payload exceeds uint32 capacity.. Woohhh!!" );
544- }
545-
546- // For variable-size elements, we append the element to the write buffer and update offsets_.
547- //
548- // We append the element to the write buffer and update offsets_.
549- //
550- // This can result on orphaned bytes if a position is set multiple times or positions written out of order.
551- // This is intentional to allow random writes of elements while the buffer is built.
552- // During FinalizeAndTakeBuffer, the buffer is rebuilt to be sequential and orphaned bytes are removed.
553- const size_t offset = write_buffer_.size ();
554- offsets_[position] = offset;
555- append_u32_le (write_buffer_, static_cast <uint32_t >(element_size));
556- const size_t payload_offset = write_buffer_.size ();
557- write_buffer_.resize (payload_offset + element_size);
558- auto write_span = tcb::span<uint8_t >(write_buffer_.data () + payload_offset, element_size);
559- codec_.Encode (element, write_span);
560-
561- // Update next_expected_write_position_ for sequential write checking.
562- if (next_expected_write_position_ != kUnsetVariableElementOffset ) {
563- if (position == next_expected_write_position_) {
564- next_expected_write_position_ += 1 ;
565- } else {
566- next_expected_write_position_ = kUnsetVariableElementOffset ;
543+ // Defensive check for unlikely extremely large element size that exceeds uint32.
544+ if (element_size > static_cast <size_t >(std::numeric_limits<uint32_t >::max ())) {
545+ throw InvalidInputException (" Variable-size element payload exceeds uint32 capacity.. Woohhh!!" );
567546 }
568- }
569547
570- RebindSpanToWriteBuffer ();
548+ // For variable-size elements, we append the element to the write buffer and update offsets_.
549+ //
550+ // We append the element to the write buffer and update offsets_.
551+ //
552+ // This can result on orphaned bytes if a position is set multiple times or positions written out of order.
553+ // This is intentional to allow random writes of elements while the buffer is built.
554+ // During FinalizeAndTakeBuffer, the buffer is rebuilt to be sequential and orphaned bytes are removed.
555+ const size_t offset = write_buffer_.size ();
556+ offsets_[position] = offset;
557+ append_u32_le (write_buffer_, static_cast <uint32_t >(element_size));
558+ const size_t payload_offset = write_buffer_.size ();
559+ write_buffer_.resize (payload_offset + element_size);
560+ auto write_span = tcb::span<uint8_t >(write_buffer_.data () + payload_offset, element_size);
561+ codec_.Encode (element, write_span);
562+
563+ // Update next_expected_write_position_ for sequential write checking.
564+ if (next_expected_write_position_ != kUnsetVariableElementOffset ) {
565+ if (position == next_expected_write_position_) {
566+ next_expected_write_position_ += 1 ;
567+ } else {
568+ next_expected_write_position_ = kUnsetVariableElementOffset ;
569+ }
570+ }
571+
572+ RebindSpanToWriteBuffer ();
573+ }
571574}
572575
573576template <class Codec >
0 commit comments