rusty_fork/
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    struct _RustyForkId;
21    $crate::RustyForkId::of(::std::any::TypeId::of::<_RustyForkId>())
22} } }
23
24/// The type of the value produced by
25/// [`rusty_fork_id!`](macro.rusty_fork_id.html).
26#[derive(Clone, Hash, PartialEq, Debug)]
27pub struct RustyForkId(::std::any::TypeId);
28impl RustyForkId {
29    #[allow(missing_docs)]
30    #[doc(hidden)]
31    pub fn of(id: ::std::any::TypeId) -> Self {
32        RustyForkId(id)
33    }
34}
35
36#[cfg(test)]
37mod test {
38    #[test]
39    fn ids_are_actually_distinct() {
40        assert_ne!(rusty_fork_id!(), rusty_fork_id!());
41    }
42}