FIX: Fix serde feature for const gen

This commit is contained in:
bluss
2021-03-23 17:49:11 +01:00
parent c9b095f263
commit 6daae9ae68
5 changed files with 22 additions and 25 deletions
+3 -3
View File
@@ -533,10 +533,10 @@ impl<'de, const CAP: usize> Deserialize<'de> for ArrayString<CAP>
use serde::de::{self, Visitor};
use std::marker::PhantomData;
struct ArrayStringVisitor<const CAP: usize>(PhantomData([u8; CAP]));
struct ArrayStringVisitor<const CAP: usize>(PhantomData<[u8; CAP]>);
impl<'de, const CAP: usize> Visitor<'de> for ArrayStringVisitor {
type Value = ArrayString;
impl<'de, const CAP: usize> Visitor<'de> for ArrayStringVisitor<CAP> {
type Value = ArrayString<CAP>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a string no more than {} bytes long", CAP)
+3 -3
View File
@@ -1141,17 +1141,17 @@ impl<'de, T: Deserialize<'de>, const CAP: usize> Deserialize<'de> for ArrayVec<T
type Value = ArrayVec<T, CAP>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "an array with no more than {} items", A::CAPACITY)
write!(formatter, "an array with no more than {} items", CAP)
}
fn visit_seq<SA>(self, mut seq: SA) -> Result<Self::Value, SA::Error>
where SA: SeqAccess<'de>,
{
let mut values = ArrayVec::<A>::new();
let mut values = ArrayVec::<T, CAP>::new();
while let Some(value) = seq.next_element()? {
if let Err(_) = values.try_push(value) {
return Err(SA::Error::invalid_length(A::CAPACITY + 1, &self));
return Err(SA::Error::invalid_length(CAP + 1, &self));
}
}
-3
View File
@@ -34,9 +34,6 @@ extern crate serde;
#[cfg(not(feature="std"))]
extern crate core as std;
#[cfg(feature="serde")]
use serde::{Serialize, Deserialize, Serializer, Deserializer};
mod array;
mod arrayvec_impl;
mod arrayvec;
+6 -6
View File
@@ -9,7 +9,7 @@ mod array_vec {
#[test]
fn test_ser_de_empty() {
let vec = ArrayVec::<[u32; 0]>::new();
let vec = ArrayVec::<u32, 0>::new();
assert_tokens(&vec, &[
Token::Seq { len: Some(0) },
@@ -20,7 +20,7 @@ mod array_vec {
#[test]
fn test_ser_de() {
let mut vec = ArrayVec::<[u32; 3]>::new();
let mut vec = ArrayVec::<u32, 3>::new();
vec.push(20);
vec.push(55);
vec.push(123);
@@ -36,7 +36,7 @@ mod array_vec {
#[test]
fn test_de_too_large() {
assert_de_tokens_error::<ArrayVec<[u32; 2]>>(&[
assert_de_tokens_error::<ArrayVec<u32, 2>>(&[
Token::Seq { len: Some(3) },
Token::U32(13),
Token::U32(42),
@@ -52,7 +52,7 @@ mod array_string {
#[test]
fn test_ser_de_empty() {
let string = ArrayString::<[u8; 0]>::new();
let string = ArrayString::<0>::new();
assert_tokens(&string, &[
Token::Str(""),
@@ -62,7 +62,7 @@ mod array_string {
#[test]
fn test_ser_de() {
let string = ArrayString::<[u8; 9]>::from("1234 abcd")
let string = ArrayString::<9>::from("1234 abcd")
.expect("expected exact specified capacity to be enough");
assert_tokens(&string, &[
@@ -72,7 +72,7 @@ mod array_string {
#[test]
fn test_de_too_large() {
assert_de_tokens_error::<ArrayString<[u8; 2]>>(&[
assert_de_tokens_error::<ArrayString<2>>(&[
Token::Str("afd")
], "invalid length 3, expected a string no more than 2 bytes long");
}
+10 -10
View File
@@ -483,7 +483,7 @@ fn test_string() {
use std::error::Error;
let text = "hello world";
let mut s = ArrayString::<[_; 16]>::new();
let mut s = ArrayString::<16>::new();
s.try_push_str(text).unwrap();
assert_eq!(&s, text);
assert_eq!(text, &s);
@@ -493,7 +493,7 @@ fn test_string() {
map.insert(s, 1);
assert_eq!(map[text], 1);
let mut t = ArrayString::<[_; 2]>::new();
let mut t = ArrayString::<2>::new();
assert!(t.try_push_str(text).is_err());
assert_eq!(&t, "");
@@ -504,7 +504,7 @@ fn test_string() {
// Test Error trait / try
let t = || -> Result<(), Box<dyn Error>> {
let mut t = ArrayString::<[_; 2]>::new();
let mut t = ArrayString::<2>::new();
t.try_push_str(text)?;
Ok(())
}();
@@ -515,7 +515,7 @@ fn test_string() {
fn test_string_from() {
let text = "hello world";
// Test `from` constructor
let u = ArrayString::<[_; 11]>::from(text).unwrap();
let u = ArrayString::<11>::from(text).unwrap();
assert_eq!(&u, text);
assert_eq!(u.len(), text.len());
}
@@ -523,7 +523,7 @@ fn test_string_from() {
#[test]
fn test_string_parse_from_str() {
let text = "hello world";
let u: ArrayString<[_; 11]> = text.parse().unwrap();
let u: ArrayString<11> = text.parse().unwrap();
assert_eq!(&u, text);
assert_eq!(u.len(), text.len());
}
@@ -539,9 +539,9 @@ fn test_string_from_bytes() {
#[test]
fn test_string_clone() {
let text = "hi";
let mut s = ArrayString::<[_; 4]>::new();
let mut s = ArrayString::<4>::new();
s.push_str("abcd");
let t = ArrayString::<[_; 4]>::from(text).unwrap();
let t = ArrayString::<4>::from(text).unwrap();
s.clone_from(&t);
assert_eq!(&t, &s);
}
@@ -549,7 +549,7 @@ fn test_string_clone() {
#[test]
fn test_string_push() {
let text = "abcαβγ";
let mut s = ArrayString::<[_; 8]>::new();
let mut s = ArrayString::<8>::new();
for c in text.chars() {
if let Err(_) = s.try_push(c) {
break;
@@ -645,7 +645,7 @@ fn test_sizes() {
#[test]
fn test_default() {
use std::net;
let s: ArrayString<[u8; 4]> = Default::default();
let s: ArrayString<4> = Default::default();
// Something without `Default` implementation.
let v: ArrayVec<net::TcpStream, 4> = Default::default();
assert_eq!(s.len(), 0);
@@ -689,6 +689,6 @@ fn test_extend_zst() {
#[test]
fn test_try_from_argument() {
use core::convert::TryFrom;
let v = ArrayString::<[u8; 16]>::try_from(format_args!("Hello {}", 123)).unwrap();
let v = ArrayString::<16>::try_from(format_args!("Hello {}", 123)).unwrap();
assert_eq!(&v, "Hello 123");
}