prelude

Macro prelude 

Source
macro_rules! prelude {
    () => { ... };
    (U=) => { ... };
    (U=$vis:vis use $($mod_path:path),+; $($rest:tt)*) => { ... };
    (U=$vis:vis use $root:ident::{$($mod_path:path),+}; $($rest:tt)*) => { ... };
    ($(#[$attr:meta])* $mod_vis:vis mod $use_vis:vis use $($module:ident),+; $($rest:tt)*) => { ... };
    ($(#[$attr:meta])* $use_vis:vis use $($mod_path:path),+; $($rest:tt)*) => { ... };
    ($(#[$attr:meta])* $use_vis:vis use $root:ident::{$($mod_path:path),+}; $($rest:tt)*) => { ... };
}
Expand description

Generates a prelude module with some uses.

§Syntax

At the start of each pattern, you can add attributes that will then be added to the prelude module. You can also write documentation!

There’s three different patterns the macro matches against. “use” means “use in the mod prelude”.

  • Define modules in the file and use the contents (::*). This will use super::mod_name to get the modules in the file.
  • Use the contents of modules that are already in scope.
  • Use the contents of modules that are in a root module. For example, crate::{mod1, mod2} instead of crate::mod1, crate::mod2.

After each pattern, you can type a semicolon (;) and make another one!

§Examples

This showcases each of the three patterns:

mod internal1 {
    pub(crate) fn int1() {}
}

mod internal2 {
    pub(crate) fn int2() {}
}

dry_mods::prelude! {
    // `foo` and `bar` will be public modules and their contents will also be public.
    /// Re-exports some commonly used modules.
    pub mod pub use foo, bar;
    // `internal1` and `internal2` will only be visible within the crate.
    // We don't use `mod` here, because they are already defined.
    pub(crate) use crate::{internal1, internal2};
}

// Generates:

pub mod foo;
pub mod bar;
#[doc = " Re-exports some commonly used modules."]
pub mod prelude {
    pub use super::foo::*;
    pub use super::bar::*;
    pub(crate) use crate::{internal1::*, internal2::*};
}