Skip to content

Conversation

@bushrat011899
Copy link
Contributor

@bushrat011899 bushrat011899 commented Mar 11, 2025

Objective

Solution

  • Promoted hashbrown to a direct dependency (currently transient through indexmap)
  • Added a new std feature, and included it in all features which rely on std for documentation purposes.
  • Added new CI task to ensure no_std support works (using wasm32v1-none as an example no_std target)
  • Added several lints which help minimise std usage
  • Adjusted certain public APIs to allow passing a S: BuildHasher instead of implicitly relying on std::hash::RandomState when the std feature is disabled

Notes

I know this has been attempted several times before and delayed due to a desire to refactor the crate first before adding this functionality. Delays in adding no_std support forced Bevy to drop petgraph from its core crates, relying on a specialised alternative. We'd love to keep using petgraph, but no_std support is now a requirement going forward. This will also come up with wgpu which is also pursuing no_std support here.


BREAKING CHANGE:

Petgraph previously assumed the usage of std::hash::RandomState as the hashing implementation across its codebase. In no_std, this is not possible. To minimise friction for libraries supporting both std and no_std with Petgraph, I have made the following changes to the public API:

petgraph::algo::simple_paths::all_simple_paths

This function now has a 3rd generic parameter S which controls the hashing implementation used. Because all_simple_paths is a function it cannot have default generic parameters, so users must specify the hasher themselves.

// Before
let foo = all_simple_paths(/* ... */);

// After
let foo = all_simple_paths::<_, _, std::hash::RandomState>(/* ... */);

Switched from std::collections::{HashMap, HashSet} to hashbrown::{HashMap, HashSet}

To support no_std, we cannot use the standard library's implementations of HashMap or HashSet. Methods and types previously referencing those collections from std will now reference them from hashbrown. Note that hashbrown was already a dependency of Petgraph, so no change in audit requirements.

Added Hashing Parameter

The following types have had a new generic parameter S added to specify the hashing implementation. Note that when std is enabled, these will all default to std::hash::RandomState, as before.

  • UnGraphMap
  • DiGraphMap
  • MatrixGraph
  • DiMatrix
  • UnMatrix
  • NodeIdentifiers
  • NodeReferences

Note that for MatrixGraph, DiMatrix, UnMatrix the new S parameter is in the 3rd position (all others have S in the last position). The reason is S has a default parameter with std enabled, but is required with std disabled. This means it must be the last required parameter.

// Before
let foo: MatrixGraph<Foo, Bar, Directed, Option<Bar>, DefaultIx>;

// After
let foo: MatrixGraph<Foo, Bar, std::hash::RandomState, Directed, Option<Bar>, DefaultIx>;

Also note that because Default can now be implemented for multiple versions of the above types (generic over the hasher), you may need to either specify the hasher, or explicitly declare it as default (with the std feature enabled):

// Note that N and E are inferred by code below.
// Before
let foo = UnMatrix::default();

// After (Explicitly infer N and E, but use defaults otherwise)
let foo = UnMatrix::<_, _>::default();

default-features = false MSRV is 1.81

If you don't enable the std feature, the MSRV increases to 1.81, when core::error::Error was stabilised. To preserve the original MSRV of 1.64, just enable the std feature.

@bushrat011899
Copy link
Contributor Author

CI issue caused by not fixing up imports in tests. Should be resolved now; tested with cargo clippy --all-features --lib --bins --examples --tests -- -D warnings locally.

@ABorgna ABorgna changed the title Add no_std Support feat: Add no_std Support Mar 19, 2025
@starovoid
Copy link
Collaborator

Hi, very nice!

I have looked at the proposed changes and feel satisfied. Can you also add no_std support for the matrix_graph and stable_graph features? This will make no_std support more complete.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 20, 2025

This PR contains breaking changes to the public Rust API.

cargo-semver-checks summary

--- failure function_requires_different_generic_type_params: function now requires a different number of generic type parameters ---

Description:
A function now requires a different number of generic type parameters than it used to. Uses of this function that supplied the previous number of generic types (e.g. via turbofish syntax) will be broken.
      ref: https://doc.rust-lang.org/reference/items/generics.html
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.40.0/src/lints/function_requires_different_generic_type_params.ron

Failed in:
function all_simple_paths (2 -> 3 generic types) in /home/runner/work/petgraph/petgraph/PR_BRANCH/src/algo/simple_paths.rs:55
function all_simple_paths (2 -> 3 generic types) in /home/runner/work/petgraph/petgraph/PR_BRANCH/src/algo/simple_paths.rs:55

@bushrat011899 bushrat011899 changed the title feat: Add no_std Support feat!: Add no_std Support Mar 20, 2025
@bushrat011899
Copy link
Contributor Author

I believe semver-checks needs to be re-run to observe the declared breaking changes, but please let me know if that's not the case and what I need to action!

@bushrat011899
Copy link
Contributor Author

Can you also add no_std support for the matrix_graph and stable_graph features? This will make no_std support more complete.

I have gone back and added no_std support to all features in Petgraph, excluding rayon and std.

BREAKING CHANGE: refer to petgraph#747 PR description for details on changes.
Updated CI task to highlight that `std` is required for Rust versions between 1.64 and 1.80 inclusive. For 1.81 and above, the `std` feature is not required.
@starovoid starovoid added this to the 0.8 milestone Mar 21, 2025
@starovoid starovoid mentioned this pull request Mar 21, 2025
8 tasks
@starovoid starovoid added this pull request to the merge queue Mar 21, 2025
Merged via the queue into petgraph:master with commit 5087201 Mar 21, 2025
10 checks passed
@starovoid starovoid mentioned this pull request Mar 26, 2025
github-merge-queue bot pushed a commit that referenced this pull request Apr 5, 2025
A sufficient number of proposed changes have accumulated to combine them
and publish a new major release numbered `0.8.0`.


