odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
6.12k stars 550 forks source link

Handle empty structs in hash (#v) fmt #3756

Closed matias-eduardo closed 2 weeks ago

matias-eduardo commented 2 weeks ago

Added a few checks to handle hash formatting empty structs. They now behave the same as empty arrays.

P.S. Cursed(?) Bonus: Now you can use empty structs as headers in your mega thicc structs. :) P.P.S. fmt_struct might benefit from a refactor now that we have a better understanding of its requirements.

package main

import "core:fmt"

o: struct {

    __EMPTY_STRUCTS_____________________________________: struct {},

    empty: struct {},
    empty_one: struct { one: struct{} },
    empty_two: struct { one: struct{}, two: struct{} },

    __EMPTY_STRUCT_ARRAYS_______________________________: struct {},

    empty_array_zero: [0]struct{},
    empty_array_one: [1]struct{},
    empty_array_two: [2]struct{},

    __EMPTY_STRUCT_SOA_ARRAYS___________________________: struct {},

    empty_soa_array_zero: #soa [0]struct{},
    empty_soa_array_one: #soa [1]struct{},
    empty_soa_array_two: #soa [2]struct{},

    __STRUCTS___________________________________________: struct {},

    one: struct { one: u32 },
    two: struct { one: u32, two: u32 },

    __STRUCT_ARRAYS_____________________________________: struct {},

    array_one: [0]struct{ one: u32 },
    array_two: [2]struct{ one: u32, two: u32 },

    __STRUCT_SOA_ARRAYS_________________________________: struct {},

    soa_array_one: #soa [1]struct{ one: u32 },
    soa_array_two: #soa [2]struct{ one: u32, two: u32 },
}

main :: proc() {
    fmt.printf("%#v\n", o)
}

// OUTPUT BEFORE:
// {
//  __EMPTY_STRUCTS_____________________________________ = {
//  },
//  empty = {
//  },
//  empty_one = {
//      one = {
//      },
//  },
//  empty_two = {
//      one = {
//      },
//      two = {
//      },
//  },
//  __EMPTY_STRUCT_ARRAYS_______________________________ = {
//  },
//  empty_array_zero = [],
//  empty_array_one = [
//      {
//      },
//  ],
//  empty_array_two = [
//      {
//      },
//      {
//      },
//  ],
//  __EMPTY_STRUCT_SOA_ARRAYS___________________________ = {
//  },
//  empty_soa_array_zero = [    ],
//  empty_soa_array_one = [
//      {
//      },
//  ],
//  empty_soa_array_two = [
//      {
//      },
//      {
//      },
//  ],
//  __STRUCTS___________________________________________ = {
//  },
//  one = {
//      one = 0,
//  },
//  two = {
//      one = 0,
//      two = 0,
//  },
//  __STRUCT_ARRAYS_____________________________________ = {
//  },
//  array_one = [],
//  array_two = [
//      {
//          one = 0,
//          two = 0,
//      },
//      {
//          one = 0,
//          two = 0,
//      },
//  ],
//  __STRUCT_SOA_ARRAYS_________________________________ = {
//  },
//  soa_array_one = [
//      {
//          one = 0,
//      },
//  ],
//  soa_array_two = [
//      {
//          one = 0,
//          two = 0,
//      },
//      {
//          one = 0,
//          two = 0,
//      },
//  ],
// }

// OUTPUT AFTER:
// {
//  __EMPTY_STRUCTS_____________________________________ = {},
//  empty = {},
//  empty_one = {
//      one = {},
//  },
//  empty_two = {
//      one = {},
//      two = {},
//  },
//  __EMPTY_STRUCT_ARRAYS_______________________________ = {},
//  empty_array_zero = [],
//  empty_array_one = [
//      {},
//  ],
//  empty_array_two = [
//      {},
//      {},
//  ],
//  __EMPTY_STRUCT_SOA_ARRAYS___________________________ = {},
//  empty_soa_array_zero = [],
//  empty_soa_array_one = [
//      {},
//  ],
//  empty_soa_array_two = [
//      {},
//      {},
//  ],
//  __STRUCTS___________________________________________ = {},
//  one = {
//      one = 0,
//  },
//  two = {
//      one = 0,
//      two = 0,
//  },
//  __STRUCT_ARRAYS_____________________________________ = {},
//  array_one = [],
//  array_two = [
//      {
//          one = 0,
//          two = 0,
//      },
//      {
//          one = 0,
//          two = 0,
//      },
//  ],
//  __STRUCT_SOA_ARRAYS_________________________________ = {},
//  soa_array_one = [
//      {
//          one = 0,
//      },
//  ],
//  soa_array_two = [
//      {
//          one = 0,
//          two = 0,
//      },
//      {
//          one = 0,
//          two = 0,
//      },
//  ],
// }