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] generate slice ref setter in Encoder for fixed size array #1026

Conversation

wbprime
Copy link
Contributor

@wbprime wbprime commented Nov 15, 2024

Releated to #1021

For field of primitive array type, generate 2 more setter methods in its Encoder:

  1. one named with _at_most_N_items_from_slice to enable copy as more as items less than array length to encoding bytes buffer.
  2. one named with _at_most_N_items_from_slice to enable copy as more as items less than array length to encoding bytes buffer. If no enough items provided, will continue padding using a given value provided via a third parameter.

Generated methods differs on backing primitive type for u8 and non u8.

If there exists following SBE definition:

<type name="Fixed4U8" length="4" primitiveType="uint8" semanticType="Data"/>
<type name="Fixed4i32" length="4" primitiveType="int32" />

<field name="fixedU8" id="11" type="Fixed4U8"/>
<field name="fixedi32" id="23" type="Fixed4i32"/>

Then following new methods would be generated:

// primitive array field 'fixedU8'
/// - min value: 0
/// - max value: 254
/// - null value: 255
/// - characterEncoding: null
/// - semanticType: Data
/// - encodedOffset: 0
/// - encodedLength: 4
/// - version: 0
#[inline]
pub fn fixed_u8(&mut self, value: &[u8; 4]) {
    let offset = self.offset;
    let buf = self.get_buf_mut();
    buf.put_bytes_at(offset, value);
}

/// primitive array field 'fixedU8': copy at most 4 items from slice without padding
/// - min value: 0
/// - max value: 254
/// - null value: 255
/// - characterEncoding: null
/// - semanticType: Data
/// - encodedOffset: 0
/// - encodedLength: 4
/// - version: 0
#[inline]
pub fn fixed_u8_at_most_4_items_from_slice(&mut self, value: &[u8]) {
    let offset = self.offset;
    let buf = self.get_buf_mut();
    match value.len() {
        0 => {
            // Copy all 0 items from 'value' into 'fixedU8' of [u8; 4]

            // Leave 4 left items unchanged
        }
        1 => {
            // Copy all 1 items from 'value' into 'fixedU8' of [u8; 4]
            buf.put_slice_at(offset, value);

            // Leave 3 left items unchanged
        }
        2 => {
            // Copy all 2 items from 'value' into 'fixedU8' of [u8; 4]
            buf.put_slice_at(offset, value);

            // Leave 2 left items unchanged
        }
        3 => {
            // Copy all 3 items from 'value' into 'fixedU8' of [u8; 4]
            buf.put_slice_at(offset, value);

            // Leave 1 left items unchanged
        }
        _ => {
            // Copy all 4 items from 'value' into 'fixedU8' of [u8; 4]
            buf.put_slice_at(offset, value);

            // Leave 0 left items unchanged
        }
    }
}

/// primitive array field 'fixedU8': copy at most 4 items from slice with padding
/// - min value: 0
/// - max value: 254
/// - null value: 255
/// - characterEncoding: null
/// - semanticType: Data
/// - encodedOffset: 0
/// - encodedLength: 4
/// - version: 0
#[inline]
pub fn fixed_u8_at_most_4_items_from_slice_padded(&mut self, value: &[u8], padded: u8) {
    let offset = self.offset;
    let buf = self.get_buf_mut();
    match value.len() {
        0 => {
            // Copy all 0 items from 'value' into 'fixedU8' of [u8; 4]

            // Pad 4 left items of 'fixedU8' with parameter "padded"
            buf.put_bytes_at(offset, &[padded; 4]);
        }
        1 => {
            // Copy all 1 items from 'value' into 'fixedU8' of [u8; 4]
            buf.put_slice_at(offset, value);

            // Pad 3 left items of 'fixedU8' with parameter "padded"
            buf.put_bytes_at(offset + 1, &[padded; 3]);
        }
        2 => {
            // Copy all 2 items from 'value' into 'fixedU8' of [u8; 4]
            buf.put_slice_at(offset, value);

            // Pad 2 left items of 'fixedU8' with parameter "padded"
            buf.put_bytes_at(offset + 2, &[padded; 2]);
        }
        3 => {
            // Copy all 3 items from 'value' into 'fixedU8' of [u8; 4]
            buf.put_slice_at(offset, value);

            // Pad 1 left items of 'fixedU8' with parameter "padded"
            buf.put_bytes_at(offset + 3, &[padded; 1]);
        }
        _ => {
            // Copy all 4 items from 'value' into 'fixedU8' of [u8; 4]
            buf.put_slice_at(offset, value);

            // Pad 0 left items of 'fixedU8' with parameter "padded"
        }
    }
}

/// primitive array field 'fixedi32'
/// - min value: -2147483647
/// - max value: 2147483647
/// - null value: -2147483648
/// - characterEncoding: null
/// - semanticType: null
/// - encodedOffset: 4
/// - encodedLength: 16
/// - version: 0
#[inline]
pub fn fixedi_32(&mut self, value: &[i32; 4]) {
    let offset = self.offset + 4;
    let buf = self.get_buf_mut();
    buf.put_i32_at(offset, value[0]);
    buf.put_i32_at(offset + 4, value[1]);
    buf.put_i32_at(offset + 8, value[2]);
    buf.put_i32_at(offset + 12, value[3]);
}