BREAKING CHANGE:

This will require the user to provide extra type parameter in some APIs
(Read more in #747).

## List of changes
- [x] #747
The main innovation of the current release, the long-awaited feature
that has become very relevant due to the transition of dependent
projects to support `no_std`.
- [x] #662 
- [x] #611
- [x] #728
- [x] #686
- [x] #737 
- [x] #720 
- [x] #718 

## Note
There are still a large number of PRs that we want to adopt in the near
future, so we should expect at least a release of `0.8.1` soon after the
completion of `0.8.0`.

Thank you all for participating!

---------

Co-authored-by: Agustin Borgna <agustinborgna@gmail.com>
@RaoulLuque RaoulLuque mentioned this pull request Jun 21, 2025
cactusdualcore pushed a commit to cactusdualcore/petgraph that referenced this pull request Jul 18, 2025
The function 'zip' was introduced in petgraph#747 as a part of adding `no_std`
support. However, there is already such a function in `core::iter`.
The MSRV stated in the description of petgraph#747 was 1.64 at minimum, but the
function in `core` is stable since Rust 1.59. Thus, it is an unnecessary
duplication and can be removed without consequences.
github-merge-queue bot pushed a commit that referenced this pull request Jul 26, 2025
…849)

The function 'zip' was introduced in #747 as a part of adding `no_std`
support. However, there is already such a function in `core::iter`. The
MSRV stated in the description of #747 was 1.64 at minimum, but the
function in `core` is stable since Rust 1.59. Thus, it is an unnecessary
duplication and can be removed without consequences.

<!--
  -- Thanks for contributing to `petgraph`! 
  --
-- We require PR titles to follow the Conventional Commits
specification,
-- https://www.conventionalcommits.org/en/v1.0.0/. This helps us
generate
  -- changelogs and follow semantic versioning.
  --
  -- Start the PR title with one of the following:
  --  * `feat:` for new features
  --  * `fix:` for bug fixes
  --  * `refactor:` for code refactors
  --  * `docs:` for documentation changes
  --  * `test:` for test changes
  --  * `perf:` for performance improvements
  --  * `revert:` for reverting changes
  --  * `ci:` for CI/CD changes
-- * `chore:` for changes that don't fit in any of the above categories
  -- The last two categories will not be included in the changelog.
  --
-- If your PR includes a breaking change, please add a `!` after the
type
-- and include a `BREAKING CHANGE:` line in the body of the PR
describing
  -- the necessary changes for users to update their code.
  --
  -->

Co-authored-by: cactusdualcore <cactusdualcore@noreply.codeberg.org>
BryanCruz pushed a commit to BryanCruz/petgraph that referenced this pull request Jul 26, 2025
…etgraph#849)

The function 'zip' was introduced in petgraph#747 as a part of adding `no_std`
support. However, there is already such a function in `core::iter`. The
MSRV stated in the description of petgraph#747 was 1.64 at minimum, but the
function in `core` is stable since Rust 1.59. Thus, it is an unnecessary
duplication and can be removed without consequences.

<!--
  -- Thanks for contributing to `petgraph`! 
  --
-- We require PR titles to follow the Conventional Commits
specification,
-- https://www.conventionalcommits.org/en/v1.0.0/. This helps us
generate
  -- changelogs and follow semantic versioning.
  --
  -- Start the PR title with one of the following:
  --  * `feat:` for new features
  --  * `fix:` for bug fixes
  --  * `refactor:` for code refactors
  --  * `docs:` for documentation changes
  --  * `test:` for test changes
  --  * `perf:` for performance improvements
  --  * `revert:` for reverting changes
  --  * `ci:` for CI/CD changes
-- * `chore:` for changes that don't fit in any of the above categories
  -- The last two categories will not be included in the changelog.
  --
-- If your PR includes a breaking change, please add a `!` after the
type
-- and include a `BREAKING CHANGE:` line in the body of the PR
describing
  -- the necessary changes for users to update their code.
  --
  -->

Co-authored-by: cactusdualcore <cactusdualcore@noreply.codeberg.org>
RaoulLuque pushed a commit to RaoulLuque/petgraph that referenced this pull request Sep 21, 2025
…etgraph#849)

The function 'zip' was introduced in petgraph#747 as a part of adding `no_std`
support. However, there is already such a function in `core::iter`. The
MSRV stated in the description of petgraph#747 was 1.64 at minimum, but the
function in `core` is stable since Rust 1.59. Thus, it is an unnecessary
duplication and can be removed without consequences.

<!--
  -- Thanks for contributing to `petgraph`! 
  --
-- We require PR titles to follow the Conventional Commits
specification,
-- https://www.conventionalcommits.org/en/v1.0.0/. This helps us
generate
  -- changelogs and follow semantic versioning.
  --
  -- Start the PR title with one of the following:
  --  * `feat:` for new features
  --  * `fix:` for bug fixes
  --  * `refactor:` for code refactors
  --  * `docs:` for documentation changes
  --  * `test:` for test changes
  --  * `perf:` for performance improvements
  --  * `revert:` for reverting changes
  --  * `ci:` for CI/CD changes
-- * `chore:` for changes that don't fit in any of the above categories
  -- The last two categories will not be included in the changelog.
  --
-- If your PR includes a breaking change, please add a `!` after the
type
-- and include a `BREAKING CHANGE:` line in the body of the PR
describing
  -- the necessary changes for users to update their code.
  --
  -->

Co-authored-by: cactusdualcore <cactusdualcore@noreply.codeberg.org>
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.

3 participants