Skip to contentSkip to conversation

libgpiod: Add Rust bindings

PATCH Superseded Viresh Kumar 2 participants 11 messages Aug 2, 2022 – Aug 9, 2022 GPIO

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v5 cover libgpiod: Add Rust bindings

Hello,

Here is another version of rust bindings for libgpiod v2.0, based of the next/libgpiod-2.0.

Pushed here:

https://github.com/vireshk/libgpiod master

Kent, I hope I haven't missed any of your comments, there were too many of them. Thanks for your review.

V4->V5:
- Arrange as workspace with crates for libgpiod-sys, libgpiod, gpiosim.
- Use static libgpiod and libgpiosim libraries instead of rebuilding again.
- Arrange in modules instead of flattened approach.
- New enums like Setting and SettingKind and new types based on them SettingMap and SettingKindMap.
- New property independent helpers for line_config, like set_prop_default().
- Improved tests/examples, new example for gpiowatch.
- Add pre-built bindings for gpiosim too.
- Many other changes.

V3->V4:
- Rebased on top of new changes, and made changes accordingly.
- Added rust integration tests with gpiosim.
- Found a kernel bug with tests, sent a patch for that to LKML.

V2->V3:
- Remove naming redundancy, users just need to do this now
use libgpiod:{Chip, Direction, LineConfig} now (Bartosz);
- Fix lifetime issues between event-buffer and edge-event modules, the event buffer is released after the last edge-event reference is dropped (Bartosz).
- Allow edge-event to be copied, and freed later (Bartosz).
- Add two separate rust crates, sys and wrapper (Gerard).
- Null-terminate the strings passed to libgpiod (Wedson).
- Drop unnecessary checks to validate string returned from chip:name/label/path.
- Fix SAFETY comments (Wedson).
- Drop unnecessary clone() instances (Bartosz).

