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

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>
This commit is contained in:
Bartosz Golaszewski
2025-07-23 10:16:48 +02:00
parent 8da049f2d7
commit cd32f27dd5
13 changed files with 80 additions and 52 deletions

View File

@@ -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 {

View File

@@ -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() {

View File

@@ -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)",

View File

@@ -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");

View File

@@ -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)?;

View File

@@ -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)?;

View File

@@ -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)?;

View File

@@ -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;

View File

@@ -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];

View File

@@ -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)?;
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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];