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 · 13 comments

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.

@vyazelenko
Copy link
Contributor

@mward Can you have a look? Thanks.

@mward
Copy link
Contributor

mward commented Dec 10, 2024

I'll take a look. Thanks

@mward
Copy link
Contributor

mward commented Dec 12, 2024

@wbprime - I like the idea of supporting slices, in addition to arrays, but PR #1026 generates an enormous amount of code. I'm not sure it is necessary to support all permutations of undersized slices. Do any other implementations provide functionality similar to this?

@wbprime
Copy link
Contributor Author

wbprime commented Dec 13, 2024

@mward Ok, will try to compact the implementation.

@wbprime
Copy link
Contributor Author

wbprime commented Dec 16, 2024

@mward just updated the implementation:

  1. use loops to copy data for a sub set of permutations
  2. use zero when padding, drop support for customed pading value

Could you help review it ?

@mward
Copy link
Contributor

mward commented Dec 18, 2024

I'll check it out

@mward
Copy link
Contributor

mward commented Dec 23, 2024

@wbprime I created a new PR #1040 to resolve this. I incorporated the tests you created. Please take a look

@wbprime
Copy link
Contributor Author

wbprime commented Dec 24, 2024

Hi @mward I think we should not introduce breaking changes in generated code say for primitive array setters. Maybe you can choose a new name for slice setter, like the iter variant do.

@wbprime
Copy link
Contributor Author

wbprime commented Dec 24, 2024

@mward I just leave some comments on your pr, feel free to accept or reject them.

As en is not my native language, just ignore the words or sentences may look impolite/rude to you.

@mward
Copy link
Contributor

mward commented Dec 24, 2024

Hi @mward I think we should not introduce breaking changes in generated code say for primitive array setters. Maybe you can choose a new name for slice setter, like the iter variant do.

changing function param from array to slice doesn't break anything, consumer of library won't need to make any changes

@wbprime
Copy link
Contributor Author

wbprime commented Dec 25, 2024

An array ref is not the safe thing as a slice ref. An array ref has length info built into its type while a slice ref doest.

@mward
Copy link
Contributor

mward commented Dec 25, 2024

True, I meant signature wise

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

3 participants