implements structs in bytevectors. Currently handles uints/ints/chars/bools/float32/float64 with structs/unions/arrays.
A complex type to show nesting works etc...
> (import (struct struct))
> (define-type
(struct a
(array b 3
(struct c
(u8 f)
(array inner 2
(union z
(u32 a)
(u16 b)))))
(array d 1 (u16 e))))Can get the size with this macro
> (type-sizeof a)
29A helper to make bytevectors with the appropriate size
> (define v (a-make 2))
> v
#vu8(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
getters and setters...
> (a-set! (b 2 f) v 5)
> (a-get (b 2 f) v)
5
> ;; optional index parameter (default is 0) to select from the multiple elements in the one bytevector
;; uses TOTAL byte index for now, to make looping slightly faster by avoiding uneeded multiplications
(a-get (fx* (type-sizeof a) 1) (b 2 f) v)
0nested arrays and unions of course work here
(a-set! (b 1 inner 0 a) v (fx1- (expt 2 32)))
> (a-get (b 1 inner 0 a) v)
4294967295
> (a-set! (b 1 inner 0 b) v 0)
> (a-get (b 1 inner 0 a) v)
4294901760you can also refer to other types
> (define-type (struct circle (u32 radius)))
> (define-type (struct triangle (u32 height) (u32 base)))
> (define-type
(struct shape
(u8 type)
(union shapes circle triangle)))
> (define v (shape-make 1))
> (shape-set! (shapes triangle height) v (fx1- (expt 2 32)))