Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for no_std #4

Merged
merged 1 commit into from
May 3, 2021
Merged

Support for no_std #4

merged 1 commit into from
May 3, 2021

Conversation

brysgo
Copy link
Contributor

@brysgo brysgo commented May 2, 2021

Was trying to compile a bevy app to wasm and got this error, so instead of playing with feature flags on bevy I decided to practice my rust and try and close out this issue. #2

Copy link
Owner

@chinedufn chinedufn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome stuff, thanks so much for taking the time to make this improvement.

The main feedback is around keeping the HashMap in std.

I know the original issue said to move to a BTreeMap but now that I think about it there's no reason to pay that cost in std.

Awesome stuff.

Cargo.toml Outdated
default = ["std"]
std = []

[target.'cfg(not(feature = "std"))'.dependencies]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love it if we could avoid introducing the extra dependency and just use the core crate. The core crate should work regardless of whether or not we are in no_std.

Since this crate is fairly small it hopefully isn't a hassle to just convert all of the std -> core.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I added it was because I couldn't figure out how to do use std::cmp::Ordering - do you have any idea what to do for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually the core seemed to work for that one, now I'm having trouble with Hash

@@ -19,9 +30,9 @@ where
{
// FIXME: inbound_id_to_group_id appears to be unused. If so, remove it. Also remove the
// Hash and Eq constraints on RectToPlaceId if we remove this map
pub(crate) inbound_id_to_group_ids: HashMap<RectToPlaceId, Vec<Group<GroupId, RectToPlaceId>>>,
pub(crate) inbound_id_to_group_ids: BTreeMap<RectToPlaceId, Vec<Group<GroupId, RectToPlaceId>>>,
Copy link
Owner

@chinedufn chinedufn May 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Top of file
#[cfg(std)]
use std::collections::{btree_map::Entry, BTreeMap as KeyValMap};

// Here
pub(crate) inbound_id_to_group_ids: KeyValMap<RectToPlaceId, Vec<Group<GroupId, RectToPlaceId>>>,

Then if not(std) KeyValMap can be an alloc::collections::BTreeMap

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed the part about alloc being the same as core in that std just re-exports it. Should be good to go now.

src/lib.rs Outdated
@@ -128,7 +141,7 @@ pub fn pack_rects<
box_size_heuristic: &BoxSizeHeuristicFn,
more_suitable_containers_fn: &ComparePotentialContainersFn,
) -> Result<RectanglePackOk<RectToPlaceId, BinId>, RectanglePackError> {
let mut packed_locations = HashMap::new();
let mut packed_locations = BTreeMap::new();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing in this file, KeyValMap and make it use HashMap in std and BTreeMap in no_std.

You can just define KeyValMap once in the crate root and everywhere else that's applicable use crate::KeyValMap

@brysgo
Copy link
Contributor Author

brysgo commented May 2, 2021

Okay, I think I made all the requested changes, let me know if anything is missing

@chinedufn
Copy link
Owner

Looks great, thanks so much.

@chinedufn
Copy link
Owner

Mind just squashing into one commit and rebasing then I'll merge? Thank you.

@chinedufn chinedufn mentioned this pull request May 2, 2021
Copy link
Owner

@chinedufn chinedufn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So sorry, one more bit of cleanup. Then if you rebase as one commit I can get this merged and released. Thanks a lot.

cmp::Ordering,
fmt::{Debug, Display, Error as ErrorFmt, Formatter},
};
#[cfg(std)]
Copy link
Owner

@chinedufn chinedufn May 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops I missed something.

std just relies on and re-exports core. So these cfg's are unnecessary.

This can just be

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

Regardless of whether the std feature flag is enabled or not.

std only needs to control whether we use HashMap or BTreeMap as well as whether or not we impl std::err::Error for RectanglePackError


This applies to all of the files, and also applies to the alloc crate (std just re-exports alloc).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool!

@chinedufn chinedufn merged commit 250529c into chinedufn:master May 3, 2021
@chinedufn
Copy link
Owner

Thanks!

@chinedufn
Copy link
Owner

Published to crates.io as rectangle-pack = "0.4.2"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants