-
Notifications
You must be signed in to change notification settings - Fork 4
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
Support arbitrary byte sequence containers #3
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,12 @@ pub enum Interface { | |
Contactless, | ||
} | ||
|
||
pub type Data<const S: usize> = heapless::Vec<u8, S>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
pub type Result<T=()> = core::result::Result<T, Status>; | ||
|
||
pub mod aid; | ||
pub mod command; | ||
pub mod response; | ||
pub mod somebytes; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also generally wondering if we should have all these public submodules; assuming they're just implementation, we could |
||
|
||
pub use aid::{Aid, App}; | ||
pub use command::{Command, Instruction}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
use core::convert::TryFrom; | ||
use crate::Data; | ||
|
||
impl Default for Status { | ||
fn default() -> Self { | ||
|
@@ -171,12 +170,10 @@ impl Into<[u8; 2]> for Status { | |
} | ||
} | ||
|
||
impl<const S: usize> Into<Data<S>> for Status | ||
{ | ||
impl<const S: usize> Into<heapless::Vec<u8, S>> for Status { | ||
#[inline] | ||
fn into(self) -> Data<S> { | ||
fn into(self) -> heapless::Vec<u8, S> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add an inherent |
||
let arr: [u8; 2] = self.into(); | ||
Data::from_slice(&arr).unwrap() | ||
heapless::Vec::from_slice(&arr).unwrap() | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// This could be a separate crate. | ||
|
||
use core::{cmp, fmt, ops}; | ||
|
||
pub trait TryExtendFromSlice<T> { | ||
type Error; | ||
fn extend_from_slice(&mut self, slice: &[T]) -> Result<(), Self::Error>; | ||
} | ||
|
||
impl<T: Clone, const N: usize> TryExtendFromSlice<T> for heapless::Vec<T, N> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would make sense to have a |
||
type Error = (); | ||
fn extend_from_slice(&mut self, slice: &[T]) -> Result<(), Self::Error> { | ||
heapless::Vec::extend_from_slice(self, slice) | ||
} | ||
} | ||
|
||
/// A wrapper type for a byte sequence. | ||
/// | ||
/// This wrapper implements common traits based on the content of the byte sequence. | ||
#[derive(Clone)] | ||
pub struct Bytes<T: AsRef<[u8]>>(T); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest |
||
|
||
impl<T: AsRef<[u8]>> Bytes<T> { | ||
pub fn into_bytes(self) -> T { | ||
self.0 | ||
} | ||
} | ||
|
||
impl<T: AsRef<[u8]>> AsRef<[u8]> for Bytes<T> { | ||
fn as_ref(&self) -> &[u8] { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl<T: AsRef<[u8]>> ops::Deref for Bytes<T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<T: AsRef<[u8]>> ops::DerefMut for Bytes<T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl<T: AsRef<[u8]>> From<T> for Bytes<T> { | ||
fn from(bytes: T) -> Self { | ||
Self(bytes) | ||
} | ||
} | ||
|
||
impl<T: AsRef<[u8]>> fmt::Debug for Bytes<T> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be nice to have the Debug display hex? We already depend on Not necessarily for this PR, but it would be nice to have a |
||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.0.as_ref().fmt(f) | ||
} | ||
} | ||
|
||
impl<T: AsRef<[u8]>> Eq for Bytes<T> {} | ||
|
||
impl<T: AsRef<[u8]>> PartialEq for Bytes<T> { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.0.as_ref().eq(other.0.as_ref()) | ||
} | ||
} | ||
|
||
impl<T: AsRef<[u8]>> Ord for Bytes<T> { | ||
fn cmp(&self, other: &Self) -> cmp::Ordering { | ||
self.0.as_ref().cmp(other.0.as_ref()) | ||
} | ||
} | ||
|
||
impl<T: AsRef<[u8]>> PartialOrd for Bytes<T> { | ||
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { | ||
self.0.as_ref().partial_cmp(other.0.as_ref()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems this is merged, maybe also published?