mirror of
https://github.com/odin-lang/Odin.git
synced 2026-04-26 00:03:54 +00:00
cbor support for fixed capacity dynamic arrays
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user