Closed
Description
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:
- first construct an array
- copy bytes into this array
- 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
Labels
No labels