Skip to content

Commit 86e9c20

Browse files
authored
Make the crate #![no_std] (#79)
https://effective-rust.com/no-std.html says: > So: if your dependencies support it, and the simple transformations above are all that's needed, then **consider making library code no_std compatible**. When it is possible, it's not much additional work, and it allows for the widest reuse of the library. Code changes are minimal, we don't need a `std` feature because nothing will be disabled without it. `alloc` is definitely required for Frame/Plane/AlignedData but that's still a smaller hurdle than the entire standard library. `println` is now off the table for tests but that's not really a big limitation IMHO.
1 parent b5c3c4c commit 86e9c20

12 files changed

Lines changed: 49 additions & 23 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- **Support for common subsampling formats**: YUV 4:2:0, 4:2:2, 4:4:4, and monochrome
1515
- **Performant API**: Efficient row-based and pixel-based data access
1616
- **SIMD-friendly data alignment**: Plane data is aligned to at least 64 bytes on most targets
17+
- **`no_std` support**: Does not depend on the standard library (`alloc` is required however)
1718
- **WebAssembly support**: Works in both browser (`wasm32-unknown-unknown`) and WASI environments
1819

1920
## Installation
@@ -85,6 +86,12 @@ for pixel_row in frame.y_plane.rows() {
8586
}
8687
```
8788

89+
## `no_std` support
90+
91+
This crate does not depend on the standard library `std` but does require `alloc` to allocate memory for `Plane<T>` data.
92+
93+
Users linking against `std` don't have to do anything. If you want to use this crate without `std`, you need to setup and configure a global allocator. The [Embedded Rust Book](https://docs.rust-embedded.org/book/collections/index.html#using-alloc) might be helpful.
94+
8895
## WebAssembly Support
8996

9097
v_frame works in WebAssembly environments:

src/chroma.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
#[cfg(test)]
5757
mod tests;
5858

59-
use std::num::NonZeroU8;
59+
use core::num::NonZeroU8;
6060

6161
/// Specifies the chroma subsampling for a YUV frame.
6262
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

src/chroma/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#![allow(clippy::unwrap_used, reason = "test file")]
1111

1212
use super::*;
13-
use std::num::NonZeroU8;
13+
use core::num::NonZeroU8;
1414

1515
#[test]
1616
fn has_chroma() {

src/frame.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub use error::FrameError;
8181
#[cfg(test)]
8282
mod tests;
8383

84-
use std::num::NonZeroU8;
84+
use core::num::NonZeroU8;
8585

8686
use crate::{
8787
chroma::ChromaSubsampling,

src/frame/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// Media Patent License 1.0 was not distributed with this source code in the
88
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
99

10-
use std::fmt;
10+
use core::fmt;
1111

1212
/// This enum represents all possible error conditions that can occur during
1313
/// frame creation, including data validation errors, unsupported formats,
@@ -52,4 +52,4 @@ impl fmt::Display for FrameError {
5252
}
5353
}
5454

55-
impl std::error::Error for FrameError {}
55+
impl core::error::Error for FrameError {}

src/frame/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use super::*;
1313
use crate::chroma::ChromaSubsampling;
1414

15+
use alloc::format;
16+
1517
#[test]
1618
fn plane_access() {
1719
let mut frame = FrameBuilder::new(1920, 1080, ChromaSubsampling::Yuv420, 8)

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
//! .unwrap();
3434
//! ```
3535
36+
#![no_std]
37+
38+
extern crate alloc;
39+
3640
pub mod chroma;
3741
pub mod frame;
3842
pub mod pixel;

src/plane.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub use error::CopyError;
4242
mod tests;
4343

4444
#[cfg(feature = "padding_api")]
45-
use std::mem::MaybeUninit;
45+
use core::mem::MaybeUninit;
4646

4747
mod aligned;
4848
use aligned::AlignedData;

