Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wbprime opened this issue Oct 31, 2024 · 1 comment

Comments

@wbprime
Copy link
Contributor

wbprime commented Oct 31, 2024

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);
    }
}
@wbprime
Copy link
Contributor Author

wbprime commented Nov 15, 2024

Hi Iv created a new pr #1026 to address this issue.

Please tell me what you think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant