assert_has_field 0.1.0

A Rust macro for checking if a struct has a specific field.
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented2 out of 2 items with examples
  • Size
  • Source code size: 7.38 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 176.25 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 6s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • JohnScience/assert_has_field
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • JohnScience

assert_has_field - a Rust macro for checking if a struct has a specific field

Crates.io Downloads Documentation License Dependency Status

This macro is designed to be used in Rust code to assert that a struct has a specific field and, if necessary, that this field of specific type.

Usage

Checking that a struct has a field

use assert_has_field::assert_has_field;

#[allow(dead_code)]
struct MyStruct {
    field1: i32,
    field2: String,
}

assert_has_field!(MyStruct, field1); // This will compile

Checking that a struct has a field of a specific type

use assert_has_field::assert_has_field;

#[allow(dead_code)]
struct MyStruct {
    field1: i32,
    field2: String,
}

assert_has_field!(MyStruct, field1: i32); // This will compile

Checking that a struct has a field of a specific type (failure case)

use assert_has_field::assert_has_field;

#[allow(dead_code)]
struct MyStruct {
    field1: i32,
    field2: String,
}

assert_has_field!(MyStruct, field1: String); // This will fail to compile

How it works

#[macro_export]
macro_rules! assert_has_field {
    ($struct:ty, $field:ident $(: $field_ty:ty)?) => {
        // The const block forces the const evaluation.
        #[allow(
            unreachable_code,
            unused_variables,
            clippy::diverging_sub_expression,
        )]
        const _: () = {
            // `if false { ... }` ensures that the unreacahble! macro invokation is indeed unreachable.
            if false {
                // Rust performs the type-checking at compile time even if the code is unreachable.
                //
                // The return type of core::unreachable!() is never type,
                // which can be assigned to any type.
                let unreachable_obj: $struct = core::unreachable!();
                let _ $(: $field_ty)? = unreachable_obj.$field;
            }
        };
    };
}

On the real use-cases of this macro

Let's say that you're writing a backend server and have a DTO, which is meant to be used on the frontend. Assume that this DTO aggregates different kinds of data that pertains to a candidate. You may be in a situation where candidate_id is stored in one of the fields-structures. You can use [assert_has_field] to document that expectation and future-proof the type in case the field-structure that used to store candidate_id is removed entirely or modified in a way that moves or removes the candidate_id.