1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-24 00:48:53 +03:00
Commit Graph

87 Commits

Author SHA1 Message Date
Bartosz Golaszewski
2eab5e896f bindings: rust: add examples to the README.md in the libgpiod crate
Add some code examples to the README.md with the aim of showing how to
use the bindings on the crates.io landing page for the libgpiod crate.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250926-rust-release-tweaks-v1-3-beae932eb691@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-09-30 09:53:22 +02:00
Bartosz Golaszewski
d0f1291f41 bindings: rust: update formatting to --edition 2024
Update the formatting of all rust source files to be up to date with the
2024 rust edition.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250926-rust-release-tweaks-v1-2-beae932eb691@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-09-30 09:53:22 +02:00
Bartosz Golaszewski
e549494229 bindings: rust: complete the unification of imports in examples
Commit cd32f27dd5 ("bindings: rust: unify imports in examples") failed
to consistently unify the code across all examples so finish the
process.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250926-rust-release-tweaks-v1-1-beae932eb691@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-09-30 09:52:58 +02:00
Bartosz Golaszewski
d0006cc3d5 bindings: rust: update crate versions to v1.0.0
Ahead of the first stable release, update versions for all libgpiod
crates to v1.0.0.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250828-rust-1-0-0-release-v3-10-0f9c6c1c4808@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-09-01 10:23:50 +02:00
Bartosz Golaszewski
efc39de44a bindings: rust: update intmap dependency
Bump the intmap dependency for libgpiod to the most recent major version.
The IntMap type now takes two explicit type arguments for the key and
value so adjust the code accordingly. No longer require a specific minor
version.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250828-rust-1-0-0-release-v3-9-0f9c6c1c4808@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-09-01 10:23:50 +02:00
Bartosz Golaszewski
751a37fa37 bindings: rust: update thiserror dependency
Bump the thiserror dependency for libgpiod to the most recent major
version. No code changes required. No longer require a specific minor
version.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250828-rust-1-0-0-release-v3-8-0f9c6c1c4808@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-09-01 10:23:50 +02:00
Bartosz Golaszewski
4b9924ff31 bindings: rust: update system-deps dependency
Bump the system-deps dependency for libgpiod-sys to the most recent major
version. The way we iterate over libs has changed, we now need to convert
it explicitly to an iterable. No longer require a specific minor
version.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250828-rust-1-0-0-release-v3-7-0f9c6c1c4808@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-09-01 10:23:50 +02:00
Bartosz Golaszewski
00c224897d bindings: rust: update cc dependency
Bump the cc dependency for gpiosim-sys to the most recent version which
happens to be a minor upgrade from the one we have currently. However:
let cargo select it automatically by no longer specifying the minor
number. No code changes required.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250828-rust-1-0-0-release-v3-6-0f9c6c1c4808@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-09-01 10:23:50 +02:00
Bartosz Golaszewski
1430eb1e91 bindings: rust: update errno dependency
Bump the errno dependency for gpiosim-sys and libgpiod crates to the
most recent version which is a minor update from the one we have now.
However: let cargo select it automatically by not specifying the bugfix
version. We need to be stricter than just selecting the major version
alone because crates with major versions starting with 0 can introduce
breaking changes in minor releases. No code changes required.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250828-rust-1-0-0-release-v3-5-0f9c6c1c4808@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-09-01 10:23:50 +02:00
Bartosz Golaszewski
e40018f9cc bindings: rust: update bindgen dependency
Bump bindgen dependency in both gpiosim-sys and libgpiod-sys to the most
recent version. We now need to call CargoCallbacks::new() when boxing
the ParseCallbacks trait.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250828-rust-1-0-0-release-v3-4-0f9c6c1c4808@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-09-01 10:23:50 +02:00
Bartosz Golaszewski
ec4486fde1 bindings: rust: bump the minimum required rustc version
In order to satisfy the requirements of more recent versions of bindgen
and system-deps, bump the minimum required rustc version to v1.78.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250828-rust-1-0-0-release-v3-3-0f9c6c1c4808@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-09-01 10:23:50 +02:00
Bartosz Golaszewski
07b65bb307 bindings: rust: add missing unsafe block ahead of rust version bump
Rust edition 2024 changed the semantics of the unsafe keyword used as
function modifier[1]. Unsafe operations inside unsafe functions must
still be wrapped in an unsafe {} block. Update the code in case we want
to bump the rust version or check it with clippy --edition 2024.

