aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2024-08-31 02:50:11 +0100
committermlugg <mlugg@mlugg.co.uk>2024-09-01 17:34:07 +0100
commit0b9fccf508dc85fa522947d1cf6ff84f78f2dcb4 (patch)
tree7371629835c5ee935b33515395f7ad7fbd64c897 /lib/std
parent6e3e23a941c6c82550c41771a223afeec4accd47 (diff)
downloadzig-0b9fccf508dc85fa522947d1cf6ff84f78f2dcb4.tar.gz
zig-0b9fccf508dc85fa522947d1cf6ff84f78f2dcb4.zip
std: deprecate some incorrect default initializations
In favour of newly-added decls, which can be used via decl literals.
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/array_hash_map.zig8
-rw-r--r--lib/std/array_list.zig8
-rw-r--r--lib/std/hash_map.zig9
-rw-r--r--lib/std/heap/general_purpose_allocator.zig11
4 files changed, 36 insertions, 0 deletions
diff --git a/lib/std/array_hash_map.zig b/lib/std/array_hash_map.zig
index eb31d1cae3..1b96be472a 100644
--- a/lib/std/array_hash_map.zig
+++ b/lib/std/array_hash_map.zig
@@ -510,6 +510,8 @@ pub fn ArrayHashMap(
/// `store_hash` is `false` and the number of entries in the map is less than 9,
/// the overhead cost of using `ArrayHashMapUnmanaged` rather than `std.ArrayList` is
/// only a single pointer-sized integer.
+///
+/// Default initialization of this struct is deprecated; use `.empty` instead.
pub fn ArrayHashMapUnmanaged(
comptime K: type,
comptime V: type,
@@ -538,6 +540,12 @@ pub fn ArrayHashMapUnmanaged(
/// Used to detect memory safety violations.
pointer_stability: std.debug.SafetyLock = .{},
+ /// A map containing no keys or values.
+ pub const empty: Self = .{
+ .entries = .{},
+ .index_header = null,
+ };
+
/// Modifying the key is allowed only if it does not change the hash.
/// Modifying the value is allowed.
/// Entry pointers become invalid whenever this ArrayHashMap is modified,
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig
index 2510973692..24098a01f6 100644
--- a/lib/std/array_list.zig
+++ b/lib/std/array_list.zig
@@ -618,6 +618,8 @@ pub fn ArrayListUnmanaged(comptime T: type) type {
/// Functions that potentially allocate memory accept an `Allocator` parameter.
/// Initialize directly or with `initCapacity`, and deinitialize with `deinit`
/// or use `toOwnedSlice`.
+///
+/// Default initialization of this struct is deprecated; use `.empty` instead.
pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) type {
if (alignment) |a| {
if (a == @alignOf(T)) {
@@ -638,6 +640,12 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
/// additional memory.
capacity: usize = 0,
+ /// An ArrayList containing no elements.
+ pub const empty: Self = .{
+ .items = &.{},
+ .capacity = 0,
+ };
+
pub const Slice = if (alignment) |a| ([]align(a) T) else []T;
pub fn SentinelSlice(comptime s: T) type {
diff --git a/lib/std/hash_map.zig b/lib/std/hash_map.zig
index 1c8a5d78af..7d7b2739c3 100644
--- a/lib/std/hash_map.zig
+++ b/lib/std/hash_map.zig
@@ -721,6 +721,8 @@ pub fn HashMap(
/// the price of handling size with u32, which should be reasonable enough
/// for almost all uses.
/// Deletions are achieved with tombstones.
+///
+/// Default initialization of this struct is deprecated; use `.empty` instead.
pub fn HashMapUnmanaged(
comptime K: type,
comptime V: type,
@@ -762,6 +764,13 @@ pub fn HashMapUnmanaged(
/// Capacity of the first grow when bootstrapping the hashmap.
const minimal_capacity = 8;
+ /// A map containing no keys or values.
+ pub const empty: Self = .{
+ .metadata = null,
+ .size = 0,
+ .available = 0,
+ };
+
// This hashmap is specially designed for sizes that fit in a u32.
pub const Size = u32;
diff --git a/lib/std/heap/general_purpose_allocator.zig b/lib/std/heap/general_purpose_allocator.zig
index 61e256d290..b760c9d85d 100644
--- a/lib/std/heap/general_purpose_allocator.zig
+++ b/lib/std/heap/general_purpose_allocator.zig
@@ -157,6 +157,7 @@ pub const Config = struct {
pub const Check = enum { ok, leak };
+/// Default initialization of this struct is deprecated; use `.init` instead.
pub fn GeneralPurposeAllocator(comptime config: Config) type {
return struct {
backing_allocator: Allocator = std.heap.page_allocator,
@@ -174,6 +175,16 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
const Self = @This();
+ /// The initial state of a `GeneralPurposeAllocator`, containing no allocations and backed by the system page allocator.
+ pub const init: Self = .{
+ .backing_allocator = std.heap.page_allocator,
+ .buckets = [1]Buckets{.{}} ** small_bucket_count,
+ .cur_buckets = [1]?*BucketHeader{null} ** small_bucket_count,
+ .large_allocations = .{},
+ .empty_buckets = if (config.retain_metadata) .{} else {},
+ .bucket_node_pool = .init(std.heap.page_allocator),
+ };
+
const total_requested_bytes_init = if (config.enable_memory_limit) @as(usize, 0) else {};
const requested_memory_limit_init = if (config.enable_memory_limit) @as(usize, math.maxInt(usize)) else {};