Hash maps now copy keys if keys are copyable.

This commit is contained in:
Christoffer Lerno
2023-08-10 21:14:24 +02:00
parent 356b6bb1b7
commit 3e765a3f3e
8 changed files with 80 additions and 18 deletions

View File

@@ -177,19 +177,14 @@ macro bool equals(a, b, isz len = -1, usz $align = 0)
return true;
}
macro @clone(&value) @builtin
macro @clone(&value, Allocator *using = mem::heap()) @builtin
{
$typeof(value)* x = malloc($typeof(value));
$typeof(value)* x = malloc($typeof(value), .using = using);
*x = value;
return x;
}
macro @tclone(&value) @builtin
{
$typeof(value)* x = talloc($typeof(value));
*x = value;
return x;
}
macro @tclone(&value) @builtin => @clone(value, mem::temp());
macro type_alloc_must_be_aligned($Type)
{

View File

@@ -346,6 +346,12 @@ fn String String.copy(s, Allocator* using = mem::heap())
str[len] = 0;
return (String)str[:len];
}
fn void String.free(&s, Allocator* using = mem::heap())
{
if (!s.len) return;
mem::free(s.ptr, .using = using);
*s = "";
}
fn String String.tcopy(s) => s.copy(mem::temp()) @inline;

View File

@@ -229,6 +229,14 @@ macro bool is_equatable_type($Type)
$endif
}
/**
* Checks if a type implements the copy protocol.
**/
macro bool implements_copy($Type)
{
return $checks(Allocator *a, $Type x, $Type y = x.copy(a), $Type z = x.copy(), z.free(a), z.free());
}
macro bool is_equatable_value(value)
{
return is_equatable_type($typeof(value));