Skip to content

[Rust] proposal: add a new encoder setter method for fixed length array copying from slice ref #1021

Closed
@wbprime

Description

@wbprime

It's very common to serialize an obj as SBE, when obj having a field of String type (or similar) while SBE defining it as fixed length primitive array.

When encoding a field of fixed length primitive array type, one must:

  1. first construct an array
  2. copy bytes into this array
  3. pass this array to Encoder and copy bytes into its backing ByteBuf

It may be more convenient to generate a new method copy from directly slice ref.

For example, there exists a field named "account" of char[16].

Currently it generates a method called "account" with parameter of type &[u8; 16].

/// primitive array field 'account'
/// - min value: 32
/// - max value: 126
/// - null value: 0
/// - characterEncoding: US-ASCII
/// - semanticType: String
/// - encodedOffset: 0
/// - encodedLength: 16
/// - version: 0
#[inline]
pub fn account(&mut self, value: &[u8; 16]) {
    let offset = self.offset;
    let buf = self.get_buf_mut();
    buf.put_bytes_at(offset, value);
}

Following method is proposed to be generated as well:

pub fn account_from_slice_ref(&mut self, value: &[u8]) {
    // Typical implementation

    let offset = self.offset;
    let buf = self.get_buf_mut();

    let len = value.len();
    // How to deal with cases input slice is too large: quietly copy leading bytes of fixed size or return Result or panic ?
    if len > 16 {
        buf.put_slice_at(offset, &value[..16]);
    } else {
        buf.put_slice_at(offset, value);

        // Padding with ZERO
        buf.put_u8_at(len + 0, 0_u8);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions