mirror of
https://github.com/odin-lang/Odin.git
synced 2026-06-05 18:24:06 +00:00
Convenience functions to reinterpret or cast one buffer to another type, or create a buffer of a specific type.
Example:
```odin
fmt.println("Convert []f16le (x2) to []f32 (x2).");
b := []u8{0, 60, 0, 60}; // == []f16{1.0, 1.0}
res, backing, had_to_allocate, err := bytes.buffer_convert_to_type(2, f32, f16le, b);
fmt.printf("res : %v\n", res); // [1.000, 1.000]
fmt.printf("backing : %v\n", backing); // &Buffer{buf = [0, 0, 128, 63, 0, 0, 128, 63], off = 0, last_read = Invalid}
fmt.printf("allocated: %v\n", had_to_allocate); // true
fmt.printf("err : %v\n", err); // false
if had_to_allocate { defer bytes.buffer_destroy(backing); }
fmt.println("\nConvert []f16le (x2) to []u16 (x2).");
res2: []u16;
res2, backing, had_to_allocate, err = bytes.buffer_convert_to_type(2, u16, f16le, b);
fmt.printf("res : %v\n", res2); // [15360, 15360]
fmt.printf("backing : %v\n", backing); // Buffer.buf points to `b` because it could be converted in-place.
fmt.printf("allocated: %v\n", had_to_allocate); // false
fmt.printf("err : %v\n", err); // false
if had_to_allocate { defer bytes.buffer_destroy(backing); }
fmt.println("\nConvert []f16le (x2) to []u16 (x2), force_convert=true.");
res2, backing, had_to_allocate, err = bytes.buffer_convert_to_type(2, u16, f16le, b, true);
fmt.printf("res : %v\n", res2); // [1, 1]
fmt.printf("backing : %v\n", backing); // Buffer.buf points to `b` because it could be converted in-place.
fmt.printf("allocated: %v\n", had_to_allocate); // false
fmt.printf("err : %v\n", err); // false
if had_to_allocate { defer bytes.buffer_destroy(backing); }
```