Skip to main content

littlefs_rust/
lib.rs

1//! Safe Rust API for the LittleFS embedded filesystem.
2//!
3//! Built on [`littlefs-rust-core`](https://crates.io/crates/littlefs-rust-core), a
4//! function-by-function Rust port of the
5//! [C littlefs](https://github.com/littlefs-project/littlefs). No C toolchain required.
6//!
7//! # Quick start
8//!
9//! ```rust
10//! use littlefs_rust::{Config, Filesystem, RamStorage};
11//!
12//! let mut storage = RamStorage::new(512, 128);
13//! let config = Config::new(512, 128);
14//!
15//! Filesystem::format(&mut storage, &config).unwrap();
16//! let fs = Filesystem::mount(storage, config).map_err(|(e, _)| e).unwrap();
17//!
18//! fs.write_file("/hello.txt", b"Hello, littlefs!").unwrap();
19//! let data = fs.read_to_vec("/hello.txt").unwrap();
20//! assert_eq!(data, b"Hello, littlefs!");
21//!
22//! fs.unmount().unwrap();
23//! ```
24//!
25//! # Architecture
26//!
27//! The crate uses interior mutability ([`RefCell`](core::cell::RefCell)) so that
28//! [`Filesystem`] methods take `&self`. Each operation borrows the internal state only
29//! for the duration of one core call, then releases it. This enables multiple open
30//! files, interleaved file and directory operations, and reading files while iterating
31//! directories — all without conflict.
32//!
33//! [`File`] and [`ReadDir`] hold a shared reference to the [`Filesystem`] and implement
34//! [`Drop`] for RAII close.
35
36#![cfg_attr(not(feature = "std"), no_std)]
37
38#[cfg(feature = "alloc")]
39extern crate alloc;
40
41mod config;
42mod dir;
43mod error;
44mod file;
45mod filesystem;
46mod metadata;
47mod ram;
48mod storage;
49
50pub use config::Config;
51pub use dir::ReadDir;
52pub use error::Error;
53pub use file::File;
54pub use filesystem::Filesystem;
55pub use metadata::{DirEntry, FileType, Metadata, OpenFlags, SeekFrom};
56pub use ram::RamStorage;
57pub use storage::Storage;