src/plane/aligned.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
use std::alloc::{Layout, alloc, alloc_zeroed, dealloc, handle_alloc_error};
2-
use std::fmt::Debug;
3-
use std::marker::PhantomData;
4-
use std::mem::{ManuallyDrop, MaybeUninit, align_of};
5-
use std::ops::{Deref, DerefMut};
6-
use std::ptr::NonNull;
1+
use alloc::alloc::{alloc, alloc_zeroed, dealloc, handle_alloc_error};
2+
3+
use core::alloc::Layout;
4+
use core::fmt::{self, Debug};
5+
use core::marker::PhantomData;
6+
use core::mem::{ManuallyDrop, MaybeUninit};
7+
use core::ops::{Deref, DerefMut};
8+
use core::ptr::NonNull;
79

810
use crate::pixel::Pixel;
911

@@ -148,7 +150,7 @@ impl<T: PartialEq<U>, U> PartialEq<AlignedData<U>> for AlignedData<T> {
148150
impl<T: Eq> Eq for AlignedData<T> {}
149151

150152
impl<T: Debug> Debug for AlignedData<T> {
151-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
153+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152154
if self.len() > 5 {
153155
f.debug_list().entries(&self[..5]).finish_non_exhaustive()
154156
} else {
@@ -209,6 +211,8 @@ impl<T> Drop for AlignedData<T> {
209211
mod tests {
210212
use super::*;
211213

214+
use alloc::{format, string::String, vec::Vec};
215+
212216
#[test]
213217
fn empty() {
214218
AlignedData::<u8>::new(0);
@@ -295,19 +299,24 @@ mod tests {
295299

296300
// SAFETY: Initialized above.
297301
let data = unsafe { data.assume_init() };
298-
println!("{:?}", &data[100..140]);
302+
for (idx, x) in data[100..140].iter().enumerate() {
303+
let idx = 100 + idx;
304+
assert_eq!(*x, (idx % 42) as u8);
305+
}
299306
}
300307

301308
#[test]
302309
fn uninit_with_drop() {
303310
let mut data = AlignedData::<String>::new_uninit(3);
304-
data[0].write("Hello World".into());
311+
data[0].write(String::from("Hello World"));
305312
data[1].write(String::new());
306-
data[2].write("This is a test".into());
313+
data[2].write(String::from("This is a test"));
307314

308315
// SAFETY: Initialized above.
309316
let data = unsafe { data.assume_init() };
310-
println!("{:?}", &*data);
317+
assert_eq!(data[0], String::from("Hello World"));
318+
assert_eq!(data[1], String::new());
319+
assert_eq!(data[2], String::from("This is a test"));
311320
}
312321

313322
#[test]
@@ -374,14 +383,16 @@ mod tests {
374383
#[test]
375384
fn clone() {
376385
let mut data = AlignedData::<String>::new_uninit(3);
377-
data[0].write("Hello World".into());
386+
data[0].write(String::from("Hello World"));
378387
data[1].write(String::new());
379-
data[2].write("This is a test".into());
388+
data[2].write(String::from("This is a test"));
380389

381390
// SAFETY: Initialized above.
382391
let data = unsafe { data.assume_init() };
383392
let data2 = data.clone();
384393
drop(data);
385-
println!("{:?}", &*data2);
394+
assert_eq!(data2[0], String::from("Hello World"));
395+
assert_eq!(data2[1], String::new());
396+
assert_eq!(data2[2], String::from("This is a test"));
386397
}
387398
}

src/plane/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// Media Patent License 1.0 was not distributed with this source code in the
88
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
99

10-
use std::fmt;
10+
use core::fmt;
1111

1212
/// An error representing why data couldn't be copied into a Plane.
1313
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -50,4 +50,4 @@ impl fmt::Display for CopyError {
5050
}
5151
}
5252

53-
impl std::error::Error for CopyError {}
53+
impl core::error::Error for CopyError {}

0 commit comments

Comments
 (0)