Use extern for serialized structs

This commit is contained in:
Richard Feldman 2025-11-22 12:30:32 -05:00
parent fdc1a9832d
commit bedcc21575
No known key found for this signature in database
26 changed files with 110 additions and 68 deletions

View file

@ -84,8 +84,9 @@ pub fn serialize(
return @constCast(offset_self);
}
/// Serialized representation of ModuleEnv
pub const Serialized = struct {
/// Serialized representation of CommonEnv
/// Uses extern struct to guarantee consistent field layout across optimization levels.
pub const Serialized = extern struct {
idents: Ident.Store.Serialized,
strings: StringLiteral.Store.Serialized,
exposed_items: ExposedItems.Serialized,

View file

@ -1,22 +1,23 @@
//! Just a small struct to take a span of data in an array
const DataSpan = @This();
/// DataSpan is used for serialization, so it must be extern struct for consistent layout.
pub const DataSpan = extern struct {
start: u32,
len: u32,
start: u32,
len: u32,
/// Creates an empty DataSpan with zero start and zero length.
pub fn empty() DataSpan {
return DataSpan{ .start = 0, .len = 0 };
}
/// Creates an empty DataSpan with zero start and zero length.
pub fn empty() DataSpan {
return DataSpan{ .start = 0, .len = 0 };
}
/// Creates a DataSpan with the specified start position and length.
pub fn init(start: u32, len: u32) DataSpan {
return DataSpan{ .start = start, .len = len };
}
/// Creates a DataSpan with the specified start position and length.
pub fn init(start: u32, len: u32) DataSpan {
return DataSpan{ .start = start, .len = len };
}
/// Converts this DataSpan into a type that contains a span field.
/// This is useful for creating wrapper types around DataSpan.
pub fn as(self: DataSpan, comptime T: type) T {
return @as(T, .{ .span = self });
}
/// Converts this DataSpan into a type that contains a span field.
/// This is useful for creating wrapper types around DataSpan.
pub fn as(self: DataSpan, comptime T: type) T {
return @as(T, .{ .span = self });
}
};

View file

@ -105,7 +105,8 @@ pub const Store = struct {
next_unique_name: u32 = 0,
/// Serialized representation of an Ident.Store
pub const Serialized = struct {
/// Uses extern struct to guarantee consistent field layout across optimization levels.
pub const Serialized = extern struct {
interner: SmallStringInterner.Serialized,
attributes: collections.SafeList(Attributes).Serialized,
next_unique_name: u32,

View file

@ -11,7 +11,7 @@
const std = @import("std");
const testing = std.testing;
const DataSpan = @import("DataSpan.zig");
const DataSpan = @import("DataSpan.zig").DataSpan;
/// Configurable packed DataSpan with customizable bit allocation
pub fn PackedDataSpan(comptime start_bits: u6, comptime length_bits: u6) type {

View file

@ -2,7 +2,7 @@
//! when working with recursive operations
const std = @import("std");
const DataSpan = @import("DataSpan.zig");
const DataSpan = @import("DataSpan.zig").DataSpan;
/// A stack for easily adding and removing index types when doing recursive operations
pub fn Scratch(comptime T: type) type {

View file

@ -204,7 +204,8 @@ pub fn relocate(self: *SmallStringInterner, offset: isize) void {
}
/// Serialized representation of a SmallStringInterner
pub const Serialized = struct {
/// Uses extern struct to guarantee consistent field layout across optimization levels.
pub const Serialized = extern struct {
bytes: collections.SafeList(u8).Serialized,
hash_table: collections.SafeList(Idx).Serialized,
entry_count: u32,

View file

@ -97,7 +97,8 @@ pub const Store = struct {
}
/// Serialized representation of a Store
pub const Serialized = struct {
/// Uses extern struct to guarantee consistent field layout across optimization levels.
pub const Serialized = extern struct {
buffer: collections.SafeList(u8).Serialized,
/// Serialize a Store into this Serialized struct, appending data to the writer

View file

@ -13,7 +13,7 @@ pub const SmallStringInterner = @import("SmallStringInterner.zig");
pub const safe_memory = @import("safe_memory.zig");
pub const target = @import("target.zig");
pub const DataSpan = @import("DataSpan.zig");
pub const DataSpan = @import("DataSpan.zig").DataSpan;
pub const PackedDataSpan = @import("PackedDataSpan.zig").PackedDataSpan;
pub const FunctionArgs = @import("PackedDataSpan.zig").FunctionArgs;
pub const SmallCollections = @import("PackedDataSpan.zig").SmallCollections;