V1->V2:
- Added examples (I tested everything except gpiomon.rs, didn't have right hardware/mock device to test).
- Build rust bindings as part of Make, update documentation.

Thanks.

--
Viresh

Viresh Kumar (8):
libgpiod: Add libgpiod-sys rust crate
libgpiod-sys: Add pre generated rust bindings
libgpiod: Add rust wrapper crate
libgpiod: Add rust examples
libgpiod: Add gpiosim rust crate
gpiosim: Add pre generated rust bindings
libgpiod: Add rust tests
libgpiod: Integrate building of rust bindings with make

 .gitignore                                    |    5 +
 README                                        |    8 +-
 TODO                                          |    8 -
 bindings/Makefile.am                          |    6 +
 bindings/rust/Cargo.toml                      |    7 +
 bindings/rust/Makefile.am                     |   18 +
 bindings/rust/gpiosim/Cargo.toml              |   18 +
 bindings/rust/gpiosim/README.md               |   11 +
 bindings/rust/gpiosim/build.rs                |   43 +
 bindings/rust/gpiosim/src/bindings.rs         |  180 ++
 bindings/rust/gpiosim/src/lib.rs              |   25 +
 bindings/rust/gpiosim/src/sim.rs              |  323 ++++
 bindings/rust/libgpiod-sys/Cargo.toml         |   15 +
 bindings/rust/libgpiod-sys/README.md          |   11 +
 bindings/rust/libgpiod-sys/build.rs           |   41 +
 bindings/rust/libgpiod-sys/src/bindings.rs    | 1535 +++++++++++++++++
 bindings/rust/libgpiod-sys/src/lib.rs         |   13 +
 bindings/rust/libgpiod/Cargo.toml             |   16 +
 bindings/rust/libgpiod/examples/gpiodetect.rs |   30 +
 bindings/rust/libgpiod/examples/gpiofind.rs   |   35 +
 bindings/rust/libgpiod/examples/gpioget.rs    |   44 +
 bindings/rust/libgpiod/examples/gpioinfo.rs   |   95 +
 bindings/rust/libgpiod/examples/gpiomon.rs    |   71 +
 bindings/rust/libgpiod/examples/gpioset.rs    |   64 +
 bindings/rust/libgpiod/examples/gpiowatch.rs  |   54 +
 bindings/rust/libgpiod/src/chip.rs            |  253 +++
 bindings/rust/libgpiod/src/edge_event.rs      |  102 ++
 bindings/rust/libgpiod/src/event_buffer.rs    |   90 +
 bindings/rust/libgpiod/src/info_event.rs      |   68 +
 bindings/rust/libgpiod/src/lib.rs             |  477 +++++
 bindings/rust/libgpiod/src/line_config.rs     |  586 +++++++
 bindings/rust/libgpiod/src/line_info.rs       |  180 ++
 bindings/rust/libgpiod/src/line_request.rs    |  246 +++
 bindings/rust/libgpiod/src/request_config.rs  |  119 ++
 bindings/rust/libgpiod/tests/chip.rs          |   97 ++
 bindings/rust/libgpiod/tests/common/config.rs |  142 ++
 bindings/rust/libgpiod/tests/common/mod.rs    |   10 +
 bindings/rust/libgpiod/tests/edge_event.rs    |  294 ++++
 bindings/rust/libgpiod/tests/info_event.rs    |  120 ++
 bindings/rust/libgpiod/tests/line_config.rs   |  293 ++++
 bindings/rust/libgpiod/tests/line_info.rs     |  272 +++
 bindings/rust/libgpiod/tests/line_request.rs  |  473 +++++
 .../rust/libgpiod/tests/request_config.rs     |   42 +
 configure.ac                                  |   16 +
 44 files changed, 6545 insertions(+), 11 deletions(-)
 create mode 100644 bindings/rust/Cargo.toml
 create mode 100644 bindings/rust/Makefile.am
 create mode 100644 bindings/rust/gpiosim/Cargo.toml
 create mode 100644 bindings/rust/gpiosim/README.md
 create mode 100644 bindings/rust/gpiosim/build.rs
 create mode 100644 bindings/rust/gpiosim/src/bindings.rs
 create mode 100644 bindings/rust/gpiosim/src/lib.rs
 create mode 100644 bindings/rust/gpiosim/src/sim.rs
 create mode 100644 bindings/rust/libgpiod-sys/Cargo.toml
 create mode 100644 bindings/rust/libgpiod-sys/README.md
 create mode 100644 bindings/rust/libgpiod-sys/build.rs
 create mode 100644 bindings/rust/libgpiod-sys/src/bindings.rs
 create mode 100644 bindings/rust/libgpiod-sys/src/lib.rs
 create mode 100644 bindings/rust/libgpiod/Cargo.toml
 create mode 100644 bindings/rust/libgpiod/examples/gpiodetect.rs
 create mode 100644 bindings/rust/libgpiod/examples/gpiofind.rs
 create mode 100644 bindings/rust/libgpiod/examples/gpioget.rs
 create mode 100644 bindings/rust/libgpiod/examples/gpioinfo.rs
 create mode 100644 bindings/rust/libgpiod/examples/gpiomon.rs
 create mode 100644 bindings/rust/libgpiod/examples/gpioset.rs
 create mode 100644 bindings/rust/libgpiod/examples/gpiowatch.rs
 create mode 100644 bindings/rust/libgpiod/src/chip.rs
 create mode 100644 bindings/rust/libgpiod/src/edge_event.rs
 create mode 100644 bindings/rust/libgpiod/src/event_buffer.rs
 create mode 100644 bindings/rust/libgpiod/src/info_event.rs
 create mode 100644 bindings/rust/libgpiod/src/lib.rs
 create mode 100644 bindings/rust/libgpiod/src/line_config.rs
 create mode 100644 bindings/rust/libgpiod/src/line_info.rs
 create mode 100644 bindings/rust/libgpiod/src/line_request.rs
 create mode 100644 bindings/rust/libgpiod/src/request_config.rs
 create mode 100644 bindings/rust/libgpiod/tests/chip.rs
 create mode 100644 bindings/rust/libgpiod/tests/common/config.rs
 create mode 100644 bindings/rust/libgpiod/tests/common/mod.rs
 create mode 100644 bindings/rust/libgpiod/tests/edge_event.rs
 create mode 100644 bindings/rust/libgpiod/tests/info_event.rs
 create mode 100644 bindings/rust/libgpiod/tests/line_config.rs
 create mode 100644 bindings/rust/libgpiod/tests/line_info.rs
 create mode 100644 bindings/rust/libgpiod/tests/line_request.rs
 create mode 100644 bindings/rust/libgpiod/tests/request_config.rs
Signature · 1 line
2.31.1.272.g89b43f80a514
#1 lore raw

PATCH v5 1/8 libgpiod: Add libgpiod-sys rust crate

This adds libgpiod-sys rust crate, which provides FFI (foreign function interface) bindings for libgpiod APIs.

---
 .gitignore                            |  5 ++++
 bindings/rust/Cargo.toml              |  5 ++++
 bindings/rust/libgpiod-sys/Cargo.toml | 15 ++++++++++
 bindings/rust/libgpiod-sys/build.rs   | 41 +++++++++++++++++++++++++++
 bindings/rust/libgpiod-sys/src/lib.rs | 13 +++++++++
 5 files changed, 79 insertions(+)
 create mode 100644 bindings/rust/Cargo.toml
 create mode 100644 bindings/rust/libgpiod-sys/Cargo.toml
 create mode 100644 bindings/rust/libgpiod-sys/build.rs
 create mode 100644 bindings/rust/libgpiod-sys/src/lib.rs
.gitignore +5 -0
index 58e1c5fc7e00..9541482d5efb 100644
@@ -33,3 +33,8 @@ stamp-h1
 # profiling
 *.gcda
 *.gcno
+
+# Added by cargo
+
+target
+Cargo.lock
bindings/rust/Cargo.toml new file +5 -0
new file mode 100644
index 000000000000..d0b3a3c88ff1
@@ -0,0 +1,5 @@
+[workspace]
+
+members = [
+    "libgpiod-sys",
+]
bindings/rust/libgpiod-sys/Cargo.toml new file +15 -0
new file mode 100644
index 000000000000..77f82719d269
@@ -0,0 +1,15 @@
+[package]
+name = "libgpiod-sys"
+version = "0.1.0"
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+
+[features]
+generate = [ "bindgen" ]
+
+[build-dependencies]
+bindgen = { version = "0.59.1", optional = true }
+cc = "1.0.46"
bindings/rust/libgpiod-sys/build.rs new file +41 -0

41 lines not on this page · raw message

bindings/rust/libgpiod-sys/src/lib.rs new file +13 -0
new file mode 100644
index 000000000000..1ca355b5f5ac
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#[allow(non_camel_case_types, non_upper_case_globals)]
+#[cfg_attr(test, allow(deref_nullptr, non_snake_case))]
+
+mod bindings_raw {
+    #[cfg(feature = "generate")]
+    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
+
+    #[cfg(not(feature = "generate"))]
+    include!("bindings.rs");
+}
+pub use bindings_raw::*;
Signature · 1 line
2.31.1.272.g89b43f80a514
#2 lore raw

PATCH v5 2/8 libgpiod-sys: Add pre generated rust bindings

In reply to Viresh Kumar

This adds a copy of pre generated bindings and adds the suggested way of updating those in README.

---
 bindings/rust/libgpiod-sys/README.md       |   11 +
 bindings/rust/libgpiod-sys/src/bindings.rs | 1535 ++++++++++++++++++++
 2 files changed, 1546 insertions(+)
 create mode 100644 bindings/rust/libgpiod-sys/README.md
 create mode 100644 bindings/rust/libgpiod-sys/src/bindings.rs
bindings/rust/libgpiod-sys/README.md new file +11 -0
new file mode 100644
index 000000000000..ecf75b31c41e
@@ -0,0 +1,11 @@
+# Generated libgpiod-sys Rust FFI bindings
+Automatically generated Rust FFI bindings via
+	[bindgen](https://github.com/rust-lang/rust-bindgen).
+
+## Updating bindings
+1. Clone the source from
+	<https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/>
+2. run `cd libgpiod/bindings/rust/libgpiod-sys/`
+2. run `cargo build --features generate`
+3. Copy the bindings 'cp ../target/debug/build/libgpiod-sys-###/out/bindings.rs src/bindings.rs'
+4. Commit changes in `src/bindings.rs`
bindings/rust/libgpiod-sys/src/bindings.rs new file +1535 -0

1,535 lines not on this page · raw message

Signature · 1 line
2.31.1.272.g89b43f80a514
#3 lore raw

PATCH v5 3/8 libgpiod: Add rust wrapper crate

In reply to Viresh Kumar

Add rust wrapper crate, around the libpiod-sys crate added earlier, to provide a convenient interface for the users.

---
 bindings/rust/Cargo.toml                     |   1 +
 bindings/rust/libgpiod/Cargo.toml            |  13 +
 bindings/rust/libgpiod/src/chip.rs           | 253 ++++++++
 bindings/rust/libgpiod/src/edge_event.rs     | 102 ++++
 bindings/rust/libgpiod/src/event_buffer.rs   |  90 +++
 bindings/rust/libgpiod/src/info_event.rs     |  68 +++
 bindings/rust/libgpiod/src/lib.rs            | 477 +++++++++++++++
 bindings/rust/libgpiod/src/line_config.rs    | 586 +++++++++++++++++++
 bindings/rust/libgpiod/src/line_info.rs      | 180 ++++++
 bindings/rust/libgpiod/src/line_request.rs   | 246 ++++++++
 bindings/rust/libgpiod/src/request_config.rs | 119 ++++
 11 files changed, 2135 insertions(+)
 create mode 100644 bindings/rust/libgpiod/Cargo.toml
 create mode 100644 bindings/rust/libgpiod/src/chip.rs
 create mode 100644 bindings/rust/libgpiod/src/edge_event.rs
 create mode 100644 bindings/rust/libgpiod/src/event_buffer.rs
 create mode 100644 bindings/rust/libgpiod/src/info_event.rs
 create mode 100644 bindings/rust/libgpiod/src/lib.rs
 create mode 100644 bindings/rust/libgpiod/src/line_config.rs
 create mode 100644 bindings/rust/libgpiod/src/line_info.rs
 create mode 100644 bindings/rust/libgpiod/src/line_request.rs
 create mode 100644 bindings/rust/libgpiod/src/request_config.rs
bindings/rust/Cargo.toml +1 -0
index d0b3a3c88ff1..1e57ef2c0002 100644
@@ -2,4 +2,5 @@
 
 members = [
     "libgpiod-sys",
+    "libgpiod"
 ]
bindings/rust/libgpiod/Cargo.toml new file +13 -0
new file mode 100644
index 000000000000..f25242abb153
@@ -0,0 +1,13 @@
+[package]
+name = "libgpiod"
+version = "0.1.0"
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+intmap = "2.0.0"
+libc = ">=0.2.39"
+libgpiod-sys = { path = "../libgpiod-sys" }
+thiserror = "1.0"
+vmm-sys-util = "=0.10.0"
bindings/rust/libgpiod/src/chip.rs new file +253 -0

253 lines not on this page · raw message

bindings/rust/libgpiod/src/edge_event.rs new file +102 -0

102 lines not on this page · raw message

bindings/rust/libgpiod/src/event_buffer.rs new file +90 -0

90 lines not on this page · raw message

bindings/rust/libgpiod/src/info_event.rs new file +68 -0

68 lines not on this page · raw message

bindings/rust/libgpiod/src/lib.rs new file +477 -0

477 lines not on this page · raw message

bindings/rust/libgpiod/src/line_config.rs new file +586 -0

586 lines not on this page · raw message

bindings/rust/libgpiod/src/line_info.rs new file +180 -0

180 lines not on this page · raw message

bindings/rust/libgpiod/src/line_request.rs new file +246 -0

246 lines not on this page · raw message

bindings/rust/libgpiod/src/request_config.rs new file +119 -0

119 lines not on this page · raw message

Signature · 1 line
2.31.1.272.g89b43f80a514
#4 lore raw

PATCH v5 4/8 libgpiod: Add rust examples

In reply to Viresh Kumar

Add examples for the usage of the rust bindings, quite similar to the ones in cxx bindings.

---
 bindings/rust/libgpiod/examples/gpiodetect.rs | 30 ++++++
 bindings/rust/libgpiod/examples/gpiofind.rs   | 35 +++++++
 bindings/rust/libgpiod/examples/gpioget.rs    | 44 +++++++++
 bindings/rust/libgpiod/examples/gpioinfo.rs   | 95 +++++++++++++++++++
 bindings/rust/libgpiod/examples/gpiomon.rs    | 71 ++++++++++++++
 bindings/rust/libgpiod/examples/gpioset.rs    | 64 +++++++++++++
 bindings/rust/libgpiod/examples/gpiowatch.rs  | 54 +++++++++++
 7 files changed, 393 insertions(+)
 create mode 100644 bindings/rust/libgpiod/examples/gpiodetect.rs
 create mode 100644 bindings/rust/libgpiod/examples/gpiofind.rs
 create mode 100644 bindings/rust/libgpiod/examples/gpioget.rs
 create mode 100644 bindings/rust/libgpiod/examples/gpioinfo.rs
 create mode 100644 bindings/rust/libgpiod/examples/gpiomon.rs
 create mode 100644 bindings/rust/libgpiod/examples/gpioset.rs
 create mode 100644 bindings/rust/libgpiod/examples/gpiowatch.rs
bindings/rust/libgpiod/examples/gpiodetect.rs new file +30 -0
new file mode 100644
index 000000000000..f24ac72e2d48
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
+//
+// Copyright 2022 Linaro Ltd. All Rights Reserved.
+//     Viresh Kumar <[email protected]>
+//
+// Simplified Rust implementation of gpiodetect tool.
+
+use std::env;
+use std::path::Path;
+
+use libgpiod::{gpiochip_devices, Error, Result};
+
+fn main() -> Result<()> {
+    let args: Vec<String> = env::args().collect();
+    if args.len() > 1 {
+        println!("Usage: {}", args[0]);
+        return Err(Error::InvalidArguments);
+    }
+
+    for chip in gpiochip_devices(&Path::new("/dev"))? {
+        println!(
+            "{} [{}] ({})",
+            chip.name()?,
+            chip.label()?,
+            chip.num_lines(),
+        );
+    }
+
+    Ok(())
+}
bindings/rust/libgpiod/examples/gpiofind.rs new file +35 -0
new file mode 100644
index 000000000000..07e886bc3896
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
+//
+// Copyright 2022 Linaro Ltd. All Rights Reserved.
+//     Viresh Kumar <[email protected]>
+//
+// Simplified Rust implementation of gpiofind tool.
+
+use std::env;
+use std::path::Path;
+
+use libgpiod::{gpiochip_devices, Error, Result};
+
+fn main() -> Result<()> {
+    let args: Vec<String> = env::args().collect();
+    if args.len() != 2 {
+        println!("Usage: {} <line-name>", args[0]);
+        return Err(Error::InvalidArguments);
+    }
+
+    for chip in gpiochip_devices(&Path::new("/dev"))? {
+        let offset = chip.line_offset_from_name(&args[1]);
+        if offset.is_ok() {
+            println!(
+                "Line {} found: Chip: {}, offset: {}",
+                args[1],
+                chip.name()?,
+                offset?
+            );
+            return Ok(());
+        }
+    }
+
+    println!("Failed to find line: {}", args[1]);
+    Ok(())
+}
bindings/rust/libgpiod/examples/gpioget.rs new file +44 -0

44 lines not on this page · raw message

bindings/rust/libgpiod/examples/gpioinfo.rs new file +95 -0

95 lines not on this page · raw message

bindings/rust/libgpiod/examples/gpiomon.rs new file +71 -0

71 lines not on this page · raw message

bindings/rust/libgpiod/examples/gpioset.rs new file +64 -0

64 lines not on this page · raw message

bindings/rust/libgpiod/examples/gpiowatch.rs new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.31.1.272.g89b43f80a514
#5 lore raw

PATCH v5 5/8 libgpiod: Add gpiosim rust crate

In reply to Viresh Kumar

This adds gpiosim rust crate, which provides helpers to emulate GPIO chips.

---
 bindings/rust/Cargo.toml         |   3 +-
 bindings/rust/gpiosim/Cargo.toml |  18 ++
 bindings/rust/gpiosim/build.rs   |  43 ++++
 bindings/rust/gpiosim/src/lib.rs |  25 +++
 bindings/rust/gpiosim/src/sim.rs | 323 +++++++++++++++++++++++++++++++
 5 files changed, 411 insertions(+), 1 deletion(-)
 create mode 100644 bindings/rust/gpiosim/Cargo.toml
 create mode 100644 bindings/rust/gpiosim/build.rs
 create mode 100644 bindings/rust/gpiosim/src/lib.rs
 create mode 100644 bindings/rust/gpiosim/src/sim.rs
bindings/rust/Cargo.toml +2 -1
index 1e57ef2c0002..8721bc610b86 100644
@@ -2,5 +2,6 @@
 
 members = [
     "libgpiod-sys",
-    "libgpiod"
+    "libgpiod",
+    "gpiosim"
 ]
bindings/rust/gpiosim/Cargo.toml new file +18 -0
new file mode 100644
index 000000000000..d6b4cc34339b
@@ -0,0 +1,18 @@
+[package]
+name = "gpiosim"
+version = "0.1.0"
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+libc = ">=0.2.39"
+libgpiod = { path = "../libgpiod" }
+vmm-sys-util = "=0.10.0"
+
+[features]
+generate = [ "bindgen" ]
+
+[build-dependencies]
+bindgen = { version = "0.59.1", optional = true }
+cc = "1.0.46"
bindings/rust/gpiosim/build.rs new file +43 -0

43 lines not on this page · raw message

bindings/rust/gpiosim/src/lib.rs new file +25 -0
new file mode 100644
index 000000000000..94d0ddb38e0f
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#[allow(non_camel_case_types, non_upper_case_globals)]
+#[cfg_attr(test, allow(deref_nullptr, non_snake_case))]
+#[allow(dead_code)]
+mod bindings_raw {
+    #[cfg(feature = "generate")]
+    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
+
+    #[cfg(not(feature = "generate"))]
+    include!("bindings.rs");
+}
+
+use bindings_raw::*;
+
+pub use bindings_raw::{
+    GPIOSIM_HOG_DIR_INPUT, GPIOSIM_HOG_DIR_OUTPUT_HIGH, GPIOSIM_HOG_DIR_OUTPUT_LOW,
+    GPIOSIM_PULL_DOWN, GPIOSIM_PULL_UP, GPIOSIM_VALUE_ACTIVE, GPIOSIM_VALUE_INACTIVE,
+};
+
+#[allow(dead_code)]
+mod sim;
+
+#[allow(unused_imports)]
+pub use sim::*;
bindings/rust/gpiosim/src/sim.rs new file +323 -0

323 lines not on this page · raw message

Signature · 1 line
2.31.1.272.g89b43f80a514
#6 lore raw

PATCH v5 6/8 gpiosim: Add pre generated rust bindings

In reply to Viresh Kumar

This adds a copy of pre generated bindings and adds the suggested way of updating those in README.

---
 bindings/rust/gpiosim/README.md       |  11 ++
 bindings/rust/gpiosim/src/bindings.rs | 180 ++++++++++++++++++++++++++
 2 files changed, 191 insertions(+)
 create mode 100644 bindings/rust/gpiosim/README.md
 create mode 100644 bindings/rust/gpiosim/src/bindings.rs
bindings/rust/gpiosim/README.md new file +11 -0
new file mode 100644
index 000000000000..acb84543dbdc
@@ -0,0 +1,11 @@
+# Generated gpiosim Rust FFI bindings
+Automatically generated Rust FFI bindings via
+	[bindgen](https://github.com/rust-lang/rust-bindgen).
+
+## Updating bindings
+1. Clone the source from
+	<https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/>
+2. run `cd libgpiod/bindings/rust/gpiosim/`
+2. run `cargo build --features generate`
+3. Copy the bindings 'cp ../target/debug/build/gpiosim-###/out/bindings.rs src/bindings.rs'
+4. Commit changes in `src/bindings.rs`
bindings/rust/gpiosim/src/bindings.rs new file +180 -0

180 lines not on this page · raw message

Signature · 1 line
2.31.1.272.g89b43f80a514
#7 lore raw

PATCH v5 7/8 libgpiod: Add rust tests

In reply to Viresh Kumar

Add tests for the rust bindings, quite similar to the ones in cxx bindings.

---
 bindings/rust/libgpiod/Cargo.toml             |   3 +
 bindings/rust/libgpiod/tests/chip.rs          |  97 ++++
 bindings/rust/libgpiod/tests/common/config.rs | 142 ++++++
 bindings/rust/libgpiod/tests/common/mod.rs    |  10 +
 bindings/rust/libgpiod/tests/edge_event.rs    | 294 +++++++++++
 bindings/rust/libgpiod/tests/info_event.rs    | 120 +++++
 bindings/rust/libgpiod/tests/line_config.rs   | 293 +++++++++++
 bindings/rust/libgpiod/tests/line_info.rs     | 272 ++++++++++
 bindings/rust/libgpiod/tests/line_request.rs  | 473 ++++++++++++++++++
 .../rust/libgpiod/tests/request_config.rs     |  42 ++
 10 files changed, 1746 insertions(+)
 create mode 100644 bindings/rust/libgpiod/tests/chip.rs
 create mode 100644 bindings/rust/libgpiod/tests/common/config.rs
 create mode 100644 bindings/rust/libgpiod/tests/common/mod.rs
 create mode 100644 bindings/rust/libgpiod/tests/edge_event.rs
 create mode 100644 bindings/rust/libgpiod/tests/info_event.rs
 create mode 100644 bindings/rust/libgpiod/tests/line_config.rs
 create mode 100644 bindings/rust/libgpiod/tests/line_info.rs
 create mode 100644 bindings/rust/libgpiod/tests/line_request.rs
 create mode 100644 bindings/rust/libgpiod/tests/request_config.rs
bindings/rust/libgpiod/Cargo.toml +3 -0
index f25242abb153..8abcaacdee67 100644
@@ -11,3 +11,6 @@ libc = ">=0.2.39"
 libgpiod-sys = { path = "../libgpiod-sys" }
 thiserror = "1.0"
 vmm-sys-util = "=0.10.0"
+
+[dev-dependencies]
+gpiosim = { path = "../gpiosim" }
bindings/rust/libgpiod/tests/chip.rs new file +97 -0

97 lines not on this page · raw message

bindings/rust/libgpiod/tests/common/config.rs new file +142 -0

142 lines not on this page · raw message

bindings/rust/libgpiod/tests/common/mod.rs new file +10 -0
new file mode 100644
index 000000000000..8904f7121b4c
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
+//
+// Copyright 2022 Linaro Ltd. All Rights Reserved.
+//     Viresh Kumar <[email protected]>
+
+#[allow(dead_code)]
+mod config;
+
+#[allow(unused_imports)]
+pub(crate) use config::*;
bindings/rust/libgpiod/tests/edge_event.rs new file +294 -0

294 lines not on this page · raw message

bindings/rust/libgpiod/tests/info_event.rs new file +120 -0

120 lines not on this page · raw message

bindings/rust/libgpiod/tests/line_config.rs new file +293 -0

293 lines not on this page · raw message

bindings/rust/libgpiod/tests/line_info.rs new file +272 -0

272 lines not on this page · raw message

bindings/rust/libgpiod/tests/line_request.rs new file +473 -0

473 lines not on this page · raw message

bindings/rust/libgpiod/tests/request_config.rs new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.31.1.272.g89b43f80a514
#8 lore raw

PATCH v5 8/8 libgpiod: Integrate building of rust bindings with make

In reply to Viresh Kumar

Lets make build rust bindings as well.

---
 README                    |  8 +++++---
 TODO                      |  8 --------
 bindings/Makefile.am      |  6 ++++++
 bindings/rust/Makefile.am | 18 ++++++++++++++++++
 configure.ac              | 16 ++++++++++++++++
 5 files changed, 45 insertions(+), 11 deletions(-)
 create mode 100644 bindings/rust/Makefile.am
README +5 -3
index 814a0f161fd2..68b5d69f9b66 100644
@@ -119,9 +119,9 @@ TOOLS
 BINDINGS
 --------
 
-High-level, object-oriented bindings for C++ and python3 are provided. They
-can be enabled by passing --enable-bindings-cxx and --enable-bindings-python
-arguments respectively to configure.
+High-level, object-oriented bindings for C++, python3 and Rust are provided.
+They can be enabled by passing --enable-bindings-cxx, --enable-bindings-python
+and --enable-bindings-rust arguments respectively to configure.
 
 C++ bindings require C++11 support and autoconf-archive collection if building
 from git.
@@ -132,6 +132,8 @@ the PYTHON_CPPFLAGS and PYTHON_LIBS variables in order to point the build
 system to the correct locations. During native builds, the configure script
 can auto-detect the location of the development files.
 
+Rust bindings require cargo support.
+
 TESTING
 -------
 
TODO +0 -8
index 8bb4d8f3ad56..cf4fd7b4a962 100644
@@ -28,14 +28,6 @@ and is partially functional.
 
 ----------
 
-* implement rust bindings
-
-With Rust gaining popularity as a low-level system's language and the
-possibility of it making its way into the linux kernel, it's probably time to
-provide Rust bindings to libgpiod as part of the project.
-
-----------
-
 * implement a simple daemon for controlling GPIOs in C together with a client
   program
 
bindings/Makefile.am +6 -0
index 8f8c762f254f..004ae23dbc58 100644
@@ -14,3 +14,9 @@ if WITH_BINDINGS_PYTHON
 SUBDIRS += python
 
 endif
+
+if WITH_BINDINGS_RUST
+
+SUBDIRS += rust
+
+endif
bindings/rust/Makefile.am new file +18 -0
new file mode 100644
index 000000000000..79a52bc691ae
@@ -0,0 +1,18 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2022 Viresh Kumar <[email protected]>
+
+command = cargo build --release --lib
+
+if WITH_TESTS
+command += --tests
+endif
+
+if WITH_EXAMPLES
+command += --examples
+endif
+
+all:
+	$(command)
+
+clean:
+	cargo clean
configure.ac +16 -0
index ab03673589e9..8458f734a606 100644
@@ -211,6 +211,21 @@ then
 		[AC_SUBST(PYTHON_LIBS, [`$PYTHON-config --libs`])])
 fi
 
+AC_ARG_ENABLE([bindings-rust],
+	[AS_HELP_STRING([--enable-bindings-rust],[enable rust bindings [default=no]])],
+	[if test "x$enableval" = xyes; then with_bindings_rust=true; fi],
+	[with_bindings_rust=false])
+AM_CONDITIONAL([WITH_BINDINGS_RUST], [test "x$with_bindings_rust" = xtrue])
+
+if test "x$with_bindings_rust" = xtrue
+then
+	AC_CHECK_PROG([has_cargo], [cargo], [true], [false])
+	if test "x$has_cargo" = xfalse
+	then
+		AC_MSG_ERROR([cargo not found - needed for rust bindings])
+	fi
+fi
+
 AC_CHECK_PROG([has_doxygen], [doxygen], [true], [false])
 AM_CONDITIONAL([HAS_DOXYGEN], [test "x$has_doxygen" = xtrue])
 if test "x$has_doxygen" = xfalse
@@ -245,6 +260,7 @@ AC_CONFIG_FILES([Makefile
 		 bindings/python/Makefile
 		 bindings/python/examples/Makefile
 		 bindings/python/tests/Makefile
+		 bindings/rust/Makefile
 		 man/Makefile])
 
 AC_OUTPUT
Signature · 1 line
2.31.1.272.g89b43f80a514
#9 lore raw
In reply to Viresh Kumar
Quoted 6 lines · Viresh Kumar

Hello,

Here is another version of rust bindings for libgpiod v2.0, based of the next/libgpiod-2.0.

Quoted 7 lines

Pushed here:

https://github.com/vireshk/libgpiod master

Kent, I hope I haven't missed any of your comments, there were too many of them. Thanks for your review.

Hey Viresh et al.

Thank you for your hard work on that, we'll get it merged eventually. :)

I'm just letting you know that I am following the discussion. I sadly don't know Rust well enough to be able to review the patches in detail but from looking at the code, I can tell that you followed the C++ bindings in how line-config works. Looking at how line configuration works in C++ and Rust bindings and how I struggle to translate it well to Python, I decided to take one step back and revisit the line config in core C API.

My goal is to tweak the data model in a way that - while making the C API slightly more complex - will allow high-level bindings to expose more elegant interfaces using mutator chaining (like what is customary in Rust and what is also possible and often used in C++ and Python) while reducing the usage of quasi dynamic typing using std::any in C++.

I wanted to post my work this week but didn't manage to finish it and now I'm leaving for vacation. If you want to take a look, the C part is done and available here:
https://github.com/brgl/libgpiod-private/tree/topic/rebuild-line-config.
C++ part is in progress.

I hope you won't mind some more reworks to the Rust API but I'm sure it will be much better in the end and more like what Rust libraries typically look like.

Bart

#10 lore raw
Quoted 1 line · Bartosz Golaszewski

Thank you for your hard work on that, we'll get it merged eventually. :)

:)

Quoted 4 lines

I'm just letting you know that I am following the discussion. I sadly don't know Rust well enough to be able to review the patches in detail but from looking at the code, I can tell that you followed the C++ bindings in how line-config works.

Not really. I wrote the wrappers based on include/gpiod.h only and didn't look at C++ bindings for them.

The only point where I looked at C++ bindings was when I was writing examples and tests. I am not a Python guy, so the only thing left was C++ :)

