Skip to content

Commit 959b670

Browse files
Clean up example code.
If we put it in a function we need much less boiler plate to make it compile.
1 parent 4c31f43 commit 959b670

File tree

1 file changed

+13
-47
lines changed

1 file changed

+13
-47
lines changed

src/lib.rs

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,57 +16,23 @@
1616
//! couldn't work with a USB Thumb Drive, but we only supply a `BlockDevice`
1717
//! suitable for reading SD and SDHC cards over SPI.
1818
//!
19-
//! ```rust,no_run
20-
//! struct DummySpi;
21-
//! struct DummyCsPin;
22-
//! struct DummyUart;
23-
//! struct DummyTimeSource;
24-
//! struct DummyDelayer;
25-
//! use embedded_hal::spi::Operation;
26-
//! use embedded_hal::spi::ErrorType;
27-
//! use core::convert::Infallible;
28-
//! use core::fmt;
19+
//! ```rust
20+
//! use embedded_sdmmc::{Error, Mode, SdCard, SdCardError, TimeSource, VolumeIdx, VolumeManager};
2921
//!
30-
//! impl ErrorType for DummySpi {
31-
//! type Error = Infallible;
32-
//! }
33-
//! impl embedded_hal::spi::SpiDevice<u8> for DummySpi {
34-
//! fn transaction(&mut self, operations: &mut [Operation<'_, u8>]) -> Result<(), Self::Error> { Ok(()) }
35-
//! fn read(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> { Ok(()) }
36-
//! fn write(&mut self, buf: &[u8]) -> Result<(), Self::Error> { Ok(()) }
37-
//! fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> { Ok(()) }
38-
//! fn transfer_in_place(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> { Ok(()) }
39-
//! }
40-
//! impl embedded_hal::digital::ErrorType for DummyCsPin {
41-
//! type Error = Infallible;
42-
//! }
43-
//! impl embedded_hal::digital::OutputPin for DummyCsPin {
44-
//! fn set_low(&mut self) -> Result<(), Self::Error> { Ok(()) }
45-
//! fn set_high(&mut self) -> Result<(), Self::Error> { Ok(()) }
46-
//! }
47-
//! impl embedded_sdmmc::TimeSource for DummyTimeSource {
48-
//! fn get_timestamp(&self) -> embedded_sdmmc::Timestamp { embedded_sdmmc::Timestamp::from_fat(0, 0) }
49-
//! }
50-
//! impl embedded_hal::delay::DelayNs for DummyDelayer {
51-
//! fn delay_ns(&mut self, ns: u32) {}
52-
//! }
53-
//! impl fmt::Write for DummyUart {
54-
//! fn write_str(&mut self, s: &str) -> fmt::Result { Ok(()) }
55-
//! }
56-
//! use embedded_sdmmc::VolumeManager;
57-
//!
58-
//! fn main() -> Result<(), embedded_sdmmc::Error<embedded_sdmmc::SdCardError>> {
59-
//! let mut sdmmc_spi = DummySpi;
60-
//! let mut sdmmc_cs = DummyCsPin;
61-
//! let time_source = DummyTimeSource;
62-
//! let delayer = DummyDelayer;
63-
//! let sdcard = embedded_sdmmc::SdCard::new(sdmmc_spi, sdmmc_cs, delayer);
22+
//! fn example<S, CS, D, T>(spi: S, cs: CS, delay: D, ts: T) -> Result<(), Error<SdCardError>>
23+
//! where
24+
//! S: embedded_hal::spi::SpiDevice,
25+
//! CS: embedded_hal::digital::OutputPin,
26+
//! D: embedded_hal::delay::DelayNs,
27+
//! T: TimeSource,
28+
//! {
29+
//! let sdcard = SdCard::new(spi, cs, delay);
6430
//! println!("Card size is {} bytes", sdcard.num_bytes()?);
65-
//! let mut volume_mgr = VolumeManager::new(sdcard, time_source);
66-
//! let mut volume0 = volume_mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
31+
//! let mut volume_mgr = VolumeManager::new(sdcard, ts);
32+
//! let mut volume0 = volume_mgr.open_volume(VolumeIdx(0))?;
6733
//! println!("Volume 0: {:?}", volume0);
6834
//! let mut root_dir = volume0.open_root_dir()?;
69-
//! let mut my_file = root_dir.open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)?;
35+
//! let mut my_file = root_dir.open_file_in_dir("MY_FILE.TXT", Mode::ReadOnly)?;
7036
//! while !my_file.is_eof() {
7137
//! let mut buffer = [0u8; 32];
7238
//! let num_read = my_file.read(&mut buffer)?;

0 commit comments

Comments
 (0)