Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internally use error constants #149

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/coap_message/impl_0_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use coap_message_0_3::{
MutableWritableMessage, ReadableMessage, WithSortedOptions,
};

use crate::error::NumericError;
use crate::error::{NumericError, EINVAL};

/// Thin wrapper around NumericError that can render and satisfies all conversion requirements
#[derive(Debug)]
Expand Down Expand Up @@ -82,7 +82,7 @@ impl<'a> MinimalWritableMessage for super::ResponseMessage<'a> {

fn add_option(&mut self, number: Self::OptionNumber, value: &[u8]) -> Result<(), Error> {
if self.payload_written.is_some() {
return Err(NumericError::from_constant(riot_sys::EINVAL as _).into());
return Err(EINVAL.into());
}
self.message.opt_add_opaque(number.into(), value)?;
Ok(())
Expand Down Expand Up @@ -112,18 +112,18 @@ impl<'a> MutableWritableMessage for super::ResponseMessage<'a> {
*pm = 0xff;
Ok(pl)
} else {
Err(NumericError::from_constant(riot_sys::EINVAL as _).into())
Err(EINVAL.into())
}
}

fn truncate(&mut self, len: usize) -> Result<(), Error> {
if self.payload_written.is_none() {
// payload() will not even return anything sensible yet
return Err(NumericError::from_constant(riot_sys::EINVAL as _).into());
return Err(EINVAL.into());
}
let pl_len = self.message.payload().len() - 1;
if len > pl_len {
return Err(NumericError::from_constant(riot_sys::EINVAL as _).into());
return Err(EINVAL.into());
}
self.payload_written = Some(len);
Ok(())
Expand Down
3 changes: 2 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl NumericError {
/// Convert the error into an [nb::Error] that is [nb::Error::WouldBlock] if the error is
/// `-EAGAIN`, and an actual error otherwise.
pub fn again_is_wouldblock(self) -> nb::Error<Self> {
if self == Self::from_constant(riot_sys::EAGAIN as _) {
if self == EAGAIN {
return nb::Error::WouldBlock;
}
nb::Error::Other(self)
Expand Down Expand Up @@ -145,5 +145,6 @@ E!(EINVAL);
E!(ENODEV);
E!(ENOMEM);
E!(ENOSPC);
E!(ENOTCONN);
E!(ENOTSUP);
E!(EOVERFLOW);
8 changes: 4 additions & 4 deletions src/saul/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use crate::error;
use crate::helpers::PointerToCStr;
use error::NegativeErrorExt;
use error::{NegativeErrorExt, NumericError, EINVAL};

pub mod registration;

Expand Down Expand Up @@ -70,7 +70,7 @@ impl RegistryEntry {
}

/// Read a value from the SAUL device
pub fn read(&self) -> Result<Phydat, error::NumericError> {
pub fn read(&self) -> Result<Phydat, NumericError> {
// Could work with MaybeUninit here, but probably not worth it.
let mut result: Phydat = Default::default();
let length = (unsafe { riot_sys::saul_reg_read(self.0 as *mut _, &mut result.values) })
Expand All @@ -84,15 +84,15 @@ impl RegistryEntry {
/// Note that the saul_reg_write call does not really pass on the initialized length of the
/// values to the device, but the device returns the used length. If the lengths do not match,
/// the returned length is expressed as an error.
pub fn write(&self, value: Phydat) -> Result<(), error::NumericError> {
pub fn write(&self, value: Phydat) -> Result<(), NumericError> {
// Value copied as we can't really be sure that no SAUL device will ever write here
let length =
unsafe { riot_sys::saul_reg_write(self.0, &value.values as *const _ as *mut _) }
.negative_to_error()?;
if length != value.length.into() {
// It's not pretty to synthesize an error here, but neither would be expressing the
// partial write in the context of lengthful phydat items.
Err(error::NumericError::from_constant(riot_sys::EINVAL as _))
Err(EINVAL)
} else {
Ok(())
}
Expand Down
7 changes: 3 additions & 4 deletions src/socket_embedded_nal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use core::convert::TryInto;
use core::mem::MaybeUninit;

use crate::error::{NegativeErrorExt, NumericError};
use crate::error::{NegativeErrorExt, NumericError, ENOMEM, ENOTCONN};
use crate::socket::UdpEp;

use embedded_nal::SocketAddr;
Expand Down Expand Up @@ -68,8 +68,7 @@ pub struct UdpSocket<'a> {
impl<'a> UdpSocket<'a> {
/// Version of socket() that gives errors compatible with Self::Error
fn access(&mut self) -> Result<*mut riot_sys::sock_udp_t, NumericError> {
self.socket()
.ok_or(NumericError::from_constant(riot_sys::ENOTCONN as _))
self.socket().ok_or(ENOTCONN)
}

/// Accessor to the inner socket pointer
Expand Down Expand Up @@ -100,7 +99,7 @@ impl<'a, const UDPCOUNT: usize> StackAccessor<'a, UDPCOUNT> {
self.stack
.udp_sockets
.push(Default::default())
.map_err(|_| NumericError::from_constant(riot_sys::ENOMEM as _))?;
.map_err(|_| ENOMEM)?;

let last = self.stack.udp_sockets.len() - 1;
Ok(&mut self.stack.udp_sockets[last] as *mut _)
Expand Down
6 changes: 2 additions & 4 deletions src/socket_embedded_nal_tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use core::marker::PhantomData;
use core::mem::MaybeUninit;
use core::pin::Pin;

use crate::error::{NegativeErrorExt, NumericError};
use crate::error::{NegativeErrorExt, NumericError, ENOTCONN};

use embedded_nal::{SocketAddr, TcpClientStack, TcpFullStack};

Expand Down Expand Up @@ -263,9 +263,7 @@ impl<'a, const QUEUELEN: usize> embedded_nal_tcpextensions::TcpExactStack
// connection closed.
//
// FIXME is returning an error right here?
return Err(nb::Error::Other(NumericError::from_constant(
riot_sys::ENOTCONN as _,
)));
return Err(nb::Error::Other(ENOTCONN));
}
assert!(
ret == buf.len(),
Expand Down