[1] https://rust-lang.github.io/rfcs/2585-unsafe-block-in-unsafe-fn.html

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250828-rust-1-0-0-release-v3-2-0f9c6c1c4808@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-09-01 10:23:50 +02:00
Bartosz Golaszewski
fafd5e6180 bindings: rust: make Buffer::read_edge_events() lifetimes more explicit
Fix an issue pointed out by newer rustc version in the Buffer struct.
struct Events stores a reference to the buffer so tie the lifetime of the
latter to it explicitly.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250828-rust-1-0-0-release-v3-1-0f9c6c1c4808@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-09-01 10:23:50 +02:00
Bartosz Golaszewski
cd32f27dd5 bindings: rust: unify imports in examples
Some example source files avoid imports at the top of the file in favor
of overusing the namespaces in the code itself. The ones that do import
modules either group the imports or have a separate "use" in each line.
Let's unify the imports in example and use the brief approach of
grouping the modules inside use statements.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250723-rust-examples-imports-v1-2-4c87e07a9b79@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-07-24 11:26:41 +02:00
Bartosz Golaszewski
8da049f2d7 bindings: rust: fix formatting in examples
Use rustfmt to fix formatting of println!() in two examples. The lines
here don't need to be broken as they don't reach the character limit.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250723-rust-examples-imports-v1-1-4c87e07a9b79@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-07-24 11:26:41 +02:00
Erik Wierich
6263abf3ab bindings: rust: simplify format statements
clippy suggested these:

    warning: variables can be used directly in the `format!` string
       --> libgpiod/src/lib.rs:113:9
        |
    113 |         write!(f, "{:?}", self)
        |         ^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
        = note: `#[warn(clippy::uninlined_format_args)]` on by default
    help: change this to
        |
    113 -         write!(f, "{:?}", self)
    113 +         write!(f, "{self:?}")
        |

Generated with `cargo clippy --fix`.

The suggestions look like an improvement to me.

Signed-off-by: Erik Wierich <erik@riscstar.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Link: https://lore.kernel.org/r/20250721-rust-clippy-v1-2-2ac0198b2ea6@riscstar.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-07-21 14:01:19 +02:00
Erik Wierich
b20401360c bindings: rust: remove newline between attribute and attribute
These are block attributes, but the newline in between suggests that
they are for the whole module.

