From 97de601dcd7ab658f63111e77942246cb5d27a29 Mon Sep 17 00:00:00 2001 From: "R.Eugenio" Date: Tue, 23 Dec 2025 01:27:25 +0100 Subject: [PATCH] refactor: Migrar ArrayList a ArrayListUnmanaged (Zig 0.15) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - statement.zig: collectAll(), Row.toValues() - vtable.zig: generateSchema(), EponymousModule.query() - Documentación actualizada con sintaxis moderna Cambio de API: - std.ArrayList(T).init(alloc) → std.ArrayListUnmanaged(T) = .{} - list.append(item) → list.append(alloc, item) - list.deinit() → list.deinit(alloc) - list.toOwnedSlice() → list.toOwnedSlice(alloc) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- src/statement.zig | 22 +++++++++++----------- src/vtable.zig | 48 +++++++++++++++++++++++------------------------ 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/statement.zig b/src/statement.zig index b9c52d6..a47caaf 100644 --- a/src/statement.zig +++ b/src/statement.zig @@ -531,18 +531,18 @@ pub const Statement = struct { /// Each row is represented as an array of column values. /// Caller owns the returned memory. pub fn collectAll(self: *Self, allocator: std.mem.Allocator) ![]Row.Values { - var rows = std.ArrayList(Row.Values).init(allocator); + var rows: std.ArrayListUnmanaged(Row.Values) = .{}; errdefer { for (rows.items) |*r| r.deinit(allocator); - rows.deinit(); + rows.deinit(allocator); } while (try self.step()) { const row = Row{ .stmt = self }; - try rows.append(try row.toValues(allocator)); + try rows.append(allocator, try row.toValues(allocator)); } - return rows.toOwnedSlice(); + return rows.toOwnedSlice(allocator); } }; @@ -680,17 +680,17 @@ pub const Row = struct { /// name: []const u8, // Will be allocated copy /// }; /// - /// var users = std.ArrayList(User).init(allocator); + /// var users: std.ArrayListUnmanaged(User) = .{}; /// defer { /// for (users.items) |user| Row.freeStruct(User, user, allocator); - /// users.deinit(); + /// users.deinit(allocator); /// } /// /// var stmt = try db.prepare("SELECT id, name FROM users"); /// var iter = stmt.iterator(); /// while (try iter.next()) |row| { /// const user = try row.toAlloc(User, allocator); - /// try users.append(user); + /// try users.append(allocator, user); /// } /// ``` pub fn toAlloc(self: Self, comptime T: type, allocator: std.mem.Allocator) !T { @@ -862,10 +862,10 @@ pub const Row = struct { const items = try allocator.alloc(Value, count); errdefer allocator.free(items); - var text_copies = std.ArrayList([]u8).init(allocator); + var text_copies: std.ArrayListUnmanaged([]u8) = .{}; errdefer { for (text_copies.items) |copy| allocator.free(copy); - text_copies.deinit(); + text_copies.deinit(allocator); } for (0..count) |i| { @@ -877,7 +877,7 @@ pub const Row = struct { .text => blk: { if (self.text(idx)) |t| { const copy = try allocator.dupe(u8, t); - try text_copies.append(copy); + try text_copies.append(allocator, copy); break :blk .{ .text = copy }; } break :blk .{ .null_value = {} }; @@ -885,7 +885,7 @@ pub const Row = struct { .blob => blk: { if (self.blob(idx)) |b| { const copy = try allocator.dupe(u8, b); - try text_copies.append(copy); + try text_copies.append(allocator, copy); break :blk .{ .blob = copy }; } break :blk .{ .null_value = {} }; diff --git a/src/vtable.zig b/src/vtable.zig index cb1ac4b..4a79fe6 100644 --- a/src/vtable.zig +++ b/src/vtable.zig @@ -164,26 +164,26 @@ pub fn SimpleVTable(comptime Row: type) type { /// Generates CREATE TABLE statement for a virtual table schema. pub fn generateSchema(allocator: std.mem.Allocator, table_name: []const u8, columns: []const ColumnDef) ![]u8 { - var sql = std.ArrayList(u8).init(allocator); - errdefer sql.deinit(); + var sql: std.ArrayListUnmanaged(u8) = .{}; + errdefer sql.deinit(allocator); - try sql.appendSlice("CREATE TABLE "); - try sql.appendSlice(table_name); - try sql.appendSlice("("); + try sql.appendSlice(allocator, "CREATE TABLE "); + try sql.appendSlice(allocator, table_name); + try sql.appendSlice(allocator, "("); for (columns, 0..) |col, i| { - if (i > 0) try sql.appendSlice(", "); - try sql.appendSlice(col.name); - try sql.append(' '); - try sql.appendSlice(col.col_type); + if (i > 0) try sql.appendSlice(allocator, ", "); + try sql.appendSlice(allocator, col.name); + try sql.append(allocator, ' '); + try sql.appendSlice(allocator, col.col_type); if (col.constraints) |cons| { - try sql.append(' '); - try sql.appendSlice(cons); + try sql.append(allocator, ' '); + try sql.appendSlice(allocator, cons); } } - try sql.appendSlice(")"); - return sql.toOwnedSlice(); + try sql.appendSlice(allocator, ")"); + return sql.toOwnedSlice(allocator); } /// Helper to set a result value in a sqlite3_context. @@ -279,22 +279,22 @@ pub const EponymousModule = struct { /// Generates SQL to query an eponymous module. pub fn query(self: *const EponymousModule, allocator: std.mem.Allocator, args: []const []const u8) ![]u8 { - var sql = std.ArrayList(u8).init(allocator); - errdefer sql.deinit(); + var sql: std.ArrayListUnmanaged(u8) = .{}; + errdefer sql.deinit(allocator); - try sql.appendSlice("SELECT * FROM "); - try sql.appendSlice(self.name); - try sql.append('('); + try sql.appendSlice(allocator, "SELECT * FROM "); + try sql.appendSlice(allocator, self.name); + try sql.append(allocator, '('); for (args, 0..) |arg, i| { - if (i > 0) try sql.appendSlice(", "); - try sql.append('\''); - try sql.appendSlice(arg); - try sql.append('\''); + if (i > 0) try sql.appendSlice(allocator, ", "); + try sql.append(allocator, '\''); + try sql.appendSlice(allocator, arg); + try sql.append(allocator, '\''); } - try sql.append(')'); - return sql.toOwnedSlice(); + try sql.append(allocator, ')'); + return sql.toOwnedSlice(allocator); } };