mem: Export typed layout queries

e5b52896231dd90e9e41463cfd13d5b17f1143e2a1103d20a9dddd15b193a3b2
Expose sizeOf and alignOf through the standard memory module.
Exercise constant queries and callable layout values from a
consumer package.

Assisted-by: Codex:gpt-6
Change-Id: Ie56104faf632820a36522788b6a2464e93f2d3c3
Alexis Sellier committed ago 1 parent fc94b8ec
lib/std/mem.rad +6 -0
17 17
union MemoryError: Copy {
18 18
    /// Buffer is too small.
19 19
    BufferTooSmall,
20 20
}
21 21
22 +
/// Return the byte size of a value of T, including padding.
23 +
@intrinsic export constant fn sizeOf[T]() -> u32;
24 +
25 +
/// Return the required byte alignment of a value of T.
26 +
@intrinsic export constant fn alignOf[T]() -> u32;
27 +
22 28
/// Load an unsigned 16-bit value from `bytes` at `offset` in little-endian order.
23 29
/// The address can be unaligned. Trap unless all 2 bytes fit in the slice.
24 30
@intrinsic export fn load16Le(bytes: &[u8], offset: u32) -> u16;
25 31
26 32
/// Store an unsigned 16-bit value at `offset` in little-endian order.
test/tests/generic.layout.overflow.reject.rad +3 -3
1 -
//! rejects: 6:36: type layout exceeds the supported byte size
1 +
//! rejects: 6:41: type layout exceeds the supported byte size
2 2
3 -
@intrinsic export constant fn sizeOf[T]() -> u32;
3 +
use std::mem;
4 4
5 5
fn oversized[T]() -> u32 {
6 -
    return sizeOf[[T; 1073741824]]();
6 +
    return mem::sizeOf[[T; 1073741824]]();
7 7
}
8 8
9 9
@default fn main() -> u32 {
10 10
    return oversized[u64]();
11 11
}
test/tests/generic.layout.rad +8 -12
1 1
//! returns: 0
2 2
3 -
/// Return the size of a value of T.
4 -
@intrinsic export constant fn sizeOf[T]() -> u32;
5 -
6 -
/// Return the alignment of a value of T.
7 -
@intrinsic export constant fn alignOf[T]() -> u32;
3 +
use std::mem;
8 4
9 5
/// Check padding and stride.
10 6
record Padded {
11 7
    /// Byte before padding.
12 8
    first: u8,
13 9
    /// Aligned field.
14 10
    second: u32,
15 11
}
16 12
17 13
/// Byte size of four words.
18 -
constant WORDS_BYTES: u32 = sizeOf[[u32; 4]]();
14 +
constant WORDS_BYTES: u32 = mem::sizeOf[[u32; 4]]();
19 15
20 16
/// Callable layout query stored in constant data.
21 -
constant WORD_ALIGNMENT: fn() -> u32 = alignOf[u32];
17 +
constant WORD_ALIGNMENT: fn() -> u32 = mem::alignOf[u32];
22 18
23 19
/// Return the stride after substituting the argument.
24 20
fn stride[T]() -> u32 {
25 -
    return sizeOf[[T; 2]]();
21 +
    return mem::sizeOf[[T; 2]]();
26 22
}
27 23
28 24
/// Exercise direct queries, constants, and function values.
29 25
@default fn main() -> u32 {
30 26
    assert WORDS_BYTES == 16;
31 27
    assert WORD_ALIGNMENT() == 4;
32 -
    assert sizeOf[Padded]() == 8;
33 -
    assert alignOf[Padded]() == 4;
28 +
    assert mem::sizeOf[Padded]() == 8;
29 +
    assert mem::alignOf[Padded]() == 4;
34 30
    assert stride[Padded]() == 16;
35 -
    let getAlign: fn() -> u32 = alignOf[u64];
31 +
    let getAlign: fn() -> u32 = mem::alignOf[u64];
36 32
    assert getAlign() == 8;
37 -
    assert sizeOf[*opaque]() == 8;
33 +
    assert mem::sizeOf[*opaque]() == 8;
38 34
    return 0;
39 35
}