Repository navigation
libtopo: add typed property getters mirroring topo_prop_get_* - #18
Merged
Merged
Conversation
smklein
force-pushed
the
libtopo-typed-property-getters
branch
4 times, most recently
from
September 16, 2026 13:44
0a453d5 to
c1a9125
Compare
Node::property returns a PropValue for callers to pattern-match, which suits properties of unknown type, but most callers know exactly what they expect and ended up writing a three-arm match (expected variant, other variant, error) around every read. Add Node::property_i32, property_u32, property_i64, property_u64, property_f64, property_string, and property_fmri, one per scalar topo_prop_get_* function. Each returns Result<T, Error>. The fixed-size getters share one generic helper whose function-pointer parameter ties the out-type to the C getter that writes it. Two private unsafe helpers, adopt_topo_string and adopt_topo_nvlist, take ownership of a libtopo-allocated string or nvlist; the new getters and the existing label, resource, asru, fru, fmri_parse, fmri_to_string, snapshot uuid, property, and property_groups paths all go through them, so the copy-then-strfree and cast-then-wrap sequences each live in one place. Callers of these getters usually treat "property absent" and "property has the wrong type" differently (debug log vs. warning), so libtopo's ETOPO_PROP_NOENT and ETOPO_PROP_TYPE map to two new variants, Error::PropertyNotFound and Error::PropertyType, each carrying the group and name (and, for the type error, the type that was asked for). Node::property reports a missing property the same way. All other codes still surface as Error::Topo. Array getters are not wrapped; PropValue already covers them. libtopo bumps to 0.2.0: Error is not non_exhaustive, so the new variants can break exhaustive matches. Fixes #16
smklein
force-pushed
the
libtopo-typed-property-getters
branch
from
September 16, 2026 13:48
c1a9125 to
5096cf2
Compare
smklein
added a commit
that referenced
this pull request
Sep 16, 2026
Node::name and Node::property work in `&str`, but the names from `<fm/topo_hc.h>` were only available as the NUL-terminated byte arrays libtopo-sys binds, so consumers hand-copied `"nvme"`, `"io"`, `"instance"`. - libtopo-sys allowlists all of topo_hc.h (was `BAY|NVME|SLOT`); regenerated with bindgen 0.72.1 on a Helios 3 host. This adds the remaining hc node names (CHASSIS, MOTHERBOARD, PCIEX_DEVICE, ...) and TOPO_PROP_NVME_NVM_CAPACITY from the current header. - New `libtopo::hc`: every topo_hc.h string constant as `&'static str`, derived at compile time from the sys byte arrays so the values stay tied to the header. Names match the C macros. The conversion is built on the const `CStr` APIs, which keeps the crate's MSRV at the edition-2024 floor and rejects interior NULs; `Scheme::as_cstr` shares the same const helper. - `generate.sh` records the header's string-macro names in `libtopo/src/topo_hc_names.txt`, and a unit test asserts `libtopo::hc` lists exactly those names, so regenerating against a newer header fails the test until `hc.rs` is updated. - Examples use the constants instead of a libtopo-sys shim. - The `/usr/lib/fm/amd64` RPATH for libtopo's tests, examples, and doctests now comes from a `libtopo/build.rs` that consumes the `DEP_TOPO_LIBDIRS` hint libtopo-sys already exports, so they load libtopo.so.1 without the repo-local `.cargo/config.toml`, which is removed. This only affects libtopo's own targets; dependents still set their own RPATH. - CI runs `cargo test --doc`, since neither `--all-targets` nor nextest builds doctests. - libtopo-sys bumps to 0.1.1. libtopo stays at the 0.2.0 set by #18; this change is purely additive. Fixes #15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Node::propertyreturns aPropValuefor callers to pattern-match, whichsuits properties of unknown type, but callers that know the type ended up
writing a three-arm match around every read.
Node::property_i32/property_u32/property_i64/property_u64/property_f64/property_string/property_fmri, one per scalartopo_prop_get_*function, each returningResult<T, Error>.Error::PropertyNotFound { group, name }and a type mismatch as
Error::PropertyType { group, name, expected },mapped from libtopo's
ETOPO_PROP_NOENT/ETOPO_PROP_TYPE. Callerstypically log the two differently, and the old
Error::Topo(String)only let them tell the cases apart by message text.
Node::propertyreports a missing property the same way; other codes stay
Error::Topo.parameter ties the Rust out-type to the C getter that writes it.
adopt_topo_stringandadopt_topo_nvlist,own the copy-then-
topo_hdl_strfreeand cast-then-OwnedNvListsequences.The new getters and the existing
label,resource,asru,fru,fmri_parse,fmri_to_string, snapshot uuid,property, andproperty_groupspaths all go through them.PropValuealready covers them.Erroris notnon_exhaustiveand the newvariants can break exhaustive matches.
Tested on a Helios 3 host: fmt, clippy
-D warnings, and the full testsuite under pfexec all pass.
Fixes #16