diff options
| author | 2024-10-28 04:14:30 +0100 | |
|---|---|---|
| committer | 2024-11-16 11:15:49 +0100 | |
| commit | 3aa9ddb9b61534a8fe10d0fce206ed6a1209fde6 (patch) | |
| tree | 7befb8724a5c7b8df5e9bdcf9e51fd020ba84387 /sqlite.zig | |
| parent | Merge pull request #166 from vrischmann/update-sqlite (diff) | |
| download | zig-sqlite-3aa9ddb9b61534a8fe10d0fce206ed6a1209fde6.tar.gz zig-sqlite-3aa9ddb9b61534a8fe10d0fce206ed6a1209fde6.tar.xz zig-sqlite-3aa9ddb9b61534a8fe10d0fce206ed6a1209fde6.zip | |
support custom-binding unions
Diffstat (limited to 'sqlite.zig')
| -rw-r--r-- | sqlite.zig | 13 |
1 files changed, 11 insertions, 2 deletions
| @@ -1490,8 +1490,12 @@ pub fn Iterator(comptime Type: type) type { | |||
| 1490 | } | 1490 | } |
| 1491 | @compileError("enum column " ++ @typeName(FieldType) ++ " must have a BaseType of either string or int"); | 1491 | @compileError("enum column " ++ @typeName(FieldType) ++ " must have a BaseType of either string or int"); |
| 1492 | }, | 1492 | }, |
| 1493 | .@"struct" => |TI| { | 1493 | inline .@"struct", .@"union" => |TI| { |
| 1494 | if (TI.layout == .@"packed") return @bitCast(try self.readInt(TI.backing_integer.?, i)); | 1494 | if (TI.layout == .@"packed" and !@hasField(FieldType, "readField")) { |
| 1495 | const Backing = @Type(.{ .int = .{ .signedness = .unsigned, .bits = @bitSizeOf(FieldType) } }); | ||
| 1496 | return @bitCast(try self.readInt(Backing, i)); | ||
| 1497 | } | ||
| 1498 | |||
| 1495 | const inner_value = try self.readField(FieldType.BaseType, options, i); | 1499 | const inner_value = try self.readField(FieldType.BaseType, options, i); |
| 1496 | return try FieldType.readField(options.allocator, inner_value); | 1500 | return try FieldType.readField(options.allocator, inner_value); |
| 1497 | }, | 1501 | }, |
| @@ -1714,6 +1718,11 @@ pub const DynamicStatement = struct { | |||
| 1714 | try self.bindField(FieldType.BaseType, options, field_name, i, field_value); | 1718 | try self.bindField(FieldType.BaseType, options, field_name, i, field_value); |
| 1715 | }, | 1719 | }, |
| 1716 | .@"union" => |info| { | 1720 | .@"union" => |info| { |
| 1721 | if (info.layout == .@"packed") { | ||
| 1722 | const Backing = @Type(.{ .int = .{ .signedness = .unsigned, .bits = @bitSizeOf(FieldType) } }); | ||
| 1723 | try self.bindField(Backing, options, field_name, i, @as(Backing, @bitCast(field))); | ||
| 1724 | return; | ||
| 1725 | } | ||
| 1717 | if (info.tag_type) |UnionTagType| { | 1726 | if (info.tag_type) |UnionTagType| { |
| 1718 | inline for (info.fields) |u_field| { | 1727 | inline for (info.fields) |u_field| { |
| 1719 | // This wasn't entirely obvious when I saw code like this elsewhere, it works because of type coercion. | 1728 | // This wasn't entirely obvious when I saw code like this elsewhere, it works because of type coercion. |