Skip to content
This repository has been archived by the owner on Nov 12, 2023. It is now read-only.

Implement the length() methods for requests and replies with the macro. #7

Closed
Antikyth opened this issue Sep 8, 2022 · 3 comments
Closed
Labels
task An essential task for a planned milestone.

Comments

@Antikyth
Copy link
Collaborator

Antikyth commented Sep 8, 2022

The procedural macro that generates X messages in X11 needs to implement the length() method on its messages. This method needs to count up the byte size all the items in the message definition (fields, unused bytes, and field lengths).

@Antikyth Antikyth self-assigned this Sep 9, 2022
@Antikyth Antikyth added this to the 0.1.0 milestone Sep 9, 2022
@Antikyth
Copy link
Collaborator Author

Antikyth commented Sep 9, 2022

The derive macro for cornflakes::ByteSize will probably be useful reference.

@Antikyth Antikyth removed their assignment Sep 15, 2022
@Antikyth
Copy link
Collaborator Author

Antikyth commented Sep 15, 2022

Examples

The code needs to generate the implementation for length() so that can sum the byte sizes, like in this example. The cornflakes::ByteSize trait is how you can get the byte size for any type that is (de)serialized:

pub trait ByteSize {
    fn byte_size(&self) -> usize;
}

And here is an actual example of one of the tests written to ensure that this is implemented correctly:

#[test]
fn reparent_window_length_is_correct() {
    // header (total: 4 bytes):
    // - major opcode (the request's "ID"): u8, 1 byte
    // - empty (for this request)
    // - length of the request: u16, 2 bytes (that's what we're calculating)
    let reparent_window = ReparentWindow {
        target: Window::new(0), // 4 bytes (`u32` wrapper)
        new_parent: Window::new(0), // 4 bytes (`u32` wrapper)
        new_x: 0, // 2 bytes (`u16`)
        new_y: 0, // 2 bytes (`u16`)
    };

    // total is 4 + 4 + 2 + 2 + (header: 4) = 16

    // length of a message is in multiples of 4 bytes, so the length should be 16 / 4 = 4
    assert_eq!(reparent_window.length(), 4);
}

@Antikyth
Copy link
Collaborator Author

This is complete.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
task An essential task for a planned milestone.
Projects
None yet
Development

No branches or pull requests

1 participant