Skip to main content

apocalypse/demon/
location.rs

1use std::marker::PhantomData;
2use std::hash::{Hash, Hasher};
3
4/// Demon's location (to be able to send messages).
5///
6/// The Demon's Location will be handed in by the Portal's once the spawn is complete. You can clone the address in order to share it across threads. This structure's only purpose is to help the portal know the types of the Input and Output that the Demon accespts.
7pub struct Location<E: ?Sized> {
8    /// Actual id of the demon
9    pub(crate) address: usize,
10    /// Phantom data just to make Rust happy
11    pub(crate) phantom: PhantomData<E>
12}
13
14impl<A> AsRef<Location<A>> for Location<A> {
15    fn as_ref(&self) -> &Location<A> {
16        &self
17    }
18}
19
20impl<E> Clone for Location<E> {
21    fn clone(&self) -> Location<E> {
22        Location {
23            address: self.address,
24            phantom: PhantomData
25        }
26    }
27}
28
29impl<E> Hash for Location<E> {
30    fn hash<H: Hasher>(&self, state: &mut H) {
31        self.address.hash(state);
32    }
33}
34
35impl<E> PartialEq for Location<E> {
36    fn eq(&self, other: &Self) -> bool {
37        self.address == other.address
38    }
39}
40
41impl<E> Eq for Location<E>{}
42
43impl<E> std::fmt::Display for Location<E> {
44    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
45        write!(formatter, "d-{}", self.address)
46    }
47}