Skip to main content

options/
configure.rs

1use cfg_if::cfg_if;
2
3/// Configures options.
4///
5/// # Remarks
6///
7/// These are all run first.
8#[cfg_attr(feature = "async", maybe_impl::traits(Send, Sync))]
9pub trait Configure<T> {
10    /// Runs the configuration.
11    ///
12    /// # Arguments
13    ///
14    /// * `name` - The optional name of the options to configure
15    /// * `options` - The options to configure
16    fn run(&self, name: &str, options: &mut T);
17}
18
19/// Configures options.
20///
21/// # Remarks
22///
23/// These are all run last.
24#[cfg_attr(feature = "async", maybe_impl::traits(Send, Sync))]
25pub trait PostConfigure<T> {
26    /// Runs the configuration.
27    ///
28    /// # Arguments
29    ///
30    /// * `name` - The optional name of the options to configure
31    /// * `options` - The options to configure
32    fn run(&self, name: &str, options: &mut T);
33}
34
35macro_rules! configure {
36    ($($bounds:tt)+) => {
37        impl<F: Fn(&str, &mut T) + $($bounds)+, T> Configure<T> for F {
38            fn run(&self, name: &str, options: &mut T) {
39                (self)(name, options)
40            }
41        }
42
43        impl<F: Fn(&str, &mut T) + $($bounds)+, T> PostConfigure<T> for F {
44            fn run(&self, name: &str, options: &mut T) {
45                (self)(name, options)
46            }
47        }
48    }
49}
50
51cfg_if! {
52    if #[cfg(feature = "async")] {
53        configure!(Sized + Send + Sync);
54    } else {
55        configure!(Sized);
56    }
57}