From 5dd077969cd140caa65d67bec3f87cef9304b43e Mon Sep 17 00:00:00 2001 From: Thomas Bahn Date: Fri, 6 Feb 2015 15:17:24 +0100 Subject: [PATCH] Use slicing syntax instead of `.as_slice()` --- benches/bench_log.rs | 32 ++++++++++++++++---------------- serde2/benches/bench_log.rs | 20 ++++++++++---------- serde2/serde2_macros/src/lib.rs | 2 +- serde2/src/json/de.rs | 2 +- serde2/src/json/ser.rs | 4 ++-- serde2/src/ser.rs | 4 ++-- serde_macros/src/lib.rs | 2 +- src/de.rs | 6 +++--- src/json/de.rs | 6 +++--- src/json/ser.rs | 6 +++--- src/json/value.rs | 2 +- src/ser.rs | 2 +- tests/json_struct.rs | 2 +- 13 files changed, 45 insertions(+), 45 deletions(-) diff --git a/benches/bench_log.rs b/benches/bench_log.rs index 0954cba9..f49e37e4 100755 --- a/benches/bench_log.rs +++ b/benches/bench_log.rs @@ -681,7 +681,7 @@ fn test_encoder() { log.encode(&mut encoder).unwrap(); } - assert_eq!(wr.as_slice(), JSON_STR.as_bytes()); + assert_eq!(&wr[], JSON_STR.as_bytes()); } #[bench] @@ -780,7 +780,7 @@ fn test_serializer_my_mem_writer0() { log.serialize(&mut serializer).unwrap(); } - assert_eq!(wr.buf.as_slice(), JSON_STR.as_bytes()); + assert_eq!(&wr.buf[], JSON_STR.as_bytes()); } #[bench] @@ -811,7 +811,7 @@ fn test_serializer_my_mem_writer1() { log.serialize(&mut serializer).unwrap(); } - assert_eq!(wr.buf.as_slice(), JSON_STR.as_bytes()); + assert_eq!(&wr.buf[], JSON_STR.as_bytes()); } #[bench] @@ -1011,7 +1011,7 @@ fn test_manual_serialize_vec_no_escape() { manual_serialize_no_escape(&mut wr, &log); let json = String::from_utf8(wr).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] @@ -1036,7 +1036,7 @@ fn test_manual_serialize_vec_escape() { manual_serialize_escape(&mut wr, &log); let json = String::from_utf8(wr).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] @@ -1062,7 +1062,7 @@ fn test_manual_serialize_my_mem_writer0_no_escape() { manual_serialize_no_escape(&mut wr, &log); let json = String::from_utf8(wr.buf).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] @@ -1088,7 +1088,7 @@ fn test_manual_serialize_my_mem_writer0_escape() { manual_serialize_escape(&mut wr, &log); let json = String::from_utf8(wr.buf).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] @@ -1114,7 +1114,7 @@ fn test_manual_serialize_my_mem_writer1_no_escape() { manual_serialize_no_escape(&mut wr, &log); let json = String::from_utf8(wr.buf).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] @@ -1140,7 +1140,7 @@ fn test_manual_serialize_my_mem_writer1_escape() { manual_serialize_escape(&mut wr, &log); let json = String::from_utf8(wr.buf).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] @@ -1189,7 +1189,7 @@ fn test_direct_vec() { direct(&mut wr, &log); let json = String::from_utf8(wr).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] @@ -1214,7 +1214,7 @@ fn test_direct_my_mem_writer0() { direct(&mut wr, &log); let json = String::from_utf8(wr.buf).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] @@ -1240,7 +1240,7 @@ fn test_direct_my_mem_writer1() { direct(&mut wr, &log); let json = String::from_utf8(wr.buf).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] @@ -1291,7 +1291,7 @@ fn test_deserializer() { #[inline] fn manual_reader_ignore(rdr: &mut R, buf: &mut [u8], key: &[u8]) { - let buf = buf.slice_mut(0, key.len()); + let buf = &mut buf[0..key.len()]; rdr.read(buf).unwrap(); assert_eq!(buf, key); } @@ -1350,7 +1350,7 @@ fn manual_reader_string(rdr: &mut R, buf: &mut [u8], key: &[u8]) -> S let b = rdr.read_byte().unwrap(); assert!(b == b',' || b == b']' || b == b'}'); - String::from_utf8(buf.slice_to(idx).to_vec()).unwrap() + String::from_utf8(buf[..idx].to_vec()).unwrap() } #[inline] @@ -1432,7 +1432,7 @@ fn manual_reader_deserialize(rdr: &mut R) -> Log { #[inline] fn manual_iter_ignore>(mut rdr: R, buf: &mut [u8], key: &[u8]) { - let buf = buf.slice_mut(0, key.len()); + let buf = &mut buf[0..key.len()]; for idx in range(0, key.len()) { buf[idx] = rdr.next().unwrap(); @@ -1494,7 +1494,7 @@ fn manual_iter_string>(mut rdr: R, buf: &mut [u8], key: &[u let b = rdr.next().unwrap(); assert!(b == b',' || b == b']' || b == b'}'); - String::from_utf8(buf.slice_to(idx).to_vec()).unwrap() + String::from_utf8(buf[..idx].to_vec()).unwrap() } #[inline] diff --git a/serde2/benches/bench_log.rs b/serde2/benches/bench_log.rs index 8acbde86..b6590dac 100644 --- a/serde2/benches/bench_log.rs +++ b/serde2/benches/bench_log.rs @@ -989,7 +989,7 @@ fn test_encoder() { log.encode(&mut encoder).unwrap(); } - assert_eq!(wr.as_slice(), JSON_STR.as_bytes()); + assert_eq!(&wr[], JSON_STR.as_bytes()); } #[bench] @@ -1039,7 +1039,7 @@ fn test_serializer_vec() { serializer.visit(&log).unwrap(); let json = serializer.into_inner(); - assert_eq!(json.as_slice(), JSON_STR.as_bytes()); + assert_eq!(&json[], JSON_STR.as_bytes()); } #[bench] @@ -1089,7 +1089,7 @@ fn test_serializer_my_mem_writer0() { let _json = serializer.into_inner(); } - assert_eq!(wr.buf.as_slice(), JSON_STR.as_bytes()); + assert_eq!(&wr.buf[], JSON_STR.as_bytes()); } #[bench] @@ -1121,7 +1121,7 @@ fn test_serializer_my_mem_writer1() { let _json = serializer.into_inner(); } - assert_eq!(wr.buf.as_slice(), JSON_STR.as_bytes()); + assert_eq!(&wr.buf[], JSON_STR.as_bytes()); } #[bench] @@ -1321,7 +1321,7 @@ fn test_manual_serialize_vec_no_escape() { manual_serialize_no_escape(&mut wr, &log); let json = String::from_utf8(wr).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] @@ -1346,7 +1346,7 @@ fn test_manual_serialize_vec_escape() { manual_serialize_escape(&mut wr, &log); let json = String::from_utf8(wr).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] @@ -1372,7 +1372,7 @@ fn test_manual_serialize_my_mem_writer0_no_escape() { manual_serialize_no_escape(&mut wr, &log); let json = String::from_utf8(wr.buf).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] @@ -1398,7 +1398,7 @@ fn test_manual_serialize_my_mem_writer0_escape() { manual_serialize_escape(&mut wr, &log); let json = String::from_utf8(wr.buf).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] @@ -1424,7 +1424,7 @@ fn test_manual_serialize_my_mem_writer1_no_escape() { manual_serialize_no_escape(&mut wr, &log); let json = String::from_utf8(wr.buf).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] @@ -1450,7 +1450,7 @@ fn test_manual_serialize_my_mem_writer1_escape() { manual_serialize_escape(&mut wr, &log); let json = String::from_utf8(wr.buf).unwrap(); - assert_eq!(JSON_STR, json.as_slice()); + assert_eq!(JSON_STR, &json[]); } #[bench] diff --git a/serde2/serde2_macros/src/lib.rs b/serde2/serde2_macros/src/lib.rs index 52dfe2a5..5bd233fa 100644 --- a/serde2/serde2_macros/src/lib.rs +++ b/serde2/serde2_macros/src/lib.rs @@ -402,7 +402,7 @@ fn deserialize_struct_from_map( ) -> P { let fields = match *fields { Unnamed(_) => fail!(), - Named(ref fields) => fields.as_slice(), + Named(ref fields) => &fields[], }; // Declare each field. diff --git a/serde2/src/json/de.rs b/serde2/src/json/de.rs index 609004b6..bd6a6e4e 100644 --- a/serde2/src/json/de.rs +++ b/serde2/src/json/de.rs @@ -352,7 +352,7 @@ impl> Parser { let buf = &mut [0; 4]; let len = c.encode_utf8(buf).unwrap_or(0); - self.buf.extend(buf.slice_to(len).iter().map(|b| *b)); + self.buf.extend(buf[..len].iter().map(|b| *b)); } _ => { return Err(self.error(ErrorCode::InvalidEscape)); diff --git a/serde2/src/json/ser.rs b/serde2/src/json/ser.rs index d51b2db8..0749a42a 100644 --- a/serde2/src/json/ser.rs +++ b/serde2/src/json/ser.rs @@ -206,7 +206,7 @@ pub fn escape_bytes(wr: &mut W, bytes: &[u8]) -> Result<(), IoErr }; if start < i { - try!(wr.write(bytes.slice(start, i))); + try!(wr.write(&bytes[start..i])); } try!(wr.write_str(escaped)); @@ -215,7 +215,7 @@ pub fn escape_bytes(wr: &mut W, bytes: &[u8]) -> Result<(), IoErr } if start != bytes.len() { - try!(wr.write(bytes.slice_from(start))); + try!(wr.write(&bytes[start..])); } wr.write_str("\"") diff --git a/serde2/src/ser.rs b/serde2/src/ser.rs index bf6ac6de..7f3ae6bb 100644 --- a/serde2/src/ser.rs +++ b/serde2/src/ser.rs @@ -239,7 +239,7 @@ impl Serialize for String { fn visit< V: Visitor, >(&self, visitor: &mut V) -> Result { - self.as_slice().visit(visitor) + (&self[]).visit(visitor) } } @@ -321,7 +321,7 @@ impl< fn visit< V: Visitor, >(&self, visitor: &mut V) -> Result { - self.as_slice().visit(visitor) + (&self[]).visit(visitor) } } diff --git a/serde_macros/src/lib.rs b/serde_macros/src/lib.rs index 201b551d..ab8314ab 100644 --- a/serde_macros/src/lib.rs +++ b/serde_macros/src/lib.rs @@ -293,7 +293,7 @@ fn deserialize_struct( let fields = match *fields { Unnamed(_) => panic!(), - Named(ref fields) => fields.as_slice(), + Named(ref fields) => &fields[], }; // Convert each field into a unique ident. diff --git a/src/de.rs b/src/de.rs index d6625698..903bbec2 100644 --- a/src/de.rs +++ b/src/de.rs @@ -295,8 +295,8 @@ pub trait Deserializer: Iterator> + Sized { Token::Str(value) if value.chars().count() == 1 => { Ok(value.char_at(0)) } - Token::String(ref value) if value.as_slice().chars().count() == 1 => { - Ok(value.as_slice().char_at(0)) + Token::String(ref value) if value[].chars().count() == 1 => { + Ok(value[].char_at(0)) } token => { static EXPECTED_TOKENS: &'static [TokenKind] = &[ @@ -410,7 +410,7 @@ pub trait Deserializer: Iterator> + Sized { Ok(Some(fields.iter().position(|field| *field == n))) } Token::String(n) => { - Ok(Some(fields.iter().position(|field| *field == n.as_slice()))) + Ok(Some(fields.iter().position(|field| *field == &n[]))) } token => { Err(self.syntax_error(token, STR_TOKEN_KINDS)) diff --git a/src/json/de.rs b/src/json/de.rs index f69425b7..ff13a397 100644 --- a/src/json/de.rs +++ b/src/json/de.rs @@ -399,7 +399,7 @@ impl> Parser { let buf = &mut [0u8; 4]; let len = c.encode_utf8(buf).unwrap_or(0); - self.buf.extend(buf.slice_to(len).iter().map(|b| *b)); + self.buf.extend(buf[..len].iter().map(|b| *b)); } _ => { return Err(self.error(ErrorCode::InvalidEscape)); @@ -580,7 +580,7 @@ impl> de::Deserializer for Parser { _ => { return Err(self.error(ErrorCode::ExpectedEnumToken)); } } - match variants.iter().position(|v| *v == variant.as_slice()) { + match variants.iter().position(|v| *v == &variant[]) { Some(idx) => Ok(idx), None => Err(self.error(ErrorCode::UnknownVariant)), } @@ -631,7 +631,7 @@ impl> de::Deserializer for Parser { None => { return Ok(None); } }; - Ok(Some(fields.iter().position(|field| *field == s.as_slice()))) + Ok(Some(fields.iter().position(|field| *field == &s[]))) } } diff --git a/src/json/ser.rs b/src/json/ser.rs index 5b11f885..79751329 100644 --- a/src/json/ser.rs +++ b/src/json/ser.rs @@ -25,7 +25,7 @@ fn escape_bytes(wr: &mut W, bytes: &[u8]) -> IoResult<()> { }; if start < i { - try!(wr.write(bytes.slice(start, i))); + try!(wr.write(&bytes[start..i])); } try!(wr.write_str(escaped)); @@ -34,7 +34,7 @@ fn escape_bytes(wr: &mut W, bytes: &[u8]) -> IoResult<()> { } if start != bytes.len() { - try!(wr.write(bytes.slice_from(start))); + try!(wr.write(&bytes[start..])); } wr.write_str("\"") @@ -74,7 +74,7 @@ fn spaces(wr: &mut W, mut n: usize) -> IoResult<()> { } if n > 0 { - wr.write(BUF.slice_to(n)) + wr.write(&BUF[..n]) } else { Ok(()) } diff --git a/src/json/value.rs b/src/json/value.rs index d3abaecc..1a8cdeec 100644 --- a/src/json/value.rs +++ b/src/json/value.rs @@ -498,7 +498,7 @@ impl de::Deserializer for Deserializer { } }; - match variants.iter().position(|v| *v == variant.as_slice()) { + match variants.iter().position(|v| *v == &variant[]) { Some(idx) => Ok(idx), None => Err(Error::UnknownVariantError(variant)), } diff --git a/src/ser.rs b/src/ser.rs index b0e12154..5a18d4ad 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -158,7 +158,7 @@ impl<'a, S: Serializer, E> Serialize for &'a str { impl, E> Serialize for String { #[inline] fn serialize(&self, s: &mut S) -> Result<(), E> { - self.as_slice().serialize(s) + (&self[]).serialize(s) } } diff --git a/tests/json_struct.rs b/tests/json_struct.rs index 770439ff..f5b58a99 100644 --- a/tests/json_struct.rs +++ b/tests/json_struct.rs @@ -24,7 +24,7 @@ fn test_json_struct() { }; let s = serde::json::to_string(&input).unwrap(); - assert_eq!(s.as_slice(), r#"{"$schema":"a","title":"b","type":3}"#); + assert_eq!(&s[], r#"{"$schema":"a","title":"b","type":3}"#); let output: Test = serde::json::from_str(&s).unwrap(); assert_eq!(input, output);