Application Context Implementation #4
| @ -11,9 +11,7 @@ const sys = @import("./sys.zig"); | ||||
| /// Entry point. | ||||
| /// | ||||
| pub fn main() anyerror!void { | ||||
|     var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||||
| 
 | ||||
|     return nosuspend await async sys.runGraphics(gpa.allocator(), anyerror, run); | ||||
|     return nosuspend await async sys.runGraphics(anyerror, run); | ||||
| } | ||||
| 
 | ||||
| test { | ||||
|  | ||||
							
								
								
									
										12
									
								
								src/sys.zig
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								src/sys.zig
									
									
									
									
									
								
							| @ -819,7 +819,7 @@ pub const RunError = error { | ||||
| /// Should an error from `run` occur, an `Error` is returned, otherwise a [RunError] is returned if | ||||
| /// the underlying runtime fails and is logged. | ||||
| /// | ||||
| pub fn runGraphics(allocator: std.mem.Allocator, comptime Error: anytype, | ||||
| pub fn runGraphics(comptime Error: anytype, | ||||
|     comptime run: GraphicsRunner(Error)) (RunError || Error)!void { | ||||
| 
 | ||||
|     if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) { | ||||
| @ -856,13 +856,15 @@ pub fn runGraphics(allocator: std.mem.Allocator, comptime Error: anytype, | ||||
|     defer ext.SDL_DestroyRenderer(renderer); | ||||
| 
 | ||||
|     var cwd_file_system = FileSystem{.native =.{.path_prefix = "./"}}; | ||||
|     var data_access = try (try cwd_file_system.joinedPath(&.{"./data.oar"})).open(.readonly); | ||||
| 
 | ||||
|     var data_archive_file_access = try (try cwd_file_system. | ||||
|         joinedPath(&.{"./data.oar"})).open(.readonly); | ||||
|     defer data_access.close(); | ||||
| 
 | ||||
|     defer data_archive_file_access.close(); | ||||
|     var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||||
| 
 | ||||
|     var app_context = AppContext.Implementation.init(allocator, data_archive_file_access) catch |err| { | ||||
|     defer _ = gpa.deinit(); | ||||
| 
 | ||||
|     var app_context = AppContext.Implementation.init(gpa.allocator(), data_access) catch |err| { | ||||
|         ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, switch (err) { | ||||
|             error.OutOfMemory => "Failed to allocate necessary memory", | ||||
|             error.OutOfMutexes => "Failed to create file-system work lock", | ||||
|  | ||||
| @ -32,6 +32,29 @@ pub fn Hashed(comptime Key: type, comptime Value: type, | ||||
|         /// | ||||
|         const Self = @This(); | ||||
| 
 | ||||
|         /// | ||||
|         /// Deinitializes `self`, preventing any further use. | ||||
|         /// | ||||
|         pub fn deinit(self: *Self) void { | ||||
|             self.allocator.free(self.buckets); | ||||
| 
 | ||||
|             self.buckets = &.{}; | ||||
|         } | ||||
| 
 | ||||
|         /// | ||||
|         /// Initializes a [Self] using `allocator` as the memory allocation strategy. | ||||
|         /// | ||||
|         /// Returns a new [Self] value or an [Allocator.Error] if initializing failed. | ||||
|         /// | ||||
|         pub fn init(allocator: Allocator) Allocator.Error!Self { | ||||
|             return Self{ | ||||
|                 .buckets = try allocator.alloc(Bucket, 4), | ||||
|                 .filled = 0, | ||||
|                 .allocator = allocator, | ||||
|                 .load_limit = 0.75, | ||||
|             }; | ||||
|         } | ||||
| 
 | ||||
|         /// | ||||
|         /// Searches for `key` to delete it, returning the deleted value or `null` if no matching | ||||
|         /// key was found. | ||||
| @ -62,15 +85,6 @@ pub fn Hashed(comptime Key: type, comptime Value: type, | ||||
|             return null; | ||||
|         } | ||||
| 
 | ||||
|         pub fn init(allocator: Allocator) Allocator.Error!Self { | ||||
|             return Self{ | ||||
|                 .buckets = try allocator.alloc(Bucket, 4), | ||||
|                 .filled = 0, | ||||
|                 .allocator = allocator, | ||||
|                 .load_limit = 0.75, | ||||
|             }; | ||||
|         } | ||||
| 
 | ||||
|         /// | ||||
|         /// Attempts to insert the value at `key` to be `value` in `self`, returning an | ||||
|         /// [InsertError] if it fails. | ||||
| @ -137,12 +151,17 @@ pub fn Hashed(comptime Key: type, comptime Value: type, | ||||
| } | ||||
| 
 | ||||
| /// | ||||
| /// | ||||
| /// [InsertError.KeyExists] occurs when an insertion was attempted on a table with a matching key | ||||
| /// already present. | ||||
| /// | ||||
| pub const InsertError = std.mem.Allocator.Error || error { | ||||
|     KeyExists, | ||||
| }; | ||||
| 
 | ||||
| /// | ||||
| /// Returns a context type for handling `Key` as a key in a table, associating hashing and equality | ||||
| /// behaviors to it. | ||||
| /// | ||||
| pub fn KeyContext(comptime Key: type) type { | ||||
|     return struct { | ||||
|         hash: fn (Key) usize, | ||||
| @ -150,10 +169,16 @@ pub fn KeyContext(comptime Key: type) type { | ||||
|     }; | ||||
| } | ||||
| 
 | ||||
| /// | ||||
| /// Tests if the contents of `this_string` lexically equals the contents of `that_string`. | ||||
| /// | ||||
| fn equalsString(this_string: []const u8, that_string: []const u8) bool { | ||||
|     return std.mem.eql(u8, this_string, that_string); | ||||
| } | ||||
| 
 | ||||
| /// | ||||
| /// Hashes `string` into a hash value of `usize`. | ||||
| /// | ||||
| fn hashString(string: []const u8) usize { | ||||
|     var hash = @as(usize, 5381); | ||||
| 
 | ||||
| @ -162,6 +187,9 @@ fn hashString(string: []const u8) usize { | ||||
|     return hash; | ||||
| } | ||||
| 
 | ||||
| /// | ||||
| /// A [KeyContext] for handling `[]const u8` types. | ||||
| /// | ||||
| pub const string_context = KeyContext([]const u8){ | ||||
|     .hash = hashString, | ||||
|     .equals = equalsString, | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user