Skip to content

Commit

Permalink
Document no_std gotchas, provide c-stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
nickray committed Sep 26, 2021
1 parent e461162 commit 3110912
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "littlefs2"
description = "Idiomatic Rust API for littlefs"
version = "0.3.1"
version = "0.3.2"
authors = ["Nicolas Stalder <[email protected]>", "Brandon Edens <[email protected]>"]
edition = "2018"
license = "Apache-2.0 OR MIT"
Expand Down Expand Up @@ -43,6 +43,7 @@ dir-entry-path = []
ll-assertions = ["littlefs2-sys/assertions"]
# enable trace in backend C code
ll-trace = ["littlefs2-sys/trace"]
c-stubs = []

log-all = []
log-none = []
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ Upstream release: [v2.1.4][upstream-release]
[littlefs2-sys]: https://lib.rs/littlefs2-sys
[upstream-release]: https://github.com/ARMmbed/littlefs/releases/tag/v2.1.4

## `no_std`

This library is `no_std` compatible, but there are two gotchas.

- The dev-dependency `memchr` of `littlefs2-sys` has its `std` features activated. To prevent this, upgrade to at least Rust 1.51
and add `resolver = "2"` in the consuming code's `[package]` section. This will be the default in Rust 2021 edition.

- At link time, `lfs.c` has a dependency on `strcpy`. When not linking to a `libc` with this symbol, activate the `c-stubs` feature
to provide an implementation.

#### License

<sup>littlefs is licensed under [BSD-3-Clause](https://github.com/ARMmbed/littlefs/blob/master/LICENSE.md).</sup>
Expand Down
29 changes: 29 additions & 0 deletions src/c_stubs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! C functions not provided by compiler-builtins
//!
//! Use this instead of linking to libc if you only need a handful of free functions
use cty::{c_char, c_void, size_t};

extern "C" {
// provided by `compiler-builtins`
fn memcpy(dst: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
}

/// # Safety
/// - `src` must be a valid C string (null terminated)
/// - `dst` must be large enough to hold `src`
#[no_mangle]
unsafe fn strcpy(dst: *mut c_char, src: *const c_char) -> *mut c_char {
memcpy(dst as *mut c_void, src as *const c_void, strlen(src)) as *mut c_char
}

/// # Safety
/// `s` must point to valid memory; `s` will be treated as a null terminated string
pub unsafe fn strlen(mut s: *const c_char) -> size_t {
let mut n = 0;
while *s != 0 {
s = s.add(1);
n += 1;
}
n
}
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ terms of actual and `typenum` constants, and an implementation supplies methods
The filesystem and each open file need memory for state and caching, this has to be allocated
beforehand and passed to constructors.
### `no_std`
This library is `no_std` compatible, but there are two gotchas.
- The dev-dependency `memchr` of `littlefs2-sys` has its `std` features activated. To prevent this, upgrade to at least Rust 1.51
and add `resolver = "2"` in the consuming code's `[package]` section. This will be the default in Rust 2021 edition.
- At link time, `lfs.c` has a dependency on `strcpy`. When not linking to a `libc` with this symbol, activate the `c-stubs` feature
to provide an implementation.
### Design notes
All operations on the filesystem require passing a `&mut Storage`, which guarantees by Rust's
Expand Down Expand Up @@ -124,6 +134,9 @@ generate_macros!();
#[macro_use]
pub mod macros;

#[cfg(feature = "c-stubs")]
mod c_stubs;

pub mod consts;
pub mod driver;

Expand Down

0 comments on commit 3110912

Please sign in to comment.