mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-22 04:34:44 +00:00
use enum approach instead of vtable
This commit is contained in:
33
src/network/cache/Cache.zig
vendored
33
src/network/cache/Cache.zig
vendored
@@ -17,37 +17,26 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const FsCache = @import("FsCache.zig");
|
||||||
|
|
||||||
/// A browser-wide cache for resources across the network.
|
/// A browser-wide cache for resources across the network.
|
||||||
/// This mostly conforms to RFC9111 with regards to caching behavior.
|
/// This mostly conforms to RFC9111 with regards to caching behavior.
|
||||||
pub const Cache = @This();
|
pub const Cache = @This();
|
||||||
|
|
||||||
ptr: *anyopaque,
|
kind: union(enum) {
|
||||||
vtable: *const VTable,
|
fs: FsCache,
|
||||||
|
|
||||||
const VTable = struct {
|
|
||||||
get: *const fn (ptr: *anyopaque, allocator: std.mem.Allocator, key: []const u8) ?CachedResponse,
|
|
||||||
put: *const fn (ptr: *anyopaque, key: []const u8, metadata: CachedMetadata, body: []const u8) anyerror!void,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn init(ptr: anytype) Cache {
|
|
||||||
const T = @TypeOf(ptr.*);
|
|
||||||
|
|
||||||
return .{
|
|
||||||
.ptr = ptr,
|
|
||||||
.vtable = &.{
|
|
||||||
.get = T.get,
|
|
||||||
.put = T.put,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
pub fn get(self: *Cache, allocator: std.mem.Allocator, key: []const u8) ?CachedResponse {
|
||||||
|
return switch (self.kind) {
|
||||||
|
inline else => |*c| c.get(allocator, key),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(self: Cache, allocator: std.mem.Allocator, key: []const u8) ?CachedResponse {
|
pub fn put(self: *Cache, key: []const u8, metadata: CachedMetadata, body: []const u8) !void {
|
||||||
return self.vtable.get(self.ptr, allocator, key);
|
return switch (self.kind) {
|
||||||
}
|
inline else => |*c| c.put(key, metadata, body),
|
||||||
|
};
|
||||||
pub fn put(self: Cache, key: []const u8, metadata: CachedMetadata, body: []const u8) !void {
|
|
||||||
return self.vtable.put(self.ptr, key, metadata, body);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const CachedMetadata = struct {
|
pub const CachedMetadata = struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user