cbor support for fixed capacity dynamic arrays

This commit is contained in:
gingerBill
2026-03-12 13:01:29 +00:00
parent bc636e4b36
commit e485d82c9d
2 changed files with 58 additions and 0 deletions

View File

@@ -285,6 +285,32 @@ _marshal_into_encoder :: proc(e: Encoder, v: any, ti: ^runtime.Type_Info) -> (er
}
return
case runtime.Type_Info_Fixed_Capacity_Dynamic_Array:
if info.elem.id == byte {
raw := (^[dynamic]byte)(v.data)
return err_conv(_encode_bytes(e, raw[:]))
}
array_len := (^int)(uintptr(v.data) + info.len_offset)^
array_data := uintptr(v.data)
err_conv(_encode_u64(e, u64(array_len), .Array)) or_return
if impl, ok := _tag_implementations_type[info.elem.id]; ok {
for i in 0..<array_len {
data := array_data + uintptr(i*info.elem_size)
impl->marshal(e, any{rawptr(data), info.elem.id}) or_return
}
return
}
elem_ti := runtime.type_info_core(type_info_of(info.elem.id))
for i in 0..<array_len {
data := array_data + uintptr(i*info.elem_size)
_marshal_into_encoder(e, any{rawptr(data), info.elem.id}, elem_ti) or_return
}
return
case runtime.Type_Info_Slice:
if info.elem.id == byte {
raw := (^[]byte)(v.data)

View File

@@ -389,6 +389,23 @@ _unmarshal_bytes :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
n := copy(slice, bytes)
assert(n == len(bytes))
return
case reflect.Type_Info_Fixed_Capacity_Dynamic_Array:
elem_base := reflect.type_info_base(t.elem)
if elem_base.id != byte { return _unsupported(v, hdr) }
bytes := err_conv(_decode_bytes(d, add, allocator=context.temp_allocator)) or_return
defer delete(bytes, context.temp_allocator)
if len(bytes) > t.capacity { return _unsupported(v, hdr) }
// Copy into array type, delete original.
slice := ([^]byte)(v.data)[:len(bytes)]
n := copy(slice, bytes)
assert(n == len(bytes))
(^int)(uintptr(v.data) + t.len_offset)^ = n
return
}
return _unsupported(v, hdr)
@@ -553,6 +570,21 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
if out_of_space { return _unsupported(v, hdr) }
return
case reflect.Type_Info_Fixed_Capacity_Dynamic_Array:
length, _ := err_conv(_decode_len_container(d, add)) or_return
if length > t.capacity {
return _unsupported(v, hdr)
}
da := mem.Raw_Dynamic_Array{rawptr(v.data), 0, length, allocator }
out_of_space := assign_array(d, &da, t.elem, length, growable=false) or_return
if out_of_space { return _unsupported(v, hdr) }
(^int)(uintptr(v.data) + t.len_offset)^ = length
return
case reflect.Type_Info_Complex:
length, _ := err_conv(_decode_len_container(d, add)) or_return
if length > 2 {