Re: [libgpiod][PATCH V9 6/8] bindings: rust: Add examples to libgpiod crate

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> diff --git a/bindings/rust/libgpiod/examples/gpioset.rs b/bindings/rust/libgpiod/examples/gpioset.rs
> new file mode 100644
> index 000000000000..f72a623ab28c
> --- /dev/null
> +++ b/bindings/rust/libgpiod/examples/gpioset.rs
> @@ -0,0 +1,64 @@
> +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
> +//
> +// Copyright 2022 Linaro Ltd. All Rights Reserved.
> +//     Viresh Kumar <viresh.kumar@xxxxxxxxxx>
> +//
> +// Simplified Rust implementation of the gpioset tool.
> +
> +use std::env;
> +use std::io::{stdin, Read};
> +
> +use libgpiod::{
> +    chip::Chip,
> +    line::{self, Direction, Offset, SettingVal, Value},
> +    request, Error, Result,
> +};
> +
> +fn usage(name: &str) {
> +    println!("Usage: {} <chip> <line_offset0>=<value0> ...", name);
> +}
> +
> +fn main() -> Result<()> {
> +    let args: Vec<String> = env::args().collect();
> +    if args.len() < 3 {
> +        usage(&args[0]);
> +        return Err(Error::InvalidArguments);
> +    }
> +
> +    let lconfig = line::Config::new()?;
> +
> +    for arg in &args[2..] {
> +        let pair: Vec<&str> = arg.split('=').collect();
> +        if pair.len() != 2 {
> +            usage(&args[0]);
> +            return Err(Error::InvalidArguments);
> +        }
> +
> +        let offset = pair[0]
> +            .parse::<Offset>()
> +            .map_err(|_| Error::InvalidArguments)?;
> +        let value = pair[1]
> +            .parse::<i32>()
> +            .map_err(|_| Error::InvalidArguments)?;
> +
> +        let mut lsettings = line::Settings::new()?;
> +        lsettings.set_prop(&[
> +            SettingVal::Direction(Direction::Output),
> +            SettingVal::OutputValue(Value::new(value)?),
> +        ])?;
> +        lconfig.add_line_settings(&[offset], lsettings)?;
> +    }
> +

I'm looking at it and thinking that it would look much better as:

let settings = line::Settings::new()
    .set_direction(Direction::Output)
    .set_output_value(Value::new(value))
    .build()?;

settings would not need to be mutable (we'd have some intermediate
SettingsBuilder object?) and could be directly passed to
add_line_settings()?

We now have chained mutators for LineSettings in C++ and keyword
arguments in Python - somehow I think that the former suits rust much
better than passing an array of properties.

Bartosz



[Index of Archives]     [Linux SPI]     [Linux Kernel]     [Linux ARM (vger)]     [Linux ARM MSM]     [Linux Omap]     [Linux Arm]     [Linux Tegra]     [Fedora ARM]     [Linux for Samsung SOC]     [eCos]     [Linux Fastboot]     [Gcc Help]     [Git]     [DCCP]     [IETF Announce]     [Security]     [Linux MIPS]     [Yosemite Campsites]

  Powered by Linux