Skip to content

qsrmc/comptime_allocator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

comptime_allocator

Based on bnl1's work and expanded upon, this library was made to simplify the use of std lib containers in comptime contexts.

License

While bnl1's original work was made under MIT License, comptime_allocator is licensed under the European Union Public License 1.2 or later.

Example

Directly from the tests:

const allocator = @import("comptime_allocator");

test "comptime ArrayList" {
    const hello_world = comptime blk: {
        var array_list = std.ArrayList(u8).init(allocator);

        try array_list.appendSlice("Helloo");
        try array_list.appendSlice("wworld!");

        try array_list.replaceRange(5, 2, ", ");

        // You cannot use a pointer allocated at compile time during runtime,
        // but you can copy its content to a static array
        var out_buf: [array_list.items.len]u8 = undefined;
        @memcpy(&out_buf, array_list.items);
        break :blk out_buf;
    };
    try std.testing.expectEqualSlices(u8, "Hello, world!", &hello_world);
}

FAQ

Why use that allocator?

It makes using containers from the standard library really easy, therefore making most dynamic function calls callable in a comptime context.

Why is this library under a copyleft license?

I hate proprietary software, just thinking that my code could be used in some makes me puke.

But I can't use it for my own projects >:(

Get over it. Use a copyleft license. Make the world a better place.