mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-16 08:46:08 +03:00
termio: MessageData should pick appropriately sized int for len
This commit is contained in:
@ -84,16 +84,15 @@ pub const Message = union(enum) {
|
|||||||
/// are a stable pointer, or require deallocation. This is helpful for thread
|
/// are a stable pointer, or require deallocation. This is helpful for thread
|
||||||
/// messaging utilities.
|
/// messaging utilities.
|
||||||
pub fn MessageData(comptime Elem: type, comptime small_size: comptime_int) type {
|
pub fn MessageData(comptime Elem: type, comptime small_size: comptime_int) type {
|
||||||
assert(small_size <= std.math.maxInt(u8));
|
|
||||||
|
|
||||||
return union(enum) {
|
return union(enum) {
|
||||||
pub const Self = @This();
|
pub const Self = @This();
|
||||||
|
|
||||||
pub const Small = struct {
|
pub const Small = struct {
|
||||||
pub const Max = small_size;
|
pub const Max = small_size;
|
||||||
pub const Array = [Max]Elem;
|
pub const Array = [Max]Elem;
|
||||||
|
pub const Len = std.math.IntFittingRange(0, small_size);
|
||||||
data: Array = undefined,
|
data: Array = undefined,
|
||||||
len: u8 = 0,
|
len: Len = 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Alloc = struct {
|
pub const Alloc = struct {
|
||||||
@ -184,3 +183,14 @@ test "MessageData init alloc" {
|
|||||||
try testing.expect(io == .alloc);
|
try testing.expect(io == .alloc);
|
||||||
io.alloc.alloc.free(io.alloc.data);
|
io.alloc.alloc.free(io.alloc.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "MessageData small fits non-u8 sized data" {
|
||||||
|
const testing = std.testing;
|
||||||
|
const alloc = testing.allocator;
|
||||||
|
|
||||||
|
const len = 500;
|
||||||
|
const Data = MessageData(u8, len);
|
||||||
|
const input: []const u8 = "X" ** len;
|
||||||
|
const io = try Data.init(alloc, input);
|
||||||
|
try testing.expect(io == .small);
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user