it_memory_traits/lib.rs
1/*
2 * Copyright 2022 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17mod errors;
18
19pub use errors::MemoryAccessError;
20
21pub trait Store: Send {
22 type ActualStore<'c>: Send;
23}
24
25pub trait MemoryReadable<Store: self::Store> {
26 /// This function will panic if the `offset` is out of bounds.
27 /// It is caller's responsibility to check if the offset is in bounds
28 /// using `MemoryView::check_bounds` function
29 fn read_byte(&self, store: &mut <Store as self::Store>::ActualStore<'_>, offset: u32) -> u8;
30
31 /// This function will panic if `[offset..offset + COUNT]` is out of bounds.
32 /// It is caller's responsibility to check if the offset is in bounds
33 /// using `MemoryView::check_bounds` function.
34 fn read_array<const COUNT: usize>(
35 &self,
36 store: &mut <Store as self::Store>::ActualStore<'_>,
37 offset: u32,
38 ) -> [u8; COUNT];
39
40 /// This function will panic if `[offset..offset + size]` is out of bounds.
41 /// It is caller's responsibility to check if the offset is in bounds
42 /// using `MemoryView::check_bounds` function.
43 fn read_vec(
44 &self,
45 store: &mut <Store as self::Store>::ActualStore<'_>,
46 offset: u32,
47 size: u32,
48 ) -> Vec<u8>;
49}
50
51pub trait MemoryWritable<Store: self::Store> {
52 /// This function will panic if `offset` is out of bounds.
53 /// It is caller's responsibility to check if the offset is in bounds
54 /// using `MemoryView::check_bounds` function.
55 fn write_byte(
56 &self,
57 store: &mut <Store as self::Store>::ActualStore<'_>,
58 offset: u32,
59 value: u8,
60 );
61
62 /// This function will panic if `[offset..offset + bytes.len()]`.is out of bounds.
63 /// It is caller's responsibility to check if the offset is in bounds
64 /// using `MemoryView::check_bounds` function.
65 fn write_bytes(
66 &self,
67 store: &mut <Store as self::Store>::ActualStore<'_>,
68 offset: u32,
69 bytes: &[u8],
70 );
71}
72
73pub trait MemoryView<Store: self::Store>:
74 Send + MemoryWritable<Store> + MemoryReadable<Store>
75{
76 /// For optimization purposes, user must check bounds first, then try read-write to memory
77 /// `MemoryWritable` and `MemoryReadable` functions will panic in case of out of bounds access`
78 fn check_bounds(
79 &self,
80 store: &mut <Store as self::Store>::ActualStore<'_>,
81 offset: u32,
82 size: u32,
83 ) -> Result<(), MemoryAccessError>;
84}
85
86pub trait Memory<View, Store: self::Store>: Send
87where
88 View: MemoryView<Store>,
89{
90 fn view(&self) -> View;
91}