acton_core/traits/acton_message.rs
1/*
2 * Copyright (c) 2024. Govcraft
3 *
4 * Licensed under either of
5 * * Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8 * * MIT license: http://opensource.org/licenses/MIT
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 applicable License for the specific language governing permissions and
14 * limitations under that License.
15 */
16use std::any::Any;
17use std::fmt::Debug;
18
19use dyn_clone::DynClone;
20
21/// Trait for Acton messages, providing methods for type erasure.
22pub trait ActonMessage: DynClone + Any + Send + Sync + Debug {
23 /// Returns a reference to the message as `Any`.
24 fn as_any(&self) -> &dyn Any;
25
26 /// Returns a mutable reference to the message as `Any`.
27 fn as_any_mut(&mut self) -> &mut dyn Any;
28}
29
30impl<T> ActonMessage for T
31where
32 T: Any + Send + Sync + Debug + DynClone + 'static,
33{
34 fn as_any(&self) -> &dyn Any {
35 self
36 }
37
38 fn as_any_mut(&mut self) -> &mut dyn Any {
39 self
40 }
41}