Skip to content
Merged
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
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ jobs:
- run: ./examples/run_examples.sh
- run: ./examples/example_crates/run_example_crates.sh

multi-targets-no-std:
name: multi-targets no_std
runs-on: ubuntu-latest
strategy:
matrix:
target:
- x86_64-unknown-linux-gnu
- x86_64-unknown-none
- aarch64-unknown-linux-gnu
- aarch64-unknown-none-softfloat
- riscv64gc-unknown-none-elf
- riscv64imac-unknown-none-elf
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
components: rustc, cargo
- run: cargo build --target ${{ matrix.target }} --no-default-features

rustfmt-check:
name: rustfmt check
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exclude = ["examples"]
authors = [ "aarkegz <aarkegz@gmail.com>" ]
license = "MIT"
edition = "2021"
version = "1.1.0"
version = "1.2.0"
repository = "https://github.com/GeminiLab/enumerable/"
include = [
"README.md",
Expand All @@ -33,4 +33,4 @@ std = ["enumerable_derive/std"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
enumerable_derive = { path = "enumerable_derive", version = "=1.1.0" }
enumerable_derive = { path = "enumerable_derive", version = "=1.2.0" }
5 changes: 5 additions & 0 deletions examples/example_crates/no_std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ version = "0.1.0"

[dependencies]
enumerable = { path = "../../../", default-features = false }

[profile]
dev.panic = "abort"
release.panic = "abort"

4 changes: 2 additions & 2 deletions examples/example_crates/no_std/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This example tests the `Enumerable` derive macro in a `no_std` environment.

#![no_std]
//!
//! This example itself is not `no_std`, but it uses the `enumerable` crate with `default-features = false`.

use enumerable::Enumerable;

Expand Down
20 changes: 20 additions & 0 deletions src/impl_built_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ where
self.inner.next().map(Some)
}
}

/// Implements the standard library's `Iterator::size_hint` method.
///
/// This method returns the lower and upper bound of the iterator. For `OptionEnumerator`,
/// the bounds are adjusted to account for the `None` variant:
/// - If `self.first` is `true`, the lower bound is incremented by 1, and the upper bound
/// is incremented by 1 (if it exists), to include the `None` variant.
/// - Otherwise, the bounds are directly derived from the inner enumerator.
fn size_hint(&self) -> (usize, Option<usize>) {
let (lower, upper) = self.inner.size_hint();

if self.first {
(
lower.saturating_add(1),
upper.and_then(|u| u.checked_add(1)),
)
} else {
(lower, upper)
}
}
}

/// This is an implementation of the `Enumerable` trait for `Option<T>` where `T` is `Enumerable`.
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![doc = include_str!("./CRATE_DOC.md")]

/// `Enumerable` is a trait for types that can have their possible values enumerated.
Expand Down
2 changes: 1 addition & 1 deletion src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod primitive {

#[test]
fn test_option_bool() {
assert_enumerator_eq(vec![None, Some(false), Some(true)]);
assert_enumerator_eq_with_size_hint(vec![None, Some(false), Some(true)]);
}

#[test]
Expand Down