Skip to content

Commit

Permalink
Rust: add support for no_std env
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Sep 12, 2024
1 parent ecf547f commit 3bac661
Show file tree
Hide file tree
Showing 25 changed files with 225 additions and 275 deletions.
20 changes: 9 additions & 11 deletions lib/xdrgen/generators/rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ use std::{
};

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io::{Error as _, ErrorType, Read, Write};
#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io_cursor::Cursor;
use embedded_io_extras::{Cursor, Error as _, ErrorType, Read, Write};

/// Error contains all errors returned by functions in this crate. It can be
/// compared via `PartialEq`, however any contained IO errors will only be
Expand All @@ -61,7 +59,7 @@ pub enum Error {
#[cfg(feature = "std")]
Io(io::Error),
#[cfg(all(not(feature = "std"), feature = "alloc"))]
Io(embedded_io::ErrorKind),
Io(embedded_io_extras::ErrorKind),
DepthLimitExceeded,
#[cfg(feature = "serde_json")]
Json(serde_json::Error),
Expand All @@ -87,21 +85,21 @@ impl PartialEq for Error {


#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl embedded_io::Error for Error {
fn kind(&self) -> embedded_io::ErrorKind {
impl embedded_io_extras::Error for Error {
fn kind(&self) -> embedded_io_extras::ErrorKind {
match self {
Self::Io(e) => *e,
_ => embedded_io::ErrorKind::Other,
_ => embedded_io_extras::ErrorKind::Other,
}
}
}

#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl From<embedded_io::ReadExactError<Error>> for Error {
fn from(value: embedded_io::ReadExactError<Error>) -> Self {
impl From<embedded_io_extras::ReadExactError<Error>> for Error {
fn from(value: embedded_io_extras::ReadExactError<Error>) -> Self {
match value {
embedded_io::ReadExactError::UnexpectedEof => Error::Io(embedded_io::ErrorKind::Other),
embedded_io::ReadExactError::Other(e) => e
embedded_io_extras::ReadExactError::UnexpectedEof => Error::Io(embedded_io_extras::ErrorKind::Other),
embedded_io_extras::ReadExactError::Other(e) => e
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions spec/output/generator_spec_rust/block_comments.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ use std::{
};

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io::{Error as _, ErrorType, Read, Write};
#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io_cursor::Cursor;
use embedded_io_extras::{Cursor, Error as _, ErrorType, Read, Write};

/// Error contains all errors returned by functions in this crate. It can be
/// compared via `PartialEq`, however any contained IO errors will only be
Expand All @@ -71,7 +69,7 @@ pub enum Error {
#[cfg(feature = "std")]
Io(io::Error),
#[cfg(all(not(feature = "std"), feature = "alloc"))]
Io(embedded_io::ErrorKind),
Io(embedded_io_extras::ErrorKind),
DepthLimitExceeded,
#[cfg(feature = "serde_json")]
Json(serde_json::Error),
Expand All @@ -97,21 +95,21 @@ impl PartialEq for Error {


#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl embedded_io::Error for Error {
fn kind(&self) -> embedded_io::ErrorKind {
impl embedded_io_extras::Error for Error {
fn kind(&self) -> embedded_io_extras::ErrorKind {
match self {
Self::Io(e) => *e,
_ => embedded_io::ErrorKind::Other,
_ => embedded_io_extras::ErrorKind::Other,
}
}
}

#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl From<embedded_io::ReadExactError<Error>> for Error {
fn from(value: embedded_io::ReadExactError<Error>) -> Self {
impl From<embedded_io_extras::ReadExactError<Error>> for Error {
fn from(value: embedded_io_extras::ReadExactError<Error>) -> Self {
match value {
embedded_io::ReadExactError::UnexpectedEof => Error::Io(embedded_io::ErrorKind::Other),
embedded_io::ReadExactError::Other(e) => e
embedded_io_extras::ReadExactError::UnexpectedEof => Error::Io(embedded_io_extras::ErrorKind::Other),
embedded_io_extras::ReadExactError::Other(e) => e
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions spec/output/generator_spec_rust/const.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ use std::{
};

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io::{Error as _, ErrorType, Read, Write};
#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io_cursor::Cursor;
use embedded_io_extras::{Cursor, Error as _, ErrorType, Read, Write};

/// Error contains all errors returned by functions in this crate. It can be
/// compared via `PartialEq`, however any contained IO errors will only be
Expand All @@ -71,7 +69,7 @@ pub enum Error {
#[cfg(feature = "std")]
Io(io::Error),
#[cfg(all(not(feature = "std"), feature = "alloc"))]
Io(embedded_io::ErrorKind),
Io(embedded_io_extras::ErrorKind),
DepthLimitExceeded,
#[cfg(feature = "serde_json")]
Json(serde_json::Error),
Expand All @@ -97,21 +95,21 @@ impl PartialEq for Error {


#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl embedded_io::Error for Error {
fn kind(&self) -> embedded_io::ErrorKind {
impl embedded_io_extras::Error for Error {
fn kind(&self) -> embedded_io_extras::ErrorKind {
match self {
Self::Io(e) => *e,
_ => embedded_io::ErrorKind::Other,
_ => embedded_io_extras::ErrorKind::Other,
}
}
}

#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl From<embedded_io::ReadExactError<Error>> for Error {
fn from(value: embedded_io::ReadExactError<Error>) -> Self {
impl From<embedded_io_extras::ReadExactError<Error>> for Error {
fn from(value: embedded_io_extras::ReadExactError<Error>) -> Self {
match value {
embedded_io::ReadExactError::UnexpectedEof => Error::Io(embedded_io::ErrorKind::Other),
embedded_io::ReadExactError::Other(e) => e
embedded_io_extras::ReadExactError::UnexpectedEof => Error::Io(embedded_io_extras::ErrorKind::Other),
embedded_io_extras::ReadExactError::Other(e) => e
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions spec/output/generator_spec_rust/enum.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ use std::{
};

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io::{Error as _, ErrorType, Read, Write};
#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io_cursor::Cursor;
use embedded_io_extras::{Cursor, Error as _, ErrorType, Read, Write};

/// Error contains all errors returned by functions in this crate. It can be
/// compared via `PartialEq`, however any contained IO errors will only be
Expand All @@ -71,7 +69,7 @@ pub enum Error {
#[cfg(feature = "std")]
Io(io::Error),
#[cfg(all(not(feature = "std"), feature = "alloc"))]
Io(embedded_io::ErrorKind),
Io(embedded_io_extras::ErrorKind),
DepthLimitExceeded,
#[cfg(feature = "serde_json")]
Json(serde_json::Error),
Expand All @@ -97,21 +95,21 @@ impl PartialEq for Error {


#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl embedded_io::Error for Error {
fn kind(&self) -> embedded_io::ErrorKind {
impl embedded_io_extras::Error for Error {
fn kind(&self) -> embedded_io_extras::ErrorKind {
match self {
Self::Io(e) => *e,
_ => embedded_io::ErrorKind::Other,
_ => embedded_io_extras::ErrorKind::Other,
}
}
}

#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl From<embedded_io::ReadExactError<Error>> for Error {
fn from(value: embedded_io::ReadExactError<Error>) -> Self {
impl From<embedded_io_extras::ReadExactError<Error>> for Error {
fn from(value: embedded_io_extras::ReadExactError<Error>) -> Self {
match value {
embedded_io::ReadExactError::UnexpectedEof => Error::Io(embedded_io::ErrorKind::Other),
embedded_io::ReadExactError::Other(e) => e
embedded_io_extras::ReadExactError::UnexpectedEof => Error::Io(embedded_io_extras::ErrorKind::Other),
embedded_io_extras::ReadExactError::Other(e) => e
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions spec/output/generator_spec_rust/nesting.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ use std::{
};

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io::{Error as _, ErrorType, Read, Write};
#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io_cursor::Cursor;
use embedded_io_extras::{Cursor, Error as _, ErrorType, Read, Write};

/// Error contains all errors returned by functions in this crate. It can be
/// compared via `PartialEq`, however any contained IO errors will only be
Expand All @@ -71,7 +69,7 @@ pub enum Error {
#[cfg(feature = "std")]
Io(io::Error),
#[cfg(all(not(feature = "std"), feature = "alloc"))]
Io(embedded_io::ErrorKind),
Io(embedded_io_extras::ErrorKind),
DepthLimitExceeded,
#[cfg(feature = "serde_json")]
Json(serde_json::Error),
Expand All @@ -97,21 +95,21 @@ impl PartialEq for Error {


#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl embedded_io::Error for Error {
fn kind(&self) -> embedded_io::ErrorKind {
impl embedded_io_extras::Error for Error {
fn kind(&self) -> embedded_io_extras::ErrorKind {
match self {
Self::Io(e) => *e,
_ => embedded_io::ErrorKind::Other,
_ => embedded_io_extras::ErrorKind::Other,
}
}
}

#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl From<embedded_io::ReadExactError<Error>> for Error {
fn from(value: embedded_io::ReadExactError<Error>) -> Self {
impl From<embedded_io_extras::ReadExactError<Error>> for Error {
fn from(value: embedded_io_extras::ReadExactError<Error>) -> Self {
match value {
embedded_io::ReadExactError::UnexpectedEof => Error::Io(embedded_io::ErrorKind::Other),
embedded_io::ReadExactError::Other(e) => e
embedded_io_extras::ReadExactError::UnexpectedEof => Error::Io(embedded_io_extras::ErrorKind::Other),
embedded_io_extras::ReadExactError::Other(e) => e
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions spec/output/generator_spec_rust/optional.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ use std::{
};

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io::{Error as _, ErrorType, Read, Write};
#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io_cursor::Cursor;
use embedded_io_extras::{Cursor, Error as _, ErrorType, Read, Write};

/// Error contains all errors returned by functions in this crate. It can be
/// compared via `PartialEq`, however any contained IO errors will only be
Expand All @@ -71,7 +69,7 @@ pub enum Error {
#[cfg(feature = "std")]
Io(io::Error),
#[cfg(all(not(feature = "std"), feature = "alloc"))]
Io(embedded_io::ErrorKind),
Io(embedded_io_extras::ErrorKind),
DepthLimitExceeded,
#[cfg(feature = "serde_json")]
Json(serde_json::Error),
Expand All @@ -97,21 +95,21 @@ impl PartialEq for Error {


#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl embedded_io::Error for Error {
fn kind(&self) -> embedded_io::ErrorKind {
impl embedded_io_extras::Error for Error {
fn kind(&self) -> embedded_io_extras::ErrorKind {
match self {
Self::Io(e) => *e,
_ => embedded_io::ErrorKind::Other,
_ => embedded_io_extras::ErrorKind::Other,
}
}
}

#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl From<embedded_io::ReadExactError<Error>> for Error {
fn from(value: embedded_io::ReadExactError<Error>) -> Self {
impl From<embedded_io_extras::ReadExactError<Error>> for Error {
fn from(value: embedded_io_extras::ReadExactError<Error>) -> Self {
match value {
embedded_io::ReadExactError::UnexpectedEof => Error::Io(embedded_io::ErrorKind::Other),
embedded_io::ReadExactError::Other(e) => e
embedded_io_extras::ReadExactError::UnexpectedEof => Error::Io(embedded_io_extras::ErrorKind::Other),
embedded_io_extras::ReadExactError::Other(e) => e
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions spec/output/generator_spec_rust/struct.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ use std::{
};

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io::{Error as _, ErrorType, Read, Write};
#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io_cursor::Cursor;
use embedded_io_extras::{Cursor, Error as _, ErrorType, Read, Write};

/// Error contains all errors returned by functions in this crate. It can be
/// compared via `PartialEq`, however any contained IO errors will only be
Expand All @@ -71,7 +69,7 @@ pub enum Error {
#[cfg(feature = "std")]
Io(io::Error),
#[cfg(all(not(feature = "std"), feature = "alloc"))]
Io(embedded_io::ErrorKind),
Io(embedded_io_extras::ErrorKind),
DepthLimitExceeded,
#[cfg(feature = "serde_json")]
Json(serde_json::Error),
Expand All @@ -97,21 +95,21 @@ impl PartialEq for Error {


#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl embedded_io::Error for Error {
fn kind(&self) -> embedded_io::ErrorKind {
impl embedded_io_extras::Error for Error {
fn kind(&self) -> embedded_io_extras::ErrorKind {
match self {
Self::Io(e) => *e,
_ => embedded_io::ErrorKind::Other,
_ => embedded_io_extras::ErrorKind::Other,
}
}
}

#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl From<embedded_io::ReadExactError<Error>> for Error {
fn from(value: embedded_io::ReadExactError<Error>) -> Self {
impl From<embedded_io_extras::ReadExactError<Error>> for Error {
fn from(value: embedded_io_extras::ReadExactError<Error>) -> Self {
match value {
embedded_io::ReadExactError::UnexpectedEof => Error::Io(embedded_io::ErrorKind::Other),
embedded_io::ReadExactError::Other(e) => e
embedded_io_extras::ReadExactError::UnexpectedEof => Error::Io(embedded_io_extras::ErrorKind::Other),
embedded_io_extras::ReadExactError::Other(e) => e
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions spec/output/generator_spec_rust/test.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ use std::{
};

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io::{Error as _, ErrorType, Read, Write};
#[cfg(all(not(feature = "std"), feature = "alloc"))]
use embedded_io_cursor::Cursor;
use embedded_io_extras::{Cursor, Error as _, ErrorType, Read, Write};

/// Error contains all errors returned by functions in this crate. It can be
/// compared via `PartialEq`, however any contained IO errors will only be
Expand All @@ -71,7 +69,7 @@ pub enum Error {
#[cfg(feature = "std")]
Io(io::Error),
#[cfg(all(not(feature = "std"), feature = "alloc"))]
Io(embedded_io::ErrorKind),
Io(embedded_io_extras::ErrorKind),
DepthLimitExceeded,
#[cfg(feature = "serde_json")]
Json(serde_json::Error),
Expand All @@ -97,21 +95,21 @@ impl PartialEq for Error {


#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl embedded_io::Error for Error {
fn kind(&self) -> embedded_io::ErrorKind {
impl embedded_io_extras::Error for Error {
fn kind(&self) -> embedded_io_extras::ErrorKind {
match self {
Self::Io(e) => *e,
_ => embedded_io::ErrorKind::Other,
_ => embedded_io_extras::ErrorKind::Other,
}
}
}

#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl From<embedded_io::ReadExactError<Error>> for Error {
fn from(value: embedded_io::ReadExactError<Error>) -> Self {
impl From<embedded_io_extras::ReadExactError<Error>> for Error {
fn from(value: embedded_io_extras::ReadExactError<Error>) -> Self {
match value {
embedded_io::ReadExactError::UnexpectedEof => Error::Io(embedded_io::ErrorKind::Other),
embedded_io::ReadExactError::Other(e) => e
embedded_io_extras::ReadExactError::UnexpectedEof => Error::Io(embedded_io_extras::ErrorKind::Other),
embedded_io_extras::ReadExactError::Other(e) => e
}
}
}
Expand Down
Loading

0 comments on commit 3bac661

Please sign in to comment.