Skip to content

Commit 397560d

Browse files
committed
add fut example
1 parent 7e24c5e commit 397560d

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/lib.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@
7171
//! ```
7272
//!
7373
//! Once your API uses [`nb::Result`] you can leverage the [`block!`], macro
74-
//! to adapt it for blocking operation, or handle scheduling yourself.
74+
//! to adapt it for blocking operation, or handle scheduling yourself. You can
75+
//! also use the [`fut!`] macro to use it in an async/await context
7576
//!
7677
//! [`block!`]: macro.block.html
78+
//! [`fut!`]: macro.fut.html
7779
//! [`nb::Result`]: type.Result.html
7880
//!
7981
//! # Examples
@@ -182,6 +184,44 @@
182184
//! # }
183185
//! # }
184186
//! ```
187+
//! ## Future mode
188+
//!
189+
//! Turn on an LED for one second and *then* loops back serial data.
190+
//!
191+
//! ```
192+
//! use core::convert::Infallible;
193+
//! use nb::fut;
194+
//!
195+
//! use hal::{Led, Serial, Timer};
196+
//!
197+
//! # async fn run() -> Result<(), Infallible>{
198+
//! Led.on();
199+
//! fut!(Timer.wait()).await;
200+
//! Led.off();
201+
//! loop {
202+
//! let byte = fut!(Serial.read()).await?;
203+
//! fut!(Serial.write(byte)).await?;
204+
//! }
205+
//! # }
206+
//!
207+
//! # mod hal {
208+
//! # use nb;
209+
//! # use core::convert::Infallible;
210+
//! # pub struct Led;
211+
//! # impl Led {
212+
//! # pub fn off(&self) {}
213+
//! # pub fn on(&self) {}
214+
//! # }
215+
//! # pub struct Serial;
216+
//! # impl Serial {
217+
//! # pub fn read(&self) -> nb::Result<u8, Infallible> { Ok(0) }
218+
//! # pub fn write(&self, _: u8) -> nb::Result<(), Infallible> { Ok(()) }
219+
//! # }
220+
//! # pub struct Timer;
221+
//! # impl Timer {
222+
//! # pub fn wait(&self) -> nb::Result<(), Infallible> { Ok(()) }
223+
//! # }
224+
//! # }
185225
186226
#![no_std]
187227
#![doc(html_root_url = "https://docs.rs/nb/1.0.0")]

0 commit comments

Comments
 (0)