diff --git a/xous-rs/src/carton.rs b/xous-rs/src/carton.rs index 65a5c7772..642e2beb4 100644 --- a/xous-rs/src/carton.rs +++ b/xous-rs/src/carton.rs @@ -84,7 +84,7 @@ impl<'a> AsRef for Carton<'a> { impl<'a> AsRef<[u8]> for Carton<'a> { fn as_ref(&self) -> &[u8] { - &self.slice + self.slice } } diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index 379d7c920..9b7f5a877 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -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 for [u32; 4] { + fn from(s: SID) -> [u32; 4] { + s.0 } } diff --git a/xous-rs/src/string.rs b/xous-rs/src/string.rs index 4da58ba35..1b5706879 100644 --- a/xous-rs/src/string.rs +++ b/xous-rs/src/string.rs @@ -14,28 +14,6 @@ impl String { } } - pub fn from_str(src: T) -> String - where - T: AsRef, - { - 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 String { 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 { - 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 { match ch.len_utf8() { 1 => { @@ -149,6 +92,32 @@ impl String { } } +impl Default for String { + fn default() -> Self { + Self::new() + } +} + +impl core::str::FromStr for String { + type Err = &'static str; + fn from_str(src: &str) -> core::result::Result, &'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 core::fmt::Display for String { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "{}", self.to_str())