Repository: vercel/next.js
File: turbopack/crates/turbo-tasks-macros/src/derive/task_storage_macro.rs (Rust)

Code before:
    } else {
        0
    };
    let persisted_mask = if persisted_count > 0 {
        (1u16 << persisted_count) - 1
    } else {
        0
    };

    quote! {
        bitfield::bitfield! {
            #[doc = "Combined bitfield for task flags."]
            #[doc = ""]
            #[doc = "Bit layout: [meta flags: 0..M] [data flags: M..M+D] [transient: M+D..]"]
            #[doc = "This ordering allows separate masks for per-category serialization."]
            #[derive(Clone, Default, PartialEq, Eq)]
            pub struct TaskFlags(u16);
            impl Debug;

            #(#bitfield_accessors;)*
        }

        #[automatically_derived]
        impl TaskFlags {
            #[doc = "Mask for persisted meta flags"]
            pub const META_MASK: u16 = #meta_mask;

            #[doc = "Mask for persisted data flags"]
            pub const DATA_MASK: u16 = #data_mask;

            #[doc = "Mask for all persisted flags (meta + data)"]
            pub const PERSISTED_MASK: u16 = #persisted_mask;

            #[doc = "Get the raw bits value"]
            pub fn bits(&self) -> u16 {
                self.0
            }

            #[doc = "Get only the persisted meta bits (for meta serialization)"]
            pub fn persisted_meta_bits(&self) -> u16 {
                self.0 & Self::META_MASK
            }

            #[doc = "Get only the persisted data bits (for data serialization)"]
            pub fn persisted_data_bits(&self) -> u16 {
                self.0 & Self::DATA_MASK
            }

            #[doc = "Get all persisted bits (for serialization)"]
            pub fn persisted_bits(&self) -> u16 {
                self.0 & Self::PERSISTED_MASK

Review comment (suggestion):
This could benefit from an assertion that these counts are all <=16, if there's not one somewhere already.

Code after:
            let bit_idx = i;
            quote! {
                pub #field_name, #set_name: #bit_idx
            }
        })
        .collect();

    // Generate masks for each category
    // Meta flags are in bits 0..meta_count
    // Data flags are in bits meta_count..meta_count+data_count
    // Combined persisted mask covers both
    let meta_mask = if meta_count > 0 {
        (1u16 << meta_count) - 1
    } else {
        0
    };
    let data_mask = if data_count > 0 {
        ((1u16 << data_count) - 1) << meta_count
    } else {
        0
    };
    let persisted_mask = if persisted_count > 0 {
        (1u16 << persisted_count) - 1
    } else {
        0
    };

    quote! {
        bitfield::bitfield! {
            #[doc = "Combined bitfield for task flags."]
            #[doc = ""]
            #[doc = "Bit layout: [meta flags: 0..M] [data flags: M..M+D] [transient: M+D..]"]
            #[doc = "This ordering allows separate masks for per-category serialization."]
            #[derive(Clone, Default, PartialEq, Eq)]
            pub struct TaskFlags(u16);
            impl Debug;

            #(#bitfield_accessors;)*
        }

        #[automatically_derived]
        impl TaskFlags {
            #[doc = "Mask for persisted meta flags"]
            pub const META_MASK: u16 = #meta_mask;

            #[doc = "Mask for persisted data flags"]
            pub const DATA_MASK: u16 = #data_mask;

            #[doc = "Mask for all persisted flags (meta + data)"]
            pub const PERSISTED_MASK: u16 = #persisted_mask;