xous-rs: remove all remaining clippy warnings

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2021-08-06 12:21:24 +08:00
parent c2bc966020
commit b3cb5ff40a
3 changed files with 30 additions and 61 deletions

View file

@ -84,7 +84,7 @@ impl<'a> AsRef<MemoryRange> for Carton<'a> {
impl<'a> AsRef<[u8]> for Carton<'a> {
fn as_ref(&self) -> &[u8] {
&self.slice
self.slice
}
}

View file

@ -145,9 +145,9 @@ impl From<&[u32; 4]> for SID {
}
}
impl Into<[u32; 4]> for SID {
fn into(self) -> [u32; 4] {
self.0
impl From<SID> for [u32; 4] {
fn from(s: SID) -> [u32; 4] {
s.0
}
}

View file

@ -14,28 +14,6 @@ impl<const N: usize> String<N> {
}
}
pub fn from_str<T>(src: T) -> String<N>
where
T: AsRef<str>,
{
let src = src.as_ref();
let mut s = Self::new();
// Copy the string into our backing store.
for (&src_byte, dest_byte) in src.as_bytes().iter().zip(&mut s.bytes) {
*dest_byte = src_byte;
}
// Set the string length to the length of the passed-in String,
// or the maximum possible length. Which ever is smaller.
s.len = s.bytes.len().min(src.as_bytes().len()) as u32;
// If the string is not valid, set its length to 0.
if s.as_str().is_err() {
s.len = 0;
}
s
}
pub fn as_bytes(&self) -> [u8; N] {
self.bytes
}
@ -62,41 +40,6 @@ impl<const N: usize> String<N> {
unsafe { core::str::from_utf8_unchecked(&self.bytes[0..self.len()]) }
}
/// awful, O(N) implementation because we have to iterate through the entire string
/// and decode variable-length utf8 characters, until we can't.
pub fn pop(&mut self) -> Option<char> {
if self.is_empty() {
return None;
}
// first, make a copy of the string
let tempbytes: [u8; N] = self.bytes;
let tempstr = unsafe { core::str::from_utf8_unchecked(&tempbytes[0..self.len()]) }.clone();
// clear our own string
self.len = 0;
self.bytes = [0; N];
// now copy over character by character, until just before the last character
let mut char_iter = tempstr.chars();
let mut maybe_c = char_iter.next();
loop {
match maybe_c {
Some(c) => {
let next_c = char_iter.next();
match next_c {
Some(_thing) => {
self.push(c).unwrap(); // always succeeds because we're re-encoding our string
maybe_c = next_c;
}
None => return next_c,
}
}
None => {
return None; // we should actually never get here because len() == 0 case already covered
}
}
}
}
pub fn push(&mut self, ch: char) -> core::result::Result<usize, Error> {
match ch.len_utf8() {
1 => {
@ -149,6 +92,32 @@ impl<const N: usize> String<N> {
}
}
impl<const N: usize> Default for String<N> {
fn default() -> Self {
Self::new()
}
}
impl<const N: usize> core::str::FromStr for String<N> {
type Err = &'static str;
fn from_str(src: &str) -> core::result::Result<String<N>, &'static str> {
let mut s = Self::new();
// Copy the string into our backing store.
for (&src_byte, dest_byte) in src.as_bytes().iter().zip(&mut s.bytes) {
*dest_byte = src_byte;
}
// Set the string length to the length of the passed-in String,
// or the maximum possible length. Which ever is smaller.
s.len = s.bytes.len().min(src.as_bytes().len()) as u32;
// If the string is not valid, set its length to 0.
if s.as_str().is_err() {
s.len = 0;
}
Ok(s)
}
}
impl<const N: usize> core::fmt::Display for String<N> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.to_str())