gear_core/message/
handle.rs1use crate::{
5 buffer::Payload,
6 ids::{ActorId, MessageId},
7 message::{
8 Dispatch, DispatchKind, GasLimit, Message, Packet, StoredDispatch, StoredMessage, Value,
9 },
10};
11
12#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct HandleMessage {
16 id: MessageId,
18 destination: ActorId,
20 payload: Payload,
22 gas_limit: Option<GasLimit>,
24 value: Value,
26}
27
28impl HandleMessage {
29 pub fn from_packet(id: MessageId, packet: HandlePacket) -> Self {
31 Self {
32 id,
33 destination: packet.destination,
34 payload: packet.payload,
35 gas_limit: packet.gas_limit,
36 value: packet.value,
37 }
38 }
39
40 pub fn into_message(self, source: ActorId) -> Message {
42 Message::new(
43 self.id,
44 source,
45 self.destination,
46 self.payload,
47 self.gas_limit,
48 self.value,
49 None,
50 )
51 }
52
53 pub fn into_stored(self, source: ActorId) -> StoredMessage {
55 self.into_message(source).into()
56 }
57
58 pub fn into_dispatch(self, source: ActorId) -> Dispatch {
60 Dispatch::new(DispatchKind::Handle, self.into_message(source))
61 }
62
63 pub fn into_stored_dispatch(self, source: ActorId) -> StoredDispatch {
65 self.into_dispatch(source).into()
66 }
67
68 pub fn id(&self) -> MessageId {
70 self.id
71 }
72
73 pub fn destination(&self) -> ActorId {
75 self.destination
76 }
77
78 pub fn payload_bytes(&self) -> &[u8] {
80 &self.payload
81 }
82
83 pub fn gas_limit(&self) -> Option<GasLimit> {
85 self.gas_limit
86 }
87
88 pub fn value(&self) -> Value {
90 self.value
91 }
92}
93
94#[derive(Clone, Debug, PartialEq, Eq)]
98#[cfg_attr(any(feature = "mock", test), derive(Default))]
99pub struct HandlePacket {
100 destination: ActorId,
102 payload: Payload,
104 gas_limit: Option<GasLimit>,
106 value: Value,
108}
109
110impl HandlePacket {
111 pub fn new(destination: ActorId, payload: Payload, value: Value) -> Self {
113 Self {
114 destination,
115 payload,
116 gas_limit: None,
117 value,
118 }
119 }
120
121 pub fn new_with_gas(
123 destination: ActorId,
124 payload: Payload,
125 gas_limit: GasLimit,
126 value: Value,
127 ) -> Self {
128 Self {
129 destination,
130 payload,
131 gas_limit: Some(gas_limit),
132 value,
133 }
134 }
135
136 pub fn maybe_with_gas(
138 destination: ActorId,
139 payload: Payload,
140 gas_limit: Option<GasLimit>,
141 value: Value,
142 ) -> Self {
143 match gas_limit {
144 None => Self::new(destination, payload, value),
145 Some(gas_limit) => Self::new_with_gas(destination, payload, gas_limit, value),
146 }
147 }
148
149 pub(super) fn try_prepend(&mut self, mut data: Payload) -> Result<(), Payload> {
151 if data.try_extend_from_slice(self.payload_bytes()).is_err() {
152 Err(data)
153 } else {
154 self.payload = data;
155 Ok(())
156 }
157 }
158
159 pub fn destination(&self) -> ActorId {
161 self.destination
162 }
163}
164
165impl Packet for HandlePacket {
166 fn payload_bytes(&self) -> &[u8] {
167 &self.payload
168 }
169
170 fn payload_len(&self) -> u32 {
171 self.payload.len_u32()
172 }
173
174 fn gas_limit(&self) -> Option<GasLimit> {
175 self.gas_limit
176 }
177
178 fn value(&self) -> Value {
179 self.value
180 }
181
182 fn kind() -> DispatchKind {
183 DispatchKind::Handle
184 }
185}