* [PATCH libgpiod 2/2] bindings: rust: unify imports in examples
2025-07-23 8:16 [PATCH libgpiod 0/2] bindings: rust: update examples Bartosz Golaszewski
2025-07-23 8:16 ` [PATCH libgpiod 1/2] bindings: rust: fix formatting in examples Bartosz Golaszewski
@ 2025-07-23 8:16 ` Bartosz Golaszewski
2025-07-23 8:56 ` Erik Schilling
2025-07-23 8:56 ` [PATCH libgpiod 0/2] bindings: rust: update examples Erik Schilling
` (2 subsequent siblings)
4 siblings, 1 reply; 7+ messages in thread
From: Bartosz Golaszewski @ 2025-07-23 8:16 UTC (permalink / raw)
To: Viresh Kumar, Linus Walleij, Erik Wierich; +Cc: linux-gpio, Bartosz Golaszewski
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
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.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
.../libgpiod/examples/buffered_event_lifetimes.rs | 23 ++++++++++++--------
.../rust/libgpiod/examples/find_line_by_name.rs | 6 ++++--
bindings/rust/libgpiod/examples/get_chip_info.rs | 4 ++--
bindings/rust/libgpiod/examples/get_line_info.rs | 6 +++---
bindings/rust/libgpiod/examples/get_line_value.rs | 19 ++++++++++------
.../libgpiod/examples/get_multiple_line_values.rs | 17 +++++++++------
.../examples/reconfigure_input_to_output.rs | 25 +++++++++++++---------
.../rust/libgpiod/examples/toggle_line_value.rs | 7 ++++--
.../examples/toggle_multiple_line_values.rs | 7 ++++--
bindings/rust/libgpiod/examples/watch_line_info.rs | 6 +++---
.../rust/libgpiod/examples/watch_line_rising.rs | 4 ++--
.../rust/libgpiod/examples/watch_line_value.rs | 4 ++--
.../examples/watch_multiple_line_values.rs | 4 ++--
13 files changed, 80 insertions(+), 52 deletions(-)
diff --git a/bindings/rust/libgpiod/examples/buffered_event_lifetimes.rs b/bindings/rust/libgpiod/examples/buffered_event_lifetimes.rs
index 8dbb4963b638ff9f8592f76ac85c6e106f3ddfa9..7589e1f4187d364f28477c0d410b7e666608ac88 100644
--- a/bindings/rust/libgpiod/examples/buffered_event_lifetimes.rs
+++ b/bindings/rust/libgpiod/examples/buffered_event_lifetimes.rs
@@ -5,26 +5,31 @@
// An example demonstrating that an edge event must be cloned to outlive
// subsequent writes to the containing event buffer.
-use libgpiod::line;
+use libgpiod::{
+ chip::Chip,
+ line::{Config as LineConfig, Edge, Settings},
+ request::{Buffer, Config as ReqConfig, Event},
+ Result,
+};
-fn main() -> libgpiod::Result<()> {
+fn main() -> Result<()> {
// Example configuration - customize to suit your situation
let chip_path = "/dev/gpiochip0";
let line_offset = 5;
- let mut lsettings = line::Settings::new()?;
- lsettings.set_edge_detection(Some(line::Edge::Both))?;
+ let mut lsettings = Settings::new()?;
+ lsettings.set_edge_detection(Some(Edge::Both))?;
- let mut lconfig = line::Config::new()?;
+ let mut lconfig = LineConfig::new()?;
lconfig.add_line_settings(&[line_offset], lsettings)?;
- let mut rconfig = libgpiod::request::Config::new()?;
+ let mut rconfig = ReqConfig::new()?;
rconfig.set_consumer("buffered-event-lifetimes")?;
- let chip = libgpiod::chip::Chip::open(&chip_path)?;
+ let chip = Chip::open(&chip_path)?;
let request = chip.request_lines(Some(&rconfig), &lconfig)?;
- let mut buffer = libgpiod::request::Buffer::new(4)?;
+ let mut buffer = Buffer::new(4)?;
loop {
// Blocks until at least one event is available
@@ -34,7 +39,7 @@ fn main() -> libgpiod::Result<()> {
let event = events.next().unwrap()?;
// This will out live `event` and the next read_edge_events().
- let cloned_event = libgpiod::request::Event::try_clone(event)?;
+ let cloned_event = Event::try_clone(event)?;
let events = request.read_edge_events(&mut buffer)?;
for event in events {
diff --git a/bindings/rust/libgpiod/examples/find_line_by_name.rs b/bindings/rust/libgpiod/examples/find_line_by_name.rs
index c563c45eac793d9547b729ed58712f61a9d8e338..3652b6419731b7e85d594906a1b5d41eed63fcb6 100644
--- a/bindings/rust/libgpiod/examples/find_line_by_name.rs
+++ b/bindings/rust/libgpiod/examples/find_line_by_name.rs
@@ -3,13 +3,15 @@
//
// Minimal example of finding a line with the given name.
-fn main() -> libgpiod::Result<()> {
+use libgpiod::{gpiochip_devices, Result};
+
+fn main() -> Result<()> {
// Example configuration - customize to suit your situation
let line_name = "GPIO19";
// Names are not guaranteed unique, so this finds the first line with
// the given name.
- for chip in libgpiod::gpiochip_devices(&"/dev")? {
+ for chip in gpiochip_devices(&"/dev")? {
let offset = chip.line_offset_from_name(line_name);
if offset.is_ok() {
diff --git a/bindings/rust/libgpiod/examples/get_chip_info.rs b/bindings/rust/libgpiod/examples/get_chip_info.rs
index cc23c86a3fd4a64e6fc995aba271ec8df41e2fef..1781d464f0e66471ac1beb038d566eb537fed402 100644
--- a/bindings/rust/libgpiod/examples/get_chip_info.rs
+++ b/bindings/rust/libgpiod/examples/get_chip_info.rs
@@ -3,13 +3,13 @@
//
// Minimal example of reading the info for a chip.
-use libgpiod::{self, Result};
+use libgpiod::{self, chip::Chip, Result};
fn main() -> Result<()> {
// Example configuration - customize to suit your situation
let chip_path = "/dev/gpiochip0";
- let chip = libgpiod::chip::Chip::open(&chip_path)?;
+ let chip = Chip::open(&chip_path)?;
let info = chip.info()?;
println!(
"{} [{}] ({} lines)",
diff --git a/bindings/rust/libgpiod/examples/get_line_info.rs b/bindings/rust/libgpiod/examples/get_line_info.rs
index 9eb8d0fae7ad2d0f09c2721f010513e074f98e58..36c6fbea4c873be473bbbd7d6b91c6925084c9d8 100644
--- a/bindings/rust/libgpiod/examples/get_line_info.rs
+++ b/bindings/rust/libgpiod/examples/get_line_info.rs
@@ -3,14 +3,14 @@
//
// Minimal example of reading the info for a line.
-use libgpiod::line::Direction;
+use libgpiod::{chip::Chip, line::Direction, Result};
-fn main() -> libgpiod::Result<()> {
+fn main() -> Result<()> {
// Example configuration - customize to suit your situation
let chip_path = "/dev/gpiochip0";
let line_offset = 3;
- let chip = libgpiod::chip::Chip::open(&chip_path)?;
+ let chip = Chip::open(&chip_path)?;
let info = chip.line_info(line_offset)?;
let name = info.name().unwrap_or("unnamed");
diff --git a/bindings/rust/libgpiod/examples/get_line_value.rs b/bindings/rust/libgpiod/examples/get_line_value.rs
index e1ef6c7896c3671667119a256d14e930a291d532..9986931af29a284131ce21fe11915285264786eb 100644
--- a/bindings/rust/libgpiod/examples/get_line_value.rs
+++ b/bindings/rust/libgpiod/examples/get_line_value.rs
@@ -3,23 +3,28 @@
//
// Minimal example of reading a single line.
-use libgpiod::line;
+use libgpiod::{
+ chip::Chip,
+ line::{Config as LineConfig, Direction, Settings},
+ request::Config as ReqConfig,
+ Result,
+};
-fn main() -> libgpiod::Result<()> {
+fn main() -> Result<()> {
// Example configuration - customize to suit your situation
let chip_path = "/dev/gpiochip0";
let line_offset = 5;
- let mut lsettings = line::Settings::new()?;
- lsettings.set_direction(line::Direction::Input)?;
+ let mut lsettings = Settings::new()?;
+ lsettings.set_direction(Direction::Input)?;
- let mut lconfig = line::Config::new()?;
+ let mut lconfig = LineConfig::new()?;
lconfig.add_line_settings(&[line_offset], lsettings)?;
- let mut rconfig = libgpiod::request::Config::new()?;
+ let mut rconfig = ReqConfig::new()?;
rconfig.set_consumer("get-line-value")?;
- let chip = libgpiod::chip::Chip::open(&chip_path)?;
+ let chip = Chip::open(&chip_path)?;
let request = chip.request_lines(Some(&rconfig), &lconfig)?;
let value = request.value(line_offset)?;
diff --git a/bindings/rust/libgpiod/examples/get_multiple_line_values.rs b/bindings/rust/libgpiod/examples/get_multiple_line_values.rs
index b09f3be033246c61cda43bd0e947ba34bb421642..a33a787fba4e94060c83a9c909b0f9c74517d7c1 100644
--- a/bindings/rust/libgpiod/examples/get_multiple_line_values.rs
+++ b/bindings/rust/libgpiod/examples/get_multiple_line_values.rs
@@ -3,22 +3,27 @@
//
// Minimal example of reading multiple lines.
-use libgpiod::line::{self, Direction};
+use libgpiod::{
+ chip::Chip,
+ line::{Config as LineConfig, Direction, Settings},
+ request::Config as ReqConfig,
+ Result,
+};
-fn main() -> libgpiod::Result<()> {
+fn main() -> Result<()> {
// Example configuration - customize to suit your situation
let chip_path = "/dev/gpiochip0";
let line_offsets = [5, 3, 7];
- let mut lsettings = line::Settings::new()?;
- let mut lconfig = line::Config::new()?;
+ let mut lsettings = Settings::new()?;
+ let mut lconfig = LineConfig::new()?;
lsettings.set_direction(Direction::Input)?;
lconfig.add_line_settings(&line_offsets, lsettings)?;
- let chip = libgpiod::chip::Chip::open(&chip_path)?;
+ let chip = Chip::open(&chip_path)?;
- let mut rconfig = libgpiod::request::Config::new()?;
+ let mut rconfig = ReqConfig::new()?;
rconfig.set_consumer("get-multiple-line-values")?;
let request = chip.request_lines(Some(&rconfig), &lconfig)?;
diff --git a/bindings/rust/libgpiod/examples/reconfigure_input_to_output.rs b/bindings/rust/libgpiod/examples/reconfigure_input_to_output.rs
index b872d7709f3ecc651cf6d372cb46d6fbdfed77b1..ded576529212de4aaea0c025f7d1d38c44baaa7c 100644
--- a/bindings/rust/libgpiod/examples/reconfigure_input_to_output.rs
+++ b/bindings/rust/libgpiod/examples/reconfigure_input_to_output.rs
@@ -3,23 +3,28 @@
//
// Example of a bi-directional line requested as input and then switched to output.
-use libgpiod::line;
+use libgpiod::{
+ chip::Chip,
+ line::{Config as LineConfig, Direction, Settings, Value},
+ request::Config as ReqConfig,
+ Result,
+};
-fn main() -> libgpiod::Result<()> {
+fn main() -> Result<()> {
// Example configuration - customize to suit your situation
let chip_path = "/dev/gpiochip0";
let line_offset = 5;
- let mut lsettings = line::Settings::new()?;
- lsettings.set_direction(line::Direction::Input)?;
+ let mut lsettings = Settings::new()?;
+ lsettings.set_direction(Direction::Input)?;
- let mut lconfig = line::Config::new()?;
+ let mut lconfig = LineConfig::new()?;
lconfig.add_line_settings(&[line_offset], lsettings)?;
- let mut rconfig = libgpiod::request::Config::new()?;
+ let mut rconfig = ReqConfig::new()?;
rconfig.set_consumer("reconfigure-input-to-output")?;
- let chip = libgpiod::chip::Chip::open(&chip_path)?;
+ let chip = Chip::open(&chip_path)?;
// request the line initially as an input
let mut request = chip.request_lines(Some(&rconfig), &lconfig)?;
@@ -28,9 +33,9 @@ fn main() -> libgpiod::Result<()> {
println!("{line_offset}={value:?} (input)");
// switch the line to an output and drive it low
- let mut lsettings = line::Settings::new()?;
- lsettings.set_direction(line::Direction::Output)?;
- lsettings.set_output_value(line::Value::InActive)?;
+ let mut lsettings = Settings::new()?;
+ lsettings.set_direction(Direction::Output)?;
+ lsettings.set_output_value(Value::InActive)?;
lconfig.add_line_settings(&[line_offset], lsettings)?;
request.reconfigure_lines(&lconfig)?;
diff --git a/bindings/rust/libgpiod/examples/toggle_line_value.rs b/bindings/rust/libgpiod/examples/toggle_line_value.rs
index bf87222acc87f559af1467c59b1b98b299755759..8d34c5fa18746bc89fe6597f22a77c000a6b49c6 100644
--- a/bindings/rust/libgpiod/examples/toggle_line_value.rs
+++ b/bindings/rust/libgpiod/examples/toggle_line_value.rs
@@ -4,7 +4,10 @@
// Minimal example of toggling a single line.
use core::time::Duration;
-use libgpiod::line::{self, Value};
+use libgpiod::{
+ line::{self, Value},
+ Result,
+};
fn toggle_value(value: Value) -> Value {
match value {
@@ -13,7 +16,7 @@ fn toggle_value(value: Value) -> Value {
}
}
-fn main() -> libgpiod::Result<()> {
+fn main() -> Result<()> {
// Example configuration - customize to suit your situation
let chip_path = "/dev/gpiochip0";
let line_offset = 5;
diff --git a/bindings/rust/libgpiod/examples/toggle_multiple_line_values.rs b/bindings/rust/libgpiod/examples/toggle_multiple_line_values.rs
index b7e6915fa1813a7e9d237ff0d836b0d4bf5d52da..e15b09392ccbf38d99a1aa591c6022fab64f7ecf 100644
--- a/bindings/rust/libgpiod/examples/toggle_multiple_line_values.rs
+++ b/bindings/rust/libgpiod/examples/toggle_multiple_line_values.rs
@@ -4,7 +4,10 @@
// Minimal example of toggling multiple lines.
use core::time::Duration;
-use libgpiod::line::{self, Offset, Value};
+use libgpiod::{
+ line::{self, Offset, Value},
+ Result,
+};
fn toggle_value(value: Value) -> Value {
match value {
@@ -26,7 +29,7 @@ fn print_values(offsets: &[Offset], values: &[Value]) {
println!();
}
-fn main() -> libgpiod::Result<()> {
+fn main() -> Result<()> {
// Example configuration - customize to suit your situation
let chip_path = "/dev/gpiochip0";
let line_offsets = [5, 3, 7];
diff --git a/bindings/rust/libgpiod/examples/watch_line_info.rs b/bindings/rust/libgpiod/examples/watch_line_info.rs
index e84ce133fece5ae4c6d5f1f4be98384828bdafb5..d724b6749fed19504ad13d8c3eb20aa351d00148 100644
--- a/bindings/rust/libgpiod/examples/watch_line_info.rs
+++ b/bindings/rust/libgpiod/examples/watch_line_info.rs
@@ -3,14 +3,14 @@
//
// Minimal example of watching for info changes on particular lines.
-use libgpiod::line::InfoChangeKind;
+use libgpiod::{chip::Chip, line::InfoChangeKind, Result};
-fn main() -> libgpiod::Result<()> {
+fn main() -> Result<()> {
// Example configuration - customize to suit your situation
let chip_path = "/dev/gpiochip0";
let line_offsets = [5, 3, 7];
- let chip = libgpiod::chip::Chip::open(&chip_path)?;
+ let chip = Chip::open(&chip_path)?;
for offset in line_offsets {
let _info = chip.watch_line_info(offset)?;
}
diff --git a/bindings/rust/libgpiod/examples/watch_line_rising.rs b/bindings/rust/libgpiod/examples/watch_line_rising.rs
index 81a24077e9d7acc7657ea13621e91effbddb12ab..00e65f0e9df3cf73613f71173bcc8c1f6beb5ae5 100644
--- a/bindings/rust/libgpiod/examples/watch_line_rising.rs
+++ b/bindings/rust/libgpiod/examples/watch_line_rising.rs
@@ -3,9 +3,9 @@
//
// Minimal example of watching for edges on a single line.
-use libgpiod::line;
+use libgpiod::{line, Result};
-fn main() -> libgpiod::Result<()> {
+fn main() -> Result<()> {
// Example configuration - customize to suit your situation
let chip_path = "/dev/gpiochip0";
let line_offset = 5;
diff --git a/bindings/rust/libgpiod/examples/watch_line_value.rs b/bindings/rust/libgpiod/examples/watch_line_value.rs
index 3bf40af4ba3aa50cd59b4e05775c217da302e6c3..3df5a2bec2910a96d36da989b34624e2f669394c 100644
--- a/bindings/rust/libgpiod/examples/watch_line_value.rs
+++ b/bindings/rust/libgpiod/examples/watch_line_value.rs
@@ -3,10 +3,10 @@
//
// Minimal example of watching for edges on a single line.
-use libgpiod::line;
+use libgpiod::{line, Result};
use std::time::Duration;
-fn main() -> libgpiod::Result<()> {
+fn main() -> Result<()> {
// Example configuration - customize to suit your situation
let chip_path = "/dev/gpiochip0";
let line_offset = 5;
diff --git a/bindings/rust/libgpiod/examples/watch_multiple_line_values.rs b/bindings/rust/libgpiod/examples/watch_multiple_line_values.rs
index 3fc88baa484754fadb48a2afc4110805e8db639d..81d211784d4929eb925231d05199a61a24816d74 100644
--- a/bindings/rust/libgpiod/examples/watch_multiple_line_values.rs
+++ b/bindings/rust/libgpiod/examples/watch_multiple_line_values.rs
@@ -5,10 +5,10 @@
use libgpiod::{
line::{self, EdgeKind},
- request,
+ request, Result,
};
-fn main() -> libgpiod::Result<()> {
+fn main() -> Result<()> {
// Example configuration - customize to suit your situation
let chip_path = "/dev/gpiochip0";
let line_offsets = [5, 3, 7];
--
2.48.1
^ permalink raw reply related [flat|nested] 7+ messages in thread