Can rustfmt preserve vertical alignment on enums/structs

I tend to spend a lot of time formatting code and have generally avoided code formatters because they are never exactly quite what I want. However, I've decided that my energy is probably better spent writing code and just letting a formatter do it's job even if a little different. However, there one area I'd like a little more control over or even getting rustfmt to operate differently.

I tend to have a lot of structs or enums where I'd like to keep the vertical alignment of the names and values/types which I think makes the code far more readable than what rustfmt provides. Here is one example:

const OBJ_STORAGETYPE_MASK              : u32 = 0xc0000000;
const OBJECT_TYPE_FLAGS_DEFINED_MASK    : u32 = 0xf8000000;

#[repr(u32)]
#[derive(Debug, PartialEq, FromPrimitive)]
pub enum ObjectType {
    NxSuperblock          = 0x00000001,
    Btree                 = 0x00000002,
    BtreeNode             = 0x00000003,
    Spaceman              = 0x00000005,
    SpacemanCab           = 0x00000006,
    ...
}

#[derive(Debug)]
struct CheckpointMapping {
    r#type:     ObjectTypeAndFlags,
    subtype:    ObjectTypeAndFlags,
    size:       u32,
    pad:        u32,
    fs_oid:     Oid,
    oid:        Oid,
    paddr:      Oid,
    ...
}

However, all that careful alignment is lost after running it through rustfmt:

const OBJ_STORAGETYPE_MASK: u32 = 0xc0000000;
const OBJECT_TYPE_FLAGS_DEFINED_MASK: u32 = 0xf8000000;

#[repr(u32)]
#[derive(Debug, PartialEq, FromPrimitive)]
pub enum ObjectType {
    NxSuperblock = 0x00000001,
    Btree = 0x00000002,
    BtreeNode = 0x00000003,
    Spaceman = 0x00000005,
    SpacemanCab = 0x00000006,
    ...
}

#[derive(Debug)]
struct CheckpointMapping {
    r#type: ObjectTypeAndFlags,
    subtype: ObjectTypeAndFlags,
    size: u32,
    pad: u32,
    fs_oid: Oid,
    oid: Oid,
    paddr: Oid,
    ...
}

I'm less concerned about the second case with aligning types in a struct, but for the enum, having the values vertically aligned makes it a lot clearer and easier to read and understand. Other items like const variables would be nice, but even harder to support, I realize. Ideally, rustfmt would have an option to align enum on the equals sign using the longest name as the gauge. However, if that is not supported yet, then perhaps just a way to have it pass-through the manual formatting of Enums but still apply other formatting rules. Is there any mechanism for this that I've missed?

1 Like

Are you aware of the existence of #[rustfmt::skip]?

#[rustfmt::skip]
#[repr(u32)]
#[derive(Debug, PartialEq, FromPrimitive)]
pub enum ObjectType {
    NxSuperblock          = 0x00000001,
    Btree                 = 0x00000002,
    BtreeNode             = 0x00000003,
    Spaceman              = 0x00000005,
    SpacemanCab           = 0x00000006,
}
1 Like

I think you want enum_discrim_align_threshold for the first one and struct_field_align_threshold for the second (but neither is stabilized).

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.