diff --git a/lib/src/chip.rs b/lib/src/chip.rs index 07c5507..c19d32f 100644 --- a/lib/src/chip.rs +++ b/lib/src/chip.rs @@ -133,9 +133,8 @@ impl Chip { /// /// # Examples ///```no_run - /// # use gpiocdev::Chip; - /// # fn example() -> gpiocdev::Result{ - /// let chip = Chip::from_path("/dev/gpiochip0")?; + /// # fn example() -> gpiocdev::Result{ + /// let chip = gpiocdev::Chip::from_path("/dev/gpiochip0")?; /// # Ok(chip) /// # } ///``` @@ -156,9 +155,8 @@ impl Chip { /// /// # Examples ///```no_run - /// # use gpiocdev::Chip; - /// # fn example() -> gpiocdev::Result{ - /// let chip = Chip::from_name("gpiochip0")?; + /// # fn example() -> gpiocdev::Result{ + /// let chip = gpiocdev::Chip::from_name("gpiochip0")?; /// # Ok(chip) /// # } ///``` @@ -413,12 +411,14 @@ impl Chip { mem::size_of::() } } + impl AsFd for Chip { #[inline] fn as_fd(&self) -> BorrowedFd<'_> { self.f.as_fd() } } + impl AsRawFd for Chip { #[inline] fn as_raw_fd(&self) -> i32 { @@ -452,6 +452,7 @@ pub struct Info { /// The number of lines provided by the chip. pub num_lines: u32, } + impl From for Info { fn from(ci: uapi::ChipInfo) -> Self { Info { diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 9b69ee5..0c670d2 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -16,11 +16,10 @@ //! //! Request an input line and output line, and read from the input and change the output to that value: //! ```no_run -//! use gpiocdev::Request; +//! # fn example() -> Result<(), gpiocdev::Error> { //! use gpiocdev::line::{Bias, Value}; //! -//! # fn example() -> Result<(), gpiocdev::Error> { -//! let req = Request::builder() +//! let req = gpiocdev::Request::builder() //! .on_chip("/dev/gpiochip0") //! .with_line(3) //! .as_input() @@ -35,15 +34,12 @@ //! ``` //! Monitor a line for debounced edges: //! ```no_run -//! use gpiocdev::line::EdgeDetection; -//! use std::time::Duration; -//! //! # fn example() -> Result<(), gpiocdev::Error> { //! let req = gpiocdev::Request::builder() //! .on_chip("/dev/gpiochip0") //! .with_line(5) -//! .with_edge_detection(EdgeDetection::BothEdges) -//! .with_debounce_period(Duration::from_millis(5)) +//! .with_edge_detection(gpiocdev::line::EdgeDetection::BothEdges) +//! .with_debounce_period(std::time::Duration::from_millis(5)) //! .request()?; //! for edge in req.edge_events() { //! println!("{:?}", edge); @@ -102,8 +98,8 @@ pub fn lines() -> Result { /// # Examples /// The found line can be used to request the line: /// ```no_run -/// # use gpiocdev::line::Value; /// # fn example() -> Result<(), gpiocdev::Error> { +/// # use gpiocdev::line::Value; /// let led0 = gpiocdev::find_named_line("LED0").unwrap(); /// let req = gpiocdev::Request::builder() /// .with_found_line(&led0) @@ -115,8 +111,8 @@ pub fn lines() -> Result { /// /// Using the chip and offset from the found line to request the line: /// ```no_run -/// # use gpiocdev::line::Value; /// # fn example() -> Result<(), gpiocdev::Error> { +/// # use gpiocdev::line::Value; /// let led0 = gpiocdev::find_named_line("LED0").unwrap(); /// let req = gpiocdev::Request::builder() /// .on_chip(&led0.chip) @@ -146,8 +142,8 @@ pub fn find_named_line(name: &str) -> Option { /// # Examples /// Adding the found lines to the request directly: /// ```no_run -/// # use gpiocdev::line::Value; /// # fn example() -> Result<(), gpiocdev::Error> { +/// # use gpiocdev::line::Value; /// let sensors = gpiocdev::find_named_lines(&["SENSOR0", "SENSOR1"], true)?; /// let req = gpiocdev::Request::builder() /// .with_found_lines(&sensors) @@ -162,8 +158,8 @@ pub fn find_named_line(name: &str) -> Option { /// Using the individual found lines to request the lines with different /// configuration for each line: /// ```no_run -/// # use gpiocdev::line::Value; /// # fn example() -> Result<(), gpiocdev::Error> { +/// # use gpiocdev::line::Value; /// let lines = gpiocdev::find_named_lines(&["SENSOR0", "LED0"], true)?; /// let sensor0 = lines.get("SENSOR0").unwrap(); /// let led0 = lines.get("LED0").unwrap(); @@ -208,8 +204,8 @@ pub fn find_named_lines<'a>( /// /// The discovered line can be used to request the line: /// ```no_run -/// # use gpiocdev::line::Value; /// # fn example() -> Result<(), gpiocdev::Error> { +/// # use gpiocdev::line::Value; /// let led0 = gpiocdev::find_named_line("LED0").unwrap(); /// let req = gpiocdev::Request::builder() /// .with_found_line(&led0) @@ -240,8 +236,8 @@ impl From for FoundLine { /// /// Used by [`find_named_line`] and [`find_named_lines`] to find lines based on line name. /// ```no_run -/// # use gpiocdev::line::Value; /// # fn example() -> Result<(), gpiocdev::Error> { +/// # use gpiocdev::line::Value; /// // replicating find_named_line... /// let led2 = gpiocdev::LineIterator::new()?.find(|l| l.info.name == "LED2").unwrap(); /// let req = gpiocdev::Request::builder() diff --git a/lib/src/line/event.rs b/lib/src/line/event.rs index a78bf0e..12ae87d 100644 --- a/lib/src/line/event.rs +++ b/lib/src/line/event.rs @@ -90,6 +90,7 @@ pub enum EdgeKind { /// Indicates the line transitioned from active to inactive. Falling = 2, } + impl From for EdgeKind { fn from(kind: uapi::LineEdgeEventKind) -> Self { match kind { @@ -149,6 +150,7 @@ pub enum InfoChangeKind { /// Line has been reconfigured. Reconfigured = 3, } + impl From for InfoChangeKind { fn from(kind: uapi::LineInfoChangeKind) -> Self { match kind { diff --git a/lib/src/line/value.rs b/lib/src/line/value.rs index 48ce62b..46c6441 100644 --- a/lib/src/line/value.rs +++ b/lib/src/line/value.rs @@ -30,6 +30,7 @@ pub enum Value { /// The line is active. Active, } + impl Value { /// The value opposite the current value. pub fn not(&self) -> Value { @@ -58,6 +59,7 @@ impl From for bool { } } } + impl From for u8 { fn from(l: Value) -> u8 { match l { @@ -66,6 +68,7 @@ impl From for u8 { } } } + impl From for Value { fn from(b: bool) -> Value { match b { @@ -74,6 +77,7 @@ impl From for Value { } } } + impl From for Value { fn from(i: u8) -> Value { match i { @@ -373,6 +377,7 @@ impl Values { true } } + impl<'a> FromIterator<&'a Offset> for Values { fn from_iter>(iter: I) -> Self { let mut values = Values::default(); @@ -383,6 +388,7 @@ impl<'a> FromIterator<&'a Offset> for Values { values } } + impl FromIterator<(Offset, Value)> for Values { fn from_iter>(iter: I) -> Self { let mut values = Values::default(); diff --git a/lib/src/request.rs b/lib/src/request.rs index f86272b..c223541 100644 --- a/lib/src/request.rs +++ b/lib/src/request.rs @@ -101,8 +101,8 @@ impl Request { /// # Examples /// /// ```no_run - /// # use gpiocdev::line::Values; /// # fn example() -> Result<(), gpiocdev::Error> { + /// # use gpiocdev::line::Values; /// let req = gpiocdev::Request::builder() /// .on_chip("/dev/gpiochip0") /// .with_lines(&[3,5]) @@ -121,9 +121,9 @@ impl Request { /// # Examples /// /// ```no_run + /// # fn example() -> Result<(), gpiocdev::Error> { /// # use gpiocdev::line::Value; /// # use gpiocdev::request::Config; - /// # fn example() -> Result<(), gpiocdev::Error> { /// let mut cfg = Config::default(); /// cfg.with_line(5).as_output(Value::Active); /// let req = gpiocdev::Request::from_config(cfg) @@ -144,8 +144,8 @@ impl Request { /// /// # Examples /// ```no_run - /// # use gpiocdev::line::Values; /// # fn example() -> Result<(), gpiocdev::Error> { + /// # use gpiocdev::line::Values; /// let req = gpiocdev::Request::builder() /// .on_chip("/dev/gpiochip0") /// .with_lines(&[3,5,6,8]) @@ -196,8 +196,8 @@ impl Request { /// /// # Examples /// ```no_run - /// # use gpiocdev::line::Values; /// # fn example() -> Result<(), gpiocdev::Error> { + /// # use gpiocdev::line::Values; /// let req = gpiocdev::Request::builder() /// .on_chip("/dev/gpiochip0") /// .with_lines(&[3,5]) @@ -250,8 +250,8 @@ impl Request { /// Set the values for a subset of the requested lines. /// # Examples /// ```no_run - /// # use gpiocdev::line::{Value::{Active, Inactive}, Values}; /// # fn example() -> Result<(), gpiocdev::Error> { + /// # use gpiocdev::line::{Value::{Active, Inactive}, Values}; /// let req = gpiocdev::Request::builder() /// .on_chip("/dev/gpiochip0") /// .with_lines(&[3,5,6,8]) @@ -306,9 +306,9 @@ impl Request { /// /// # Examples /// ```no_run + /// # fn example() -> Result<(), gpiocdev::Error> { /// # use gpiocdev::line::Value; /// # use gpiocdev::request::Config; - /// # fn example() -> Result<(), gpiocdev::Error> { /// let mut cfg = Config::default(); /// cfg.with_line(5).as_output(Value::Active); /// let req = gpiocdev::Request::from_config(cfg) @@ -481,9 +481,9 @@ impl Request { /// /// # Examples /// ```no_run - /// # use gpiocdev::{Request, Result}; + /// # fn example() -> Result<(), gpiocdev::Error> { + /// # use gpiocdev::Request; /// # use gpiocdev::line::{EdgeDetection, Value}; - /// # fn example() -> Result<()> { /// let req = Request::builder() /// .on_chip("/dev/gpiochip0") /// .with_consumer("watcher") @@ -657,12 +657,14 @@ impl Request { mem::size_of::() } } + impl AsFd for Request { #[inline] fn as_fd(&self) -> BorrowedFd<'_> { self.f.as_fd() } } + impl AsRawFd for Request { #[inline] fn as_raw_fd(&self) -> i32 {