/// primitive array field 'fixedi32': copy at most 4 items from slice without padding
/// - min value: -2147483647
/// - max value: 2147483647
/// - null value: -2147483648
/// - characterEncoding: null
/// - semanticType: null
/// - encodedOffset: 4
/// - encodedLength: 16
/// - version: 0
#[inline]
pub fn fixedi_32_at_most_4_items_from_slice(&mut self, value: &[i32]) {
    let offset = self.offset + 4;
    let buf = self.get_buf_mut();
    match value.len() {
        0 => {
            // Copy all 0 items from 'value' into 'fixedi32' of [i32; 4]

            // Leave 4 left items unchanged
        }
        1 => {
            // Copy all 1 items from 'value' into 'fixedi32' of [i32; 4]
            buf.put_i32_at(offset, value[0]);

            // Leave 3 left items unchanged
        }
        2 => {
            // Copy all 2 items from 'value' into 'fixedi32' of [i32; 4]
            buf.put_i32_at(offset, value[0]);
            buf.put_i32_at(offset + 4, value[1]);

            // Leave 2 left items unchanged
        }
        3 => {
            // Copy all 3 items from 'value' into 'fixedi32' of [i32; 4]
            buf.put_i32_at(offset, value[0]);
            buf.put_i32_at(offset + 4, value[1]);
            buf.put_i32_at(offset + 8, value[2]);

            // Leave 1 left items unchanged
        }
        _ => {
            // Copy all 4 items from 'value' into 'fixedi32' of [i32; 4]
            buf.put_i32_at(offset, value[0]);
            buf.put_i32_at(offset + 4, value[1]);
            buf.put_i32_at(offset + 8, value[2]);
            buf.put_i32_at(offset + 12, value[3]);

            // Leave 0 left items unchanged
        }
    }
}

/// primitive array field 'fixedi32': copy at most 4 items from slice with padding
/// - min value: -2147483647
/// - max value: 2147483647
/// - null value: -2147483648
/// - characterEncoding: null
/// - semanticType: null
/// - encodedOffset: 4
/// - encodedLength: 16
/// - version: 0
#[inline]
pub fn fixedi_32_at_most_4_items_from_slice_padded(&mut self, value: &[i32], padded: i32) {
    let offset = self.offset + 4;
    let buf = self.get_buf_mut();
    match value.len() {
        0 => {
            // Copy all 0 items from 'value' into 'fixedi32' of [i32; 4]

            // Pad 4 left items of 'fixedi32' with parameter "padded"
            buf.put_i32_at(offset, padded);
            buf.put_i32_at(offset + 4, padded);
            buf.put_i32_at(offset + 8, padded);
            buf.put_i32_at(offset + 12, padded);
        }
        1 => {
            // Copy all 1 items from 'value' into 'fixedi32' of [i32; 4]
            buf.put_i32_at(offset, value[0]);

            // Pad 3 left items of 'fixedi32' with parameter "padded"
            buf.put_i32_at(offset + 4, padded);
            buf.put_i32_at(offset + 8, padded);
            buf.put_i32_at(offset + 12, padded);
        }
        2 => {
            // Copy all 2 items from 'value' into 'fixedi32' of [i32; 4]
            buf.put_i32_at(offset, value[0]);
            buf.put_i32_at(offset + 4, value[1]);

            // Pad 2 left items of 'fixedi32' with parameter "padded"
            buf.put_i32_at(offset + 8, padded);
            buf.put_i32_at(offset + 12, padded);
        }
        3 => {
            // Copy all 3 items from 'value' into 'fixedi32' of [i32; 4]
            buf.put_i32_at(offset, value[0]);
            buf.put_i32_at(offset + 4, value[1]);
            buf.put_i32_at(offset + 8, value[2]);

            // Pad 1 left items of 'fixedi32' with parameter "padded"
            buf.put_i32_at(offset + 12, padded);
        }
        _ => {
            // Copy all 4 items from 'value' into 'fixedi32' of [i32; 4]
            buf.put_i32_at(offset, value[0]);
            buf.put_i32_at(offset + 4, value[1]);
            buf.put_i32_at(offset + 8, value[2]);
            buf.put_i32_at(offset + 12, value[3]);

            // Pad 0 left items of 'fixedi32' with parameter "padded"
        }
    }
}

@wbprime wbprime force-pushed the rust-generate-slice-ref-setter-for-fixed-size-array branch from 8bb3a27 to 1a41d6d Compare December 16, 2024 08:09
@wbprime
Copy link
Contributor Author

wbprime commented Dec 25, 2024

Closed as is deprecated by #1040 from @mward .

@wbprime wbprime closed this Dec 25, 2024
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

Successfully merging this pull request may close these issues.

1 participant