Skip to content

Commit 8832556

Browse files
committed
Documented the prelude module
1 parent edc1f35 commit 8832556

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/prelude.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
//! Convenience re-export of multiple traits.
2+
//!
3+
//! This allows a HAL user to conveniently import this module and have all the
4+
//! helper traits already imported.
5+
//! Otherwise the use of peripherals would require the import of the
6+
//! corresponding module and the import of the trait, which connects this HAL
7+
//! to the autogenerated svd2rust API in [crate::stm32].
8+
//!
9+
//! # Example
10+
//!
11+
//! Consider the following code.
12+
//!
13+
//! ```
14+
//! #[entry]
15+
//! fn main() -> ! {
16+
//! let dp = stm32::Peripherals::take().unwrap();
17+
//! let gpiog = dp.GPIOG.split();
18+
//! let mut led1 = gpiog.pg13.into_push_pull_output();
19+
//! led1.set_high().unwrap();
20+
//! }
21+
//! ```
22+
//!
23+
//! Without the prelude we would have to import the following traits:
24+
//!
25+
//! ```
26+
//! use stm32f4xx_hal::gpio::GpioExt; // for the split method.
27+
//! use embedded_hal::digital::v2::OutputPin; // for the set_high() function.
28+
//! // And more use-statements with more complex code.
29+
//! ```
30+
//!
31+
//! These imports are a bit unintuitive, because we can create the objects
32+
//! without the import. But we need these traits to access most of their
33+
//! functions.
34+
//!
35+
//! The prelude module keeps the import section cleaner:
36+
//! ```
37+
//! use stm32f4xx_hal::prelude::*;
38+
//! ```
139
pub use embedded_hal::digital::v2::InputPin as _embedded_hal_digital_v2_InputPin;
240
pub use embedded_hal::digital::v2::OutputPin as _embedded_hal_digital_v2_OutputPin;
341
pub use embedded_hal::digital::v2::StatefulOutputPin as _embedded_hal_digital_v2_StatefulOutputPin;

0 commit comments

Comments
 (0)