Skip to content

Commit

Permalink
Removed unnecessary String interface, already covered by core
Browse files Browse the repository at this point in the history
  • Loading branch information
korken89 committed Apr 1, 2021
1 parent 73d209a commit 12682bd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 134 deletions.
14 changes: 8 additions & 6 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,15 @@ impl<'de, const N: usize> Deserialize<'de> for String<N> {
where
E: de::Error,
{
let mut bytes = Vec::new();
if bytes.extend_from_slice(v).is_err() {
return Err(E::invalid_value(de::Unexpected::Bytes(v), &self));
}
let mut s = String::new();

String::from_utf8(bytes)
.map_err(|_| E::invalid_value(de::Unexpected::Bytes(v), &self))
s.push_str(
core::str::from_utf8(v)
.map_err(|_| E::invalid_value(de::Unexpected::Bytes(v), &self))?,
)
.map_err(|_| E::invalid_length(v.len(), &self))?;

Ok(s)
}
}

Expand Down
129 changes: 1 addition & 128 deletions src/string.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
use core::{
fmt,
fmt::Write,
hash,
mem::{self, MaybeUninit},
ops, str,
str::Utf8Error,
};
use core::{fmt, fmt::Write, hash, ops, str};

use hash32;

Expand All @@ -16,13 +9,6 @@ pub struct String<const N: usize> {
vec: Vec<u8, N>,
}

// impl<const N: usize> String<N> {
// /// `String` `const` constructor; wrap the returned value in [`String`](../struct.String.html)
// pub const fn new() -> Self {
// Self { vec: Vec::new() }
// }
// }

impl<const N: usize> String<N> {
/// Constructs a new, empty `String` with a fixed capacity of `N`
///
Expand All @@ -44,65 +30,6 @@ impl<const N: usize> String<N> {
Self { vec: Vec::new() }
}

/// Converts a vector of bytes into a `String`.
///
/// A string slice ([`&str`]) is made of bytes ([`u8`]), and a vector of bytes
/// ([`Vec<u8>`]) is made of bytes, so this function converts between the
/// two. Not all byte slices are valid `String`s, however: `String`
/// requires that it is valid UTF-8. `from_utf8()` checks to ensure that
/// the bytes are valid UTF-8, and then does the conversion.
///
/// See std::String for further information.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use heapless::{String, Vec};
///
/// let mut v: Vec<u8, 8> = Vec::new();
/// v.push('a' as u8).unwrap();
/// v.push('b' as u8).unwrap();
///
/// let s = String::from_utf8(v).unwrap();
/// assert!(s.len() == 2);
/// ```
///
/// Incorrect bytes:
///
/// ```
/// use heapless::{String, Vec};
///
/// // some invalid bytes, in a vector
///
/// let mut v: Vec<u8, 8> = Vec::new();
/// v.push(0).unwrap();
/// v.push(159).unwrap();
/// v.push(146).unwrap();
/// v.push(150).unwrap();
/// assert!(String::from_utf8(v).is_err());
/// ```
#[inline]
pub fn from_utf8(vec: Vec<u8, N>) -> Result<String<N>, Utf8Error> {
// validate input
str::from_utf8(&*vec)?;

Ok(unsafe { String::from_utf8_unchecked(vec) })
}

/// Converts a vector of bytes to a `String` without checking that the
/// string contains valid UTF-8.
///
/// See the safe version, `from_utf8`, for more details.
#[inline]
pub unsafe fn from_utf8_unchecked(mut vec: Vec<u8, N>) -> String<N> {
// FIXME this may result in a memcpy at runtime
let vec_ = mem::replace(&mut vec, MaybeUninit::uninit().assume_init());
mem::forget(vec);
String { vec: vec_ }
}

/// Converts a `String` into a byte vector.
///
/// This consumes the `String`, so we do not need to copy its contents.
Expand Down Expand Up @@ -544,14 +471,6 @@ impl<const N: usize> PartialEq<String<N>> for &str {

impl<const N: usize> Eq for String<N> {}

// impl<const N: usize, D: core::fmt::Display> From<D> for String<N> {
// fn from(s: D) -> Self {
// let mut new = String::new();
// write!(&mut new, "{}", s).unwrap();
// new
// }
// }

macro_rules! impl_from_num {
($num:ty, $size:expr) => {
impl<const N: usize> From<$num> for String<N> {
Expand Down Expand Up @@ -647,52 +566,6 @@ mod tests {
let _: String<4> = String::from("12345");
}

#[test]
fn from_utf8() {
let mut v: Vec<u8, 8> = Vec::new();
v.push('a' as u8).unwrap();
v.push('b' as u8).unwrap();

let s = String::from_utf8(v).unwrap();
assert_eq!(s, "ab");
}

#[test]
fn from_utf8_uenc() {
let mut v: Vec<u8, 8> = Vec::new();
v.push(240).unwrap();
v.push(159).unwrap();
v.push(146).unwrap();
v.push(150).unwrap();

assert!(String::from_utf8(v).is_ok());
}

#[test]
fn from_utf8_uenc_err() {
let mut v: Vec<u8, 8> = Vec::new();
v.push(0).unwrap();
v.push(159).unwrap();
v.push(146).unwrap();
v.push(150).unwrap();

assert!(String::from_utf8(v).is_err());
}

#[test]
fn from_utf8_unchecked() {
let mut v: Vec<u8, 8> = Vec::new();
v.push(104).unwrap();
v.push(101).unwrap();
v.push(108).unwrap();
v.push(108).unwrap();
v.push(111).unwrap();

let s = unsafe { String::from_utf8_unchecked(v) };

assert_eq!(s, "hello");
}

#[test]
fn from_num() {
let v: String<20> = String::from(18446744073709551615 as u64);
Expand Down

0 comments on commit 12682bd

Please sign in to comment.