Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 392e6139 authored by Martin Geisler's avatar Martin Geisler
Browse files

pdl: derive `Copy` where possible

The advice from the standard library[1] is to make a type `Copy` if it
_can_ be `Copy`. Here we can simplify the code a bit by passing values
instead of references.

[1]: https://doc.rust-lang.org/std/marker/trait.Copy.html#when-should-my-type-be-copy

Test: atest pdl_tests pdl_inline_tests
Change-Id: Ib910dfb3ea297fae1b69a7a5571ba0ded365406e
parent 85637296
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ pub struct SourceLocation {
    pub column: usize,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Serialize)]
pub struct SourceRange {
    pub file: FileId,
    pub start: SourceLocation,
@@ -36,14 +36,14 @@ pub struct Comment {
    pub text: String,
}

#[derive(Debug, PartialEq, Eq, Serialize)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum EndiannessValue {
    LittleEndian,
    BigEndian,
}

#[derive(Debug, Serialize)]
#[derive(Debug, Copy, Clone, Serialize)]
#[serde(tag = "kind", rename = "endianness_declaration")]
pub struct Endianness {
    pub loc: SourceRange,
+6 −6
Original line number Diff line number Diff line
@@ -143,7 +143,7 @@ fn mask_field_value(field: &ast::Field) -> Option<proc_macro2::TokenStream> {
}

fn generate_field_parser(
    endianness_value: &ast::EndiannessValue,
    endianness_value: ast::EndiannessValue,
    packet_name: &str,
    field: &ast::Field,
    offset: usize,
@@ -322,7 +322,7 @@ fn generate_packet_decl(
    // the offset manually.
    let mut offset = 0;
    let field_parsers = fields.iter().map(|field| {
        let parser = generate_field_parser(&file.endianness.value, id, field, offset);
        let parser = generate_field_parser(file.endianness.value, id, field, offset);
        offset += get_field_size(field);
        parser
    });
@@ -611,7 +611,7 @@ mod tests {
    #[test]
    fn test_mask_field_value() {
        let loc = ast::SourceRange::default();
        let field = ast::Field::Scalar { loc: loc.clone(), id: String::from("a"), width: 8 };
        let field = ast::Field::Scalar { loc, id: String::from("a"), width: 8 };
        assert_eq!(mask_field_value(&field).map(|m| m.to_string()), None);

        let field = ast::Field::Scalar { loc, id: String::from("a"), width: 24 };
@@ -624,7 +624,7 @@ mod tests {
        let field = ast::Field::Scalar { loc, id: String::from("a"), width: 8 };

        assert_expr_eq(
            generate_field_parser(&ast::EndiannessValue::BigEndian, "Foo", &field, 10),
            generate_field_parser(ast::EndiannessValue::BigEndian, "Foo", &field, 10),
            quote! {
                if bytes.len() < 11 {
                    return Err(Error::InvalidLengthError {
@@ -645,7 +645,7 @@ mod tests {
        let loc = ast::SourceRange::default();
        let field = ast::Field::Scalar { loc, id: String::from("a"), width: 24 };
        assert_expr_eq(
            generate_field_parser(&ast::EndiannessValue::LittleEndian, "Foo", &field, 10),
            generate_field_parser(ast::EndiannessValue::LittleEndian, "Foo", &field, 10),
            quote! {
                if bytes.len() < 13 {
                    return Err(Error::InvalidLengthError {
@@ -667,7 +667,7 @@ mod tests {
        let loc = ast::SourceRange::default();
        let field = ast::Field::Scalar { loc, id: String::from("a"), width: 24 };
        assert_expr_eq(
            generate_field_parser(&ast::EndiannessValue::BigEndian, "Foo", &field, 10),
            generate_field_parser(ast::EndiannessValue::BigEndian, "Foo", &field, 10),
            quote! {
                if bytes.len() < 13 {
                    return Err(Error::InvalidLengthError {