gear_core/message/
handle.rs1use crate::{
20 buffer::Payload,
21 ids::{ActorId, MessageId},
22 message::{
23 Dispatch, DispatchKind, GasLimit, Message, Packet, StoredDispatch, StoredMessage, Value,
24 },
25};
26
27#[derive(Clone, Debug, PartialEq, Eq)]
30pub struct HandleMessage {
31 id: MessageId,
33 destination: ActorId,
35 payload: Payload,
37 gas_limit: Option<GasLimit>,
39 value: Value,
41}
42
43impl HandleMessage {
44 pub fn from_packet(id: MessageId, packet: HandlePacket) -> Self {
46 Self {
47 id,
48 destination: packet.destination,
49 payload: packet.payload,
50 gas_limit: packet.gas_limit,
51 value: packet.value,
52 }
53 }
54
55 pub fn into_message(self, source: ActorId) -> Message {
57 Message::new(
58 self.id,
59 source,
60 self.destination,
61 self.payload,
62 self.gas_limit,
63 self.value,
64 None,
65 )
66 }
67
68 pub fn into_stored(self, source: ActorId) -> StoredMessage {
70 self.into_message(source).into()
71 }
72
73 pub fn into_dispatch(self, source: ActorId) -> Dispatch {
75 Dispatch::new(DispatchKind::Handle, self.into_message(source))
76 }
77
78 pub fn into_stored_dispatch(self, source: ActorId) -> StoredDispatch {
80 self.into_dispatch(source).into()
81 }
82
83 pub fn id(&self) -> MessageId {
85 self.id
86 }
87
88 pub fn destination(&self) -> ActorId {
90 self.destination
91 }
92
93 pub fn payload_bytes(&self) -> &[u8] {
95 &self.payload
96 }
97
98 pub fn gas_limit(&self) -> Option<GasLimit> {
100 self.gas_limit
101 }
102
103 pub fn value(&self) -> Value {
105 self.value
106 }
107}
108
109#[derive(Clone, Debug, PartialEq, Eq)]
113#[cfg_attr(any(feature = "mock", test), derive(Default))]
114pub struct HandlePacket {
115 destination: ActorId,
117 payload: Payload,
119 gas_limit: Option<GasLimit>,
121 value: Value,
123}
124
125impl HandlePacket {
126 pub fn new(destination: ActorId, payload: Payload, value: Value) -> Self {
128 Self {
129 destination,
130 payload,
131 gas_limit: None,
132 value,
133 }
134 }
135
136 pub fn new_with_gas(
138 destination: ActorId,
139 payload: Payload,
140 gas_limit: GasLimit,
141 value: Value,
142 ) -> Self {
143 Self {
144 destination,
145 payload,
146 gas_limit: Some(gas_limit),
147 value,
148 }
149 }
150
151 pub fn maybe_with_gas(
153 destination: ActorId,
154 payload: Payload,
155 gas_limit: Option<GasLimit>,
156 value: Value,
157 ) -> Self {
158 match gas_limit {
159 None => Self::new(destination, payload, value),
160 Some(gas_limit) => Self::new_with_gas(destination, payload, gas_limit, value),
161 }
162 }
163
164 pub(super) fn try_prepend(&mut self, mut data: Payload) -> Result<(), Payload> {
166 if data.try_extend_from_slice(self.payload_bytes()).is_err() {
167 Err(data)
168 } else {
169 self.payload = data;
170 Ok(())
171 }
172 }
173
174 pub fn destination(&self) -> ActorId {
176 self.destination
177 }
178}
179
180impl Packet for HandlePacket {
181 fn payload_bytes(&self) -> &[u8] {
182 &self.payload
183 }
184
185 fn payload_len(&self) -> u32 {
186 self.payload.len_u32()
187 }
188
189 fn gas_limit(&self) -> Option<GasLimit> {
190 self.gas_limit
191 }
192
193 fn value(&self) -> Value {
194 self.value
195 }
196
197 fn kind() -> DispatchKind {
198 DispatchKind::Handle
199 }
200}