Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ The crate automatically adjusts memory alignment for WebAssembly: non-WASI
use a 64-byte minimum. Over-aligned pixel data uses `align_of::<T>()` if that is
larger.

## `no_std` support

This crate does not depend on the standard library `std` but does require `alloc` to allocate memory for `Plane<T>` data.

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.

## Feature Flags

- `padding_api`: Exposes low-level APIs for direct access to plane padding data (`geometry()`, `data()`, `data_mut()`)
Expand Down
2 changes: 1 addition & 1 deletion src/chroma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#[cfg(test)]
mod tests;

use std::num::NonZeroU8;
use core::num::NonZeroU8;

/// Specifies the chroma subsampling for a YUV frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
2 changes: 1 addition & 1 deletion src/chroma/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#![allow(clippy::unwrap_used, reason = "test file")]

use super::*;
use std::num::NonZeroU8;
use core::num::NonZeroU8;

#[test]
fn has_chroma() {
Expand Down
2 changes: 1 addition & 1 deletion src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub use error::FrameError;
#[cfg(test)]
mod tests;

use std::num::NonZeroU8;
use core::num::NonZeroU8;

use crate::{
chroma::ChromaSubsampling,
Expand Down
4 changes: 2 additions & 2 deletions src/frame/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Media Patent License 1.0 was not distributed with this source code in the
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.

use std::fmt;
use core::fmt;

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

impl std::error::Error for FrameError {}
impl core::error::Error for FrameError {}
2 changes: 2 additions & 0 deletions src/frame/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use super::*;
use crate::chroma::ChromaSubsampling;

use alloc::format;

#[test]
fn plane_access() {
let mut frame = FrameBuilder::new(1920, 1080, ChromaSubsampling::Yuv420, 8)
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
//! .unwrap();
//! ```

#![no_std]

extern crate alloc;

pub mod chroma;
pub mod frame;
pub mod pixel;
Expand Down
2 changes: 1 addition & 1 deletion src/pixel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
//! - 8-bit frames must use `u8`
//! - 9-16 bit frames must use `u16`

use core::fmt::Debug;
use num_traits::PrimInt;
use std::fmt::Debug;

mod private {
pub trait Sealed {}
Expand Down
2 changes: 1 addition & 1 deletion src/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub use error::CopyError;
mod tests;

#[cfg(feature = "padding_api")]
use std::mem::MaybeUninit;
use core::mem::MaybeUninit;

mod aligned;
use aligned::AlignedData;
Expand Down
39 changes: 25 additions & 14 deletions src/plane/aligned.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::alloc::{Layout, alloc, alloc_zeroed, dealloc, handle_alloc_error};
use std::fmt::Debug;
use std::marker::PhantomData;
use std::mem::{ManuallyDrop, MaybeUninit, align_of};
use std::ops::{Deref, DerefMut};
use std::ptr::NonNull;
use alloc::alloc::{alloc, alloc_zeroed, dealloc, handle_alloc_error};

use core::alloc::Layout;
use core::fmt::{self, Debug};
use core::marker::PhantomData;
use core::mem::{ManuallyDrop, MaybeUninit};
use core::ops::{Deref, DerefMut};
use core::ptr::NonNull;

use crate::pixel::Pixel;

Expand Down Expand Up @@ -148,7 +150,7 @@ impl<T: PartialEq<U>, U> PartialEq<AlignedData<U>> for AlignedData<T> {
impl<T: Eq> Eq for AlignedData<T> {}

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

use alloc::{format, string::String, vec::Vec};

#[test]
fn empty() {
AlignedData::<u8>::new(0);
Expand Down Expand Up @@ -295,19 +299,24 @@ mod tests {

// SAFETY: Initialized above.
let data = unsafe { data.assume_init() };
println!("{:?}", &data[100..140]);
for (idx, x) in data[100..140].iter().enumerate() {
let idx = 100 + idx;
assert_eq!(*x, (idx % 42) as u8);
}
}

#[test]
fn uninit_with_drop() {
let mut data = AlignedData::<String>::new_uninit(3);
data[0].write("Hello World".into());
data[0].write(String::from("Hello World"));
data[1].write(String::new());
data[2].write("This is a test".into());
data[2].write(String::from("This is a test"));

// SAFETY: Initialized above.
let data = unsafe { data.assume_init() };
println!("{:?}", &*data);
assert_eq!(data[0], String::from("Hello World"));
assert_eq!(data[1], String::new());
assert_eq!(data[2], String::from("This is a test"));
}

#[test]
Expand Down Expand Up @@ -374,14 +383,16 @@ mod tests {
#[test]
fn clone() {
let mut data = AlignedData::<String>::new_uninit(3);
data[0].write("Hello World".into());
data[0].write(String::from("Hello World"));
data[1].write(String::new());
data[2].write("This is a test".into());
data[2].write(String::from("This is a test"));

// SAFETY: Initialized above.
let data = unsafe { data.assume_init() };
let data2 = data.clone();
drop(data);
println!("{:?}", &*data2);
assert_eq!(data2[0], String::from("Hello World"));
assert_eq!(data2[1], String::new());
assert_eq!(data2[2], String::from("This is a test"));
}
}
4 changes: 2 additions & 2 deletions src/plane/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Media Patent License 1.0 was not distributed with this source code in the
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.

use std::fmt;
use core::fmt;

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

impl std::error::Error for CopyError {}
impl core::error::Error for CopyError {}
2 changes: 1 addition & 1 deletion src/plane/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::num::{NonZeroU8, NonZeroUsize};
use core::num::{NonZeroU8, NonZeroUsize};

use crate::chroma::ChromaSubsampling;

Expand Down
2 changes: 2 additions & 0 deletions src/plane/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use super::*;

use alloc::{format, vec, vec::Vec};

/// Helper function to create a simple plane geometry without padding
fn simple_geometry(width: usize, height: usize) -> PlaneGeometry {
PlaneGeometry::unpadded(width, height, 1, 1).expect("can create simple geometry")
Expand Down