Quoted 11 lines

Looking at how line configuration
works in C++ and Rust bindings and how I struggle to translate it well to Python, I decided to take one step back and revisit the line config in core C API.

My goal is to tweak the data model in a way that - while making the C API slightly more complex - will allow high-level bindings to expose more elegant interfaces using mutator chaining (like what is customary in Rust and what is also possible and often used in C++ and Python) while reducing the usage of quasi dynamic typing using std::any in C++.

Makes sense.

Quoted 5 lines

I wanted to post my work this week but didn't manage to finish it and now I'm leaving for vacation. If you want to take a look, the C part is done and available here:
https://github.com/brgl/libgpiod-private/tree/topic/rebuild-line-config.
C++ part is in progress.

I did have a look at this yesterday. I would like make changes towards this once this is merged, so I don't need to do it again.

Quoted 1 line

I hope you won't mind some more reworks to the Rust API

Not at all.

Quoted 3 lines

but I'm sure
it will be much better in the end and more like what Rust libraries typically look like.

I think that the C API is still getting worked up at the moment and the modifications (at API level) won't stop after this too. We will see more improvements going forward, which is the right thing to do in order to get a robust and more user friendly API out of it.

I worry if this will keep happening and keep pushing the Rust patches to be merged to libgpiod. I am fine to resend the patches on top of the rebuild-line-config changes as well though.

FWIW, I will still be around after this is merged, to write/review Rust patches based on any changes to the C API. :)

Enjoy your vacation.

Signature · 1 line
viresh