Fixes clippy warning:

    warning: empty line after outer attribute
     --> libgpiod-sys/src/lib.rs:6:1
      |
    6 | / #[cfg_attr(test, allow(deref_nullptr, non_snake_case))]
    7 | |
      | |_^
    8 |   mod bindings_raw {
      |   ---------------- the attribute applies to this module
      |
      = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_outer_attr
      = note: `#[warn(clippy::empty_line_after_outer_attr)]` on by default
      = help: if the empty line is unintentional, remove it
    help: if the attribute should apply to the crate use an inner attribute
      |
    5 ~ #![allow(non_camel_case_types, non_upper_case_globals)]
    6 ~ #![cfg_attr(test, allow(deref_nullptr, non_snake_case))]
      |

Signed-off-by: Erik Wierich <erik@riscstar.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Link: https://lore.kernel.org/r/20250721-rust-clippy-v1-1-2ac0198b2ea6@riscstar.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-07-21 14:01:19 +02:00
Erik Wierich
f1a949f827 bindings: rust: rename constructors that wrap raw objects to from_raw
This matches the stdlib conventions[1] and seems easier to read to me.

[1] https://doc.rust-lang.org/stable/std/?search=from_raw

Signed-off-by: Erik Wierich <erik@riscstar.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Link: https://lore.kernel.org/r/20250721-rust-unsafe-consistency-v1-2-aa1b42ed5983@riscstar.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-07-21 13:59:49 +02:00
Erik Wierich
1651710b37 bindings: rust: mark constructors that take raw pointers unsafe
The functions take raw pointers and expose safe API that operates on
them. Thus, invariants have to be upheld by these constructors to ensure
safe operation of later API.

Signed-off-by: Erik Wierich <erik@riscstar.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Link: https://lore.kernel.org/r/20250721-rust-unsafe-consistency-v1-1-aa1b42ed5983@riscstar.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-07-21 13:59:49 +02:00
Bartosz Golaszewski
92407a2cde bindings: rust: make Settings:new_with_settings() crate-private
This function takes a libgpiod raw pointer as argument, is not
documented and should not be part of the public API. Make it visible
only within the libgpiod crate.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Erik Wierich <erik@riscstar.com>
[Changed my name a month ago]
Link: https://lore.kernel.org/r/20250720-new-with-settings-crate-v1-1-a51392bd5b13@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-07-21 13:58:07 +02:00
Bartosz Golaszewski
b376eaa862 bindings: rust: tests: disable device before dropping the resources
Linux kernel commit 8bd76b3d3f3a ("gpio: sim: lock up configfs that an
instantiated device depends on") uncovered an issue in Rust bindings
tests where the GPIO simulator device is not disabled before removing its
configfs entries. Implenent Drop for the Sim struct in order to disable
the simulator first before dropping its bank and device objects.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Link: https://lore.kernel.org/r/20250203-fix-gpiosim-in-bindings-v1-3-d2c9e0f3a29d@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-05 10:49:51 +01:00
Erik Schilling
d6457b28e2 bindings: rust: libpgiod: fix clippy::empty_line_after_doc_comments lint
clippy warned about this:

    warning: empty line after doc comment
      --> libgpiod/src/event_buffer.rs:15:1
       |
    15 | / /// An iterator over the elements of type `Event`.
    16 | |
       | |_
    17 |   pub struct Events<'a> {
       |   --------------------- the comment documents this struct
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_doc_comments
       = note: `#[warn(clippy::empty_line_after_doc_comments)]` on by default
       = help: if the empty line is unintentional remove it

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Link: https://lore.kernel.org/r/20250103-lifetime-fix-v2-3-63902dc8cae1@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-01-03 16:40:58 +01:00
Erik Schilling
520f9c9419 bindings: rust: libgpiod: turn standalone doc comment into normal comment
This was complained about by clippy:

    warning: empty line after doc comment
       --> libgpiod/src/lib.rs:461:1
        |
    461 | / /// Various libgpiod-related functions.
    462 | |
        | |_
    ...
    467 |   pub fn is_gpiochip_device<P: AsRef<Path>>(path: &P) -> bool {
        |   ----------------------------------------------------------- the comment documents this function
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_doc_comments
        = help: if the empty line is unintentional remove it
    help: if the documentation should include the empty line include it in the comment
        |
    462 | ///
        |

The comment more seems to be of structural matter and should not be tied
to the function immediately below it. So lets turn it into a normal
non-doc comment.

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Link: https://lore.kernel.org/r/20250103-lifetime-fix-v2-2-63902dc8cae1@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-01-03 16:40:58 +01:00
Erik Schilling
314aa08822 bindings: rust: libgpiod: make read_edge_events lifetimes more explicit
Newer rustc versions warn on elided named lifetimes. In thise case, it
actually revealed a misleading function signature.

The function defined &self with 'a. But there is no dependency on the
Request reference that extends longer than this function call. Yet,
the code expresses such a dependency and abuses it to force the buffer
argument to match &self lifetime (and thus the return lifetime too!).

Instead we want to explicitly break up the tie to the &self and tie it
to the buffer instead.

The 'a on &self caused a 'a on the return. With that being removed we
would get an automatic '0. So we are now forced to add the 'a on the
return that also fixes the compiler warning.

Reported-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Co-authored-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/CAMRc=Me-QNmJ2L1K-gGzFtVZacsDiLsNUfh3QaWPdbVzyxUduA@mail.gmail.com/
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Link: https://lore.kernel.org/r/20250103-lifetime-fix-v2-1-63902dc8cae1@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-01-03 16:40:58 +01:00
Bartosz Golaszewski
2515fae57d bindings: rust: tests: set direction when reconfiguring lines
Linux kernel commit b44039638741 ("gpiolib: cdev: Ignore reconfiguration
without direction") made the direction setting mandatory for line config
passed to the kernel when reconfiguring requested lines. Fix the Rust
test cases which don't do it and now fail due to the rest of the
settings being ignored.

Reviewed-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20240708094858.85015-1-brgl@bgdev.pl
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-07-08 20:53:40 +02:00
Erik Schilling
9f5e653fb5 bindings: rust: libgpiod-sys: new release
During 86860fb ("bindings: rust: libgpiod: release 0.2.2"), I forgot that
we also need a libgpiod-sys release in order to expose the new feature
flag to raise the minimum libgpiod version.

Changelog:

7552e5d (bindings: rust: expose v2.1 features as flag, 2023-11-06)
bc91656 (bindings: rust: add wrapper.h to EXTRA_DIST, 2023-11-03)
2e6ee87 (bindings: rust: mention the libgpiod crate from libgpiod-sys, 2023-07-03)
d04639d (bindings: rust: bump MSRV to 1.60, 2023-06-16)
ebfed6c (bindings: rust: document build without install, 2023-06-12)
bce8623 (bindings: rust: exclude Makefile.am from package, 2023-06-12)
caabf53 (bindings: rust: add missing license and copyright boilerplate, 2023-06-13)

Most changes only touch the build scripts or modify packaging details.
Bumping the MSRV and introducing a new feature does not require a major
bump.

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-21 15:47:37 +01:00
Erik Schilling
86860fb65f bindings: rust: libgpiod: release 0.2.2
Minor update the exposes feature flag and bindings for v2.1.

Changelog:
==========

7552e5d (bindings: rust: expose v2.1 features as flag, 2023-11-06)
bc91656 (bindings: rust: add wrapper.h to EXTRA_DIST, 2023-11-03)
3b40a37 (bindings: rust: fix EXTRA_DIST for examples, 2023-11-03)

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-15 18:01:55 +01:00
Erik Schilling
7552e5d9ed bindings: rust: expose v2.1 features as flag
v2.1 provided a getter to read the chip name from a request.

This adds v2_1 as feature in order to raise the minimum requested version
to v2.1 for exposing the new API.

This is identical to the concept patch that was posted [1] when this
feature flag mechanism was proposed. Only the commit message was
reworded.

[1] https://lore.kernel.org/all/20231006-b4-bindings-old-version-fix-v1-2-a65f431afb97@linaro.org/

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-08 14:06:03 +01:00
Bartosz Golaszewski
bc91656de8 bindings: rust: add wrapper.h to EXTRA_DIST
We need to package wrapper.h in the release tarball so add it to
EXTRA_DIST.

Fixes: 1f80859530 ("bindings: rust: build against pkg-config info")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-03 14:01:27 +01:00
Bartosz Golaszewski
3b40a37e34 bindings: rust: fix EXTRA_DIST for examples
One of the examples listed in EXTRA_DIST is missing the .rs suffix while
another lists a file that doesn't exist. Fix both issues.

Fixes: aaed0f2e62 ("bindings: rust: examples: replace tools examples with use case examples")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-03 12:04:16 +01:00
Erik Schilling
22173b739e bindings: rust: libgpiod: release 0.2.1
While the release technically removes a function, that function was not
available in any released libgpiod version. Therefore, this only bumps
the minor.

This release fixes building with releases from the 2.x series of the C
lib.

Changelog:
=========

acebcf2 (bindings: rust: feature gate unreleased features, 2023-10-06)

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-11 10:16:12 +02:00
Erik Schilling
acebcf2cbe bindings: rust: feature gate unreleased features
`gpiod_line_request_get_chip_name()` is not released yet. Still, libgpiod-sys
will just happily generate bindings for it if it sees the definition in the
header file.

This guards the unreleased features behind an optional feature `vnext`.

To sketch the process of what happens once these features get into an
assumed "2.1" release:

libgpiod-sys will get updated with a `v2_1` feature. That feature would
then raise the minimum version that is attempted to query from pkg-
config. An identical feature will then be introduced on the `libgpiod`
crate and `vnext` guards will be changed to `v2_1` guards. The `vnext`
feature will then be updated to require the new `v2_1` feature.

Eventually, we will probably raise the minimum supported version for the
rust bindings and drop all the version gates before that.

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-10 08:55:07 +02:00
Erik Schilling
e7b02c2259 bindings: rust: libgpiod: release 0.2.0
Since we had some (potentially-)breaking changes, we bump the major for
the next release of the crate.

Note:
I am using the term "major" as defined in the Rust SemVer compatibility
guide [1], where the first non-zero digit is considered as "major".

[1] https://doc.rust-lang.org/cargo/reference/semver.html

Changelog:
=========

(potentially-)breaking changes:
  a29f3e6 (bindings: rust: rename {event,settings}_clone to try_clone, 2023-10-04)
  b290348 (bindings: rust: fix soundness of line_info modeling, 2023-10-03)
  d04639d (bindings: rust: bump MSRV to 1.60, 2023-06-16)

new functionality:
  808d15e (bindings: rust: allow cloning line::InfoRef -> line::Info, 2023-10-03)
  64aac85 (bindings: rust: mark all owning types as `Send`, 2023-09-28)
  d12ce74 (bindings: rust: provide LineRequest::chip_name(), 2023-07-20)
  53226d5 (bindings: rust: examples: add dedicated examples, 2023-06-14)

other changes:
  0a570b6 (bindings: rust: drop unneeded Arc within Chip, 2023-09-27)
  a97fe96 (bindings: rust: construct chip infos by reference, 2023-09-27)
  27afa47 (bindings: rust: remove useless clone, 2023-09-28)
  3f6e0bf (bindings: rust: add README.md for libgpiod crate, 2023-07-03)
  4b8357b (bindings: rust: clippy: silence false-positive on iterator, 2023-06-30)
  39189f0 (bindings: rust: clippy: drop unneeded conversions, 2023-06-30)
  46115fd (bindings: rust: clippy: silence false-positives on casts, 2023-06-30)
  901104e (bindings: rust: clippy: drop unnecessary casts, 2023-06-30)
  46ecbe0 (rust: examples: file comment consistency, 2023-06-24)
  aaed0f2 (bindings: rust: examples: replace tools examples with use case examples, 2023-06-23)
  b37bd9e (bindings: rust: examples: consistency cleanup, 2023-06-23)
  06c8ad9 (bindings: rust: package new examples in the distro tarball, 2023-06-15)

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-05 19:48:40 +02:00
Erik Schilling
a29f3e6957 bindings: rust: rename {event,settings}_clone to try_clone
What is getting cloned is already clear from the type. This also aligns
a bit better with similar methods from the `std` crate [1].

[1] https://doc.rust-lang.org/std/index.html?search=try_clone

Link: https://lore.kernel.org/r/CVUKC1HXG1P8.13XIUCCXN95F0@ablu-work
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-04 20:40:36 +02:00
Erik Schilling
808d15ebff bindings: rust: allow cloning line::InfoRef -> line::Info
While one would usually use the ToOwned [1] contract in rust, libgpipd's
API only allows copying that may fail.

Thus, we cannot implement the existing trait and roll our own method. I
went with `try_clone` since that seems to be used in similar cases across
the `std` crate [2].

It also closes the gap of not having any way to clone owned instances.
Though - again - not through the Clone trait which may not fail [3].

[1] https://doc.rust-lang.org/std/borrow/trait.ToOwned.html
[2] https://doc.rust-lang.org/std/index.html?search=try_clone
[3] https://doc.rust-lang.org/std/clone/trait.Clone.html

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-04 13:23:22 +02:00
Erik Schilling
b290348726 bindings: rust: fix soundness of line_info modeling
While attention was provided to prevent freeing in non-owned use-cases,
the lifetime of these object was not properly modeled.

The line_info from an event may only be used for as long as the event
exists.

This allowed us to write unsafe-free Rust code that causes a
use-after-free:

  let event = chip.read_info_event().unwrap();
  let line_info = event.line_info().unwrap();
  drop(event);
  dbg!(line_info.name().unwrap());

Which makes the AddressSanitizer scream:

  ==90154==ERROR: AddressSanitizer: heap-use-after-free on address 0x50b000005dc4 at pc 0x55a4f883a009 bp 0x7f60ac8fbbc0 sp 0x7f60ac8fb388
  READ of size 2 at 0x50b000005dc4 thread T2
      [...]
      #3 0x55a4f8c3d5f3 in libgpiod::line_info::Info::name::h5ba0bfd360ecb405 libgpiod/bindings/rust/libgpiod/src/line_info.rs:70:18
    	[...]

  0x50b000005dc4 is located 4 bytes inside of 112-byte region [0x50b000005dc0,0x50b000005e30)
  freed by thread T2 here:
      [...]
      #1 0x7f60b07f7e31 in gpiod_info_event_free libgpiod/lib/info-event.c:61:2
      [...]

  previously allocated by thread T2 here:
      #0 0x55a4f88b04be in malloc /rustc/llvm/src/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:69:3
      #1 0x7f60b07f8ff0 in gpiod_line_info_from_uapi libgpiod/lib/line-info.c:144:9

The fix is to distinguish between the owned and non-owned variants and
assigning lifetimes to non-owned variants.

For modeling the non-owned type there are a couple of options. The ideal
solution would be using extern_types [1]. But that is still unstable.
Instead, we are defining a #[repr(transparent)] wrapper around the opaque
gpiod_line_info struct and cast the pointer to a reference.

This was recommended on the Rust Discord server as good practise.
(Thanks to Kyuuhachi, shepmaster, pie_flavor and ilyvion! Also thanks to
@epilys for a brainstorming on this on #linaro-virtualization IRC).

Of course, determining the lifetimes and casting across the types
requires some care. So this adds a couple of SAFETY comments that would
probably also have helped the existing code.

[1] https://github.com/rust-lang/rfcs/blob/master/text/1861-extern-types.md

Fixes: 91f9373c65 ("bindings: rust: Add libgpiod crate")
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-04 13:17:50 +02:00
Erik Schilling
64aac85b59 bindings: rust: mark all owning types as Send
The thread-safety rules of libgpiod allow individual object instances to
be used from different threads. So far, this was not actually possible
with the Rust bindings. Not being `Send` disallowed the user to transfer
the ownership to different threads.

Rust also has a `Sync` marker. That one would even allow sending
references of objects to other threads. Since we wrap a lot of C
functions with `fn foo(&self)` signatures, that would not be safe.
libgpiod does not allow concurrent API calls to the same object instance
- which Rust would allow for read-only references. Thus, we do not
define that one.

Chip was already modeled correctly.

line::Info is not marked as Send since it may either be owning or non-
owning. That problem is fixed as part of a separate pull request [1].

[1] https://lore.kernel.org/r/20230927-rust-line-info-soundness-v1-0-990dce6f18ab@linaro.org

Link: https://lore.kernel.org/r/CVHO091CC80Y.3KUOSLSOBVL0T@ablu-work
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-02 09:15:36 +02:00
Erik Schilling
0a570b6d5e bindings: rust: drop unneeded Arc within Chip
Chip was modeled with an Arc that only was used to pass the chip pointer
to the chip::Info constructor. With that refactored to take a reference,
we can just drop the Arc.

This allows to get rid of the `Internal` helper struct that was only
required by the Arc.

As a side-effect, we also get rid of this clippy warning:

  warning: usage of an `Arc` that is not `Send` or `Sync`
    --> libgpiod/src/chip.rs:75:21
     |
  75 |         let ichip = Arc::new(Internal::open(path)?);
     |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |
     = note: the trait `Send` is not implemented for `Internal`
     = note: the trait `Sync` is not implemented for `Internal`
     = note: required for `Arc<Internal>` to implement `Send` and `Sync`
     = help: consider using an `Rc` instead or wrapping the inner type with a `Mutex`

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-09-29 14:46:08 +02:00
Erik Schilling
a97fe96542 bindings: rust: construct chip infos by reference
No need to clone the Arc for this. A simple reference is enough to get
to the underlying chip pointer.

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-09-29 14:45:55 +02:00
Erik Schilling
27afa47fcb bindings: rust: remove useless clone
Reported by 1.74.0-nightly:

  warning: call to `.clone()` on a reference in this situation does nothing
    --> libgpiod/tests/line_request.rs:71:44
     |
  71 |             let chip_name = sim.chip_name().clone();
     |                                            ^^^^^^^^ help: remove this redundant call
     |
     = note: the type `str` does not implement `Clone`, so calling `clone` on `&str` copies the reference, which does not do anything and can be removed
     = note: `#[warn(noop_method_call)]` on by default

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-09-29 14:33:49 +02:00
Bartosz Golaszewski
d12ce74a91 bindings: rust: provide LineRequest::chip_name()
Provide a wrapper around gpiod_line_request_get_chip_name() for Rust
bindings and add a test-case.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Erik Schilling <erik.schilling@linaro.org>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
2023-07-22 14:29:04 +02:00
Erik Schilling
2e6ee87072 bindings: rust: mention the libgpiod crate from libgpiod-sys
If people view this README.md on crates.io [1], they likely want to
use the safe wrapper instead. So this hints the existence of that other
crate.

[1] https://crates.io/crates/libgpiod-sys

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-07-03 12:48:22 +02:00
Erik Schilling
3f6e0bfbee bindings: rust: add README.md for libgpiod crate
crates.io treats the README as landing page for a crate [1]. Since
we have none, it currently displays a blank page. Lets add at least a
little bit of info here so people can figure out what they are dealing
with.

[1] https://crates.io/crates/libgpiod

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-07-03 12:47:53 +02:00
Erik Schilling
4b8357b368 bindings: rust: clippy: silence false-positive on iterator
This was fixed on clippy master [1], but it is still broken on 0.1.70. So
lets silence it until the clippy fix is widely available.

clippy version: clippy 0.1.70 (90c5418 2023-05-31).

[1] 9fa4089410

Reported-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20230612154055.56556-1-warthog618@gmail.com
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-06-30 14:10:30 +02:00
Erik Schilling
39189f0bf5 bindings: rust: clippy: drop unneeded conversions
Fixes clippy warnings on these lines.

Applied the suggested fix using:

    cargo clippy --fix

clippy version: clippy 0.1.70 (90c5418 2023-05-31).

Reported-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20230612154055.56556-1-warthog618@gmail.com
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-06-30 14:10:27 +02:00
Erik Schilling
46115fddd7 bindings: rust: clippy: silence false-positives on casts
clippy falsely complains about these lines. The problem is known, but
unfixed [1]. So lets silence the warning until a fix is widely available.

clippy version: clippy 0.1.70 (90c5418 2023-05-31).

[1] https://github.com/rust-lang/rust-clippy/issues/10555

Reported-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20230612154055.56556-1-warthog618@gmail.com
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-06-30 14:10:24 +02:00
Erik Schilling
901104ec2f bindings: rust: clippy: drop unnecessary casts
Fixes clippy warnings on these lines.

Applied the suggested fix using:

    cargo clippy --fix

clippy version: clippy 0.1.70 (90c5418 2023-05-31).

Reported-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20230612154055.56556-1-warthog618@gmail.com
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-06-30 14:10:20 +02:00
Kent Gibson
72cd9898ed bindings: rust: fix unclear resolver warning
Fix the following warning:

"some crates are on edition 2021 which defaults to `resolver = "2"`,
 but virtual workspaces default to `resolver = "1"`"

Clarify the resolver selection as the 2021 edition by setting the
workspace.resolver to "2".

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Reviewed-by: Erik Schilling <erik.schilling@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-06-30 14:01:29 +02:00
Kent Gibson
46ecbe0f15 rust: examples: file comment consistency
Make the file comment for reconfigure_input_to_output consistent with
other language examples.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-06-26 10:06:48 +02:00
Kent Gibson
aaed0f2e62 bindings: rust: examples: replace tools examples with use case examples
Replace tool examples with use case examples drawn from the tools,
gpio_events example with buffered_event_lifetimes, and
gpio_threaded_info_events with reconfigure_input_to_output.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Reviewed-by: Erik Schilling <erik.schilling@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-06-23 21:32:58 +02:00