Zig: std.ArrayList: make the slice field valid and public, and the capacity a separate field

Created on 30 Mar 2020  路  2Comments  路  Source: ziglang/zig

Thanks to @thejoshwolfe for this idea.

Before:

        /// Use `span` instead of slicing this directly, because if you don't
        /// specify the end position of the slice, this will potentially give
        /// you uninitialized memory.
        items: Slice,
        len: usize,
        allocator: *Allocator,

After:

        /// It's OK to use this field! It's the slice of valid objects.
        items: Slice,
        capacity: usize,
        allocator: *Allocator,

This requires ArrayList implementation to do some unsafe slicing when dealing with memory, but it will be encapsulated. No more need for callers to use at, toSlice, span or anything like that, they'll be able to access items directly.

I apologize to everyone for not doing it this way originally.

accepted proposal standard library

Most helpful comment

taking this

All 2 comments

taking this

Should there be a len() function?
For example: table_entry.attrs.items.len vs table_entry.attrs.len().

Was this page helpful?
0 / 5 - 0 ratings