Skip to content

Commit

Permalink
Document no_std feature in README
Browse files Browse the repository at this point in the history
Also clean up a few imports.
  • Loading branch information
chinedufn committed May 3, 2021
1 parent 250529c commit de824b1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ The API shouldn't know about the specifics of any of these requirements - it sho
</details>
<p></p>

## no_std

rectangle-pack supports `no_std` by disabling the `std` feature.

```toml
rectangle-pack = {version = "0.4", default-features = false}
```

Disabling the `std` feature does the following.

- `BTreeMap`s are used internally in places where `HashMap`s would have been used.

## Features

- Place any number of 2d / 3d rectangles into any number of 2d / 3d target bins.
Expand Down
4 changes: 2 additions & 2 deletions src/bin_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{BoxSizeHeuristicFn, PackedLocation, RectToInsert, WidthHeightDepth};

use core::{
cmp::Ordering,
fmt::{Debug, Display, Error as ErrorFmt, Formatter},
fmt::{Debug, Display, Error as FmtError, Formatter},
};

mod overlaps;
Expand Down Expand Up @@ -60,7 +60,7 @@ pub enum BinSectionError {
}

impl Display for BinSectionError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), ErrorFmt> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
let err = match self {
BinSectionError::PlacementWiderThanBinSection => {
"Can not place a rectangle inside of a bin that is wider than that rectangle."
Expand Down
7 changes: 3 additions & 4 deletions src/grouped_rects_to_place.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use crate::RectToInsert;

#[cfg(std)]
use crate::Vec;
#[cfg(not(std))]
use alloc::collections::BTreeMap as KeyValMap;
#[cfg(std)]
use std::collections::HashMap as KeyValMap;

use alloc::{
collections::{btree_map::Entry, BTreeMap},
vec::Vec,
};
use core::{fmt::Debug, hash::Hash};
#[cfg(std)]
use std::collections::HashMap as KeyValMap;

/// Groups of rectangles that need to be placed into bins.
///
Expand Down
15 changes: 9 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ extern crate alloc;

#[cfg(not(std))]
use alloc::collections::BTreeMap as KeyValMap;
#[cfg(std)]
use std::collections::HashMap as KeyValMap;

use alloc::{collections::BTreeMap, vec::Vec};

use core::{
fmt::{Debug, Display, Error as ErrorFmt, Formatter},
fmt::{Debug, Display, Error as FmtError, Formatter},
hash::Hash,
};
#[cfg(std)]
use std::collections::HashMap as KeyValMap;

pub use crate::bin_section::contains_smallest_box;
pub use crate::bin_section::BinSection;
Expand Down Expand Up @@ -284,17 +286,18 @@ pub enum RectanglePackError {
NotEnoughBinSpace,
}

#[cfg(std)]
impl std::error::Error for RectanglePackError {}

impl Display for RectanglePackError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), ErrorFmt> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
match self {
RectanglePackError::NotEnoughBinSpace => {
f.write_str("Not enough space to place all of the rectangles.")
}
}
}
}
#[cfg(std)]
impl std::error::Error for RectanglePackError {}

fn sort_bins_smallest_to_largest<BinId>(
bins: &mut Vec<(&BinId, &mut TargetBin)>,
Expand Down
4 changes: 2 additions & 2 deletions src/target_bin/push_available_bin_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use crate::bin_section::BinSection;
use crate::TargetBin;
use core::fmt::{Display, Formatter, Result as ResultFmt};
use core::fmt::{Display, Formatter, Result as FmtResult};

impl TargetBin {
/// Push a [`BinSection`] to the list of remaining [`BinSection`]'s that rectangles can be
Expand Down Expand Up @@ -77,7 +77,7 @@ pub enum PushBinSectionError {
}

impl Display for PushBinSectionError {
fn fmt(&self, f: &mut Formatter<'_>) -> ResultFmt {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
PushBinSectionError::OutOfBounds(oob) => {
f.debug_tuple("BinSection").field(oob).finish()
Expand Down

0 comments on commit de824b1

Please sign in to comment.