|
1 |
| -use crate::{context::{PublicContext, UnconstrainedContext}, state_vars::storage::Storage}; |
| 1 | +use crate::{ |
| 2 | + context::{PrivateContext, PublicContext, UnconstrainedContext}, |
| 3 | + state_vars::storage::Storage, |
| 4 | +}; |
2 | 5 | use dep::protocol_types::{
|
3 | 6 | constants::INITIALIZATION_SLOT_SEPARATOR,
|
4 | 7 | traits::{Deserialize, Serialize},
|
5 | 8 | };
|
6 | 9 |
|
7 |
| -// Just like SharedImmutable but without the ability to read from private functions. |
| 10 | +/// Stores an immutable value in public state which can be read from public, private and unconstrained execution |
| 11 | +/// contexts. |
8 | 12 | // docs:start:public_immutable_struct
|
9 | 13 | pub struct PublicImmutable<T, Context> {
|
10 | 14 | context: Context,
|
|
57 | 61 |
|
58 | 62 | impl<T, let T_SERIALIZED_LEN: u32> PublicImmutable<T, UnconstrainedContext>
|
59 | 63 | where
|
60 |
| - T: Deserialize<T_SERIALIZED_LEN>, |
| 64 | + T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN>, |
61 | 65 | {
|
62 | 66 | pub unconstrained fn read(self) -> T {
|
63 | 67 | self.context.storage_read(self.storage_slot)
|
64 | 68 | }
|
65 | 69 | }
|
| 70 | + |
| 71 | +impl<T, let T_SERIALIZED_LEN: u32> PublicImmutable<T, &mut PrivateContext> |
| 72 | +where |
| 73 | + T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN>, |
| 74 | +{ |
| 75 | + pub fn read(self) -> T { |
| 76 | + let header = self.context.get_header(); |
| 77 | + let mut fields = [0; T_SERIALIZED_LEN]; |
| 78 | + |
| 79 | + for i in 0..fields.len() { |
| 80 | + fields[i] = header.public_storage_historical_read( |
| 81 | + self.storage_slot + i as Field, |
| 82 | + (*self.context).this_address(), |
| 83 | + ); |
| 84 | + } |
| 85 | + T::deserialize(fields) |
| 86 | + } |
| 87 | +} |
0 commit comments