Skip to content
This repository was archived by the owner on Jan 16, 2026. It is now read-only.
This repository was archived by the owner on Jan 16, 2026. It is now read-only.

Add public accessor method for NodeId to extract inner usize value #112

Closed

Description

@dsaad68

Description:

Problem

The NodeId struct wraps a usize with pub(crate) visibility, making the inner value inaccessible from external crates:

#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Ord, PartialOrd)]
pub struct NodeId(pub(crate) usize);

This makes it difficult for users of the library to:

  • Serialize node IDs to external formats (JSON, databases, etc.)
  • Use node IDs in external logging/monitoring systems that expect numeric identifiers
  • Interface with other systems that require numeric IDs
  • Debug and inspect node ID values programmatically

Currently, the only way to see the value is through Debug formatting ({:?}), which is fragile and not suitable for programmatic access.

Proposed Solution

Add a public accessor method to the NodeId struct:

impl NodeId {
    pub fn as_usize(&self) -> usize {
        self.0
    }
}

This follows common Rust patterns for newtype wrappers (similar to NonZeroUsize::get(), etc.) and maintains the type safety of NodeId while providing controlled access to the underlying value when needed.

Alternatives Considered

  1. Make the field public (pub usize) - This would weaken the encapsulation and type safety that the newtype pattern provides.

  2. Implement From<NodeId> for usize - This works but consumes the NodeId (though it's Copy, so not a real issue). The explicit method is more discoverable.

Activity

  1. 191220029 commented on Nov 27, 2025

    @191220029
    Collaborator

    Hi! We added both NodeId::as_usize() and a From<NodeId> for usize implementation (#113) so callers can safely obtain the numeric ID. Thanks for your issue :)

  2. dsaad68 commented on Dec 1, 2025

    @dsaad68
    Author

    Thanks, a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions