linera_witty/primitive_types/
array.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::borrow::Cow;

use crate::{
    GuestPointer, HList, InstanceWithMemory, Layout, Memory, Runtime, RuntimeError, RuntimeMemory,
    WitLoad, WitStore, WitType,
};

impl WitType for [u8; 20] {
    const SIZE: u32 = <(u64, u64, u64) as WitType>::SIZE;
    type Layout = <(u64, u64, u64) as WitType>::Layout;
    type Dependencies = HList![];

    fn wit_type_name() -> Cow<'static, str> {
        "array20".into()
    }

    fn wit_type_declaration() -> Cow<'static, str> {
        concat!(
            "    record array20 {\n",
            "        part1: u64,\n",
            "        part2: u64,\n",
            "        part3: u64,\n",
            "    }\n",
        )
        .into()
    }
}

impl WitLoad for [u8; 20] {
    fn load<Instance>(
        memory: &Memory<'_, Instance>,
        location: GuestPointer,
    ) -> Result<Self, RuntimeError>
    where
        Instance: InstanceWithMemory,
        <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>,
    {
        let (part1, part2, part3): (u64, u64, u64) = WitLoad::load(memory, location)?;
        let mut dest = [0u8; 20];
        dest[0..8].copy_from_slice(&part1.to_be_bytes());
        dest[8..16].copy_from_slice(&part2.to_be_bytes());
        dest[16..20].copy_from_slice(&part3.to_be_bytes());
        Ok(dest)
    }

    fn lift_from<Instance>(
        flat_layout: <Self::Layout as crate::Layout>::Flat,
        memory: &Memory<'_, Instance>,
    ) -> Result<Self, RuntimeError>
    where
        Instance: InstanceWithMemory,
        <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>,
    {
        let (part1, part2, part3): (u64, u64, u64) = WitLoad::lift_from(flat_layout, memory)?;
        let mut dest = [0u8; 20];
        dest[0..8].copy_from_slice(&part1.to_be_bytes());
        dest[8..16].copy_from_slice(&part2.to_be_bytes());
        dest[16..20].copy_from_slice(&part3.to_be_bytes());
        Ok(dest)
    }
}

impl WitStore for [u8; 20] {
    fn store<Instance>(
        &self,
        memory: &mut Memory<'_, Instance>,
        location: GuestPointer,
    ) -> Result<(), RuntimeError>
    where
        Instance: InstanceWithMemory,
        <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>,
    {
        let part1 = u64::from_be_bytes(self[0..8].try_into().unwrap());
        let part2 = u64::from_be_bytes(self[8..16].try_into().unwrap());
        let part3 = u64::from_be_bytes(self[16..20].try_into().unwrap());
        (part1, part2, part3).store(memory, location)
    }

    fn lower<Instance>(
        &self,
        memory: &mut Memory<'_, Instance>,
    ) -> Result<<Self::Layout as Layout>::Flat, RuntimeError>
    where
        Instance: InstanceWithMemory,
        <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>,
    {
        let part1 = u64::from_be_bytes(self[0..8].try_into().unwrap());
        let part2 = u64::from_be_bytes(self[8..16].try_into().unwrap());
        let part3 = u64::from_be_bytes(self[16..20].try_into().unwrap());
        (part1, part2, part3).lower(memory)
    }
}