String handling in comptime functions

Comptime-Mutable Memory Changes is a very good and thorough summary of what is and isn’t allowed to become runtime-known, but the relevant part is this:

So the easiest fix is probably something like this:

 pub fn getMetaname(comptime T: type) [:0]const u8 {
     const fullTypeName = @typeName(T);
     var it = std.mem.splitBackwardsAny(u8, fullTypeName, ".");
     const last = it.first();
     var buffer: [128]u8 = undefined;
     @memcpy(buffer[0..last.len], last);
     buffer[last.len] = 0;
-    return buffer[0..last.len :0];
+    const copy = buffer[0..last.len :0].*;
+    return ©
 }

 pub const metaname = getMetaname(LdtkScene);
5 Likes