Skip to main content

rusty_forkfork/
sugar.rs

1//-
2// Copyright 2018 Jason Lingle
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10/// Produce a hashable identifier unique to the particular macro invocation
11/// which is stable across processes of the same executable.
12///
13/// This is usually the best thing to pass for the `fork_id` argument of
14/// [`fork`](fn.fork.html).
15///
16/// The type of the expression this macro expands to is
17/// [`RustyForkId`](struct.RustyForkId.html).
18#[macro_export]
19macro_rules! rusty_fork_id {
20    () => {{
21        struct _RustyForkId;
22        $crate::RustyForkId::of(::std::any::TypeId::of::<_RustyForkId>())
23    }};
24}
25
26/// The type of the value produced by
27/// [`rusty_fork_id!`](macro.rusty_fork_id.html).
28#[derive(Clone, Hash, PartialEq, Debug)]
29pub struct RustyForkId(::std::any::TypeId);
30impl RustyForkId {
31    #[allow(missing_docs)]
32    #[doc(hidden)]
33    pub fn of(id: ::std::any::TypeId) -> Self {
34        RustyForkId(id)
35    }
36}
37
38#[cfg(test)]
39mod test {
40    #[test]
41    fn ids_are_actually_distinct() {
42        assert_ne!(rusty_fork_id!(), rusty_fork_id!());
43    }
44}