mirror of
https://github.com/serde-rs/serde.git
synced 2025-09-30 14:31:53 +00:00
Use deref coercions when possible
This commit is contained in:
parent
361acd37d0
commit
3022d7301a
@ -937,19 +937,19 @@ fn manual_serialize_escape<W: Writer>(wr: &mut W, log: &Log) {
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "content_type").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.http.content_type.as_slice()).unwrap();
|
||||
escape_str(wr, &log.http.content_type).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "user_agent").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.http.user_agent.as_slice()).unwrap();
|
||||
escape_str(wr, &log.http.user_agent).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "referer").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.http.referer.as_slice()).unwrap();
|
||||
escape_str(wr, &log.http.referer).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "request_uri").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.http.request_uri.as_slice()).unwrap();
|
||||
escape_str(wr, &log.http.request_uri).unwrap();
|
||||
|
||||
wr.write_str("},").unwrap();
|
||||
escape_str(wr, "origin").unwrap();
|
||||
@ -957,7 +957,7 @@ fn manual_serialize_escape<W: Writer>(wr: &mut W, log: &Log) {
|
||||
|
||||
escape_str(wr, "ip").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.origin.ip.as_slice()).unwrap();
|
||||
escape_str(wr, &log.origin.ip).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "port").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
@ -965,7 +965,7 @@ fn manual_serialize_escape<W: Writer>(wr: &mut W, log: &Log) {
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "hostname").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.origin.hostname.as_slice()).unwrap();
|
||||
escape_str(wr, &log.origin.hostname).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "protocol").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
@ -982,15 +982,15 @@ fn manual_serialize_escape<W: Writer>(wr: &mut W, log: &Log) {
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "server_ip").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.server_ip.as_slice()).unwrap();
|
||||
escape_str(wr, &log.server_ip).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "server_name").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.server_name.as_slice()).unwrap();
|
||||
escape_str(wr, &log.server_name).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "remote_ip").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.remote_ip.as_slice()).unwrap();
|
||||
escape_str(wr, &log.remote_ip).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "bytes_dlv").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
@ -999,7 +999,7 @@ fn manual_serialize_escape<W: Writer>(wr: &mut W, log: &Log) {
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "ray_id").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.ray_id.as_slice()).unwrap();
|
||||
escape_str(wr, &log.ray_id).unwrap();
|
||||
wr.write_str("}").unwrap();
|
||||
}
|
||||
|
||||
@ -1172,11 +1172,11 @@ fn direct<W: Writer>(wr: &mut W, log: &Log) {
|
||||
serializer.serialize_struct_elt("origin", &log.origin).unwrap();
|
||||
serializer.serialize_struct_elt("country", &(log.country as usize)).unwrap();
|
||||
serializer.serialize_struct_elt("cache_status", &(log.cache_status as usize)).unwrap();
|
||||
serializer.serialize_struct_elt("server_ip", &log.server_ip.as_slice()).unwrap();
|
||||
serializer.serialize_struct_elt("server_name", &log.server_name.as_slice()).unwrap();
|
||||
serializer.serialize_struct_elt("remote_ip", &log.remote_ip.as_slice()).unwrap();
|
||||
serializer.serialize_struct_elt("server_ip", &log.server_ip).unwrap();
|
||||
serializer.serialize_struct_elt("server_name", &log.server_name).unwrap();
|
||||
serializer.serialize_struct_elt("remote_ip", &log.remote_ip).unwrap();
|
||||
serializer.serialize_struct_elt("bytes_dlv", &log.bytes_dlv).unwrap();
|
||||
serializer.serialize_struct_elt("ray_id", &log.ray_id.as_slice()).unwrap();
|
||||
serializer.serialize_struct_elt("ray_id", &log.ray_id).unwrap();
|
||||
|
||||
serializer.serialize_struct_end().unwrap();
|
||||
}
|
||||
|
@ -1247,19 +1247,19 @@ fn manual_serialize_escape<W: Writer>(wr: &mut W, log: &Log) {
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "content_type").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.http.content_type.as_slice()).unwrap();
|
||||
escape_str(wr, &log.http.content_type).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "user_agent").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.http.user_agent.as_slice()).unwrap();
|
||||
escape_str(wr, &log.http.user_agent).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "referer").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.http.referer.as_slice()).unwrap();
|
||||
escape_str(wr, &log.http.referer).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "request_uri").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.http.request_uri.as_slice()).unwrap();
|
||||
escape_str(wr, &log.http.request_uri).unwrap();
|
||||
|
||||
wr.write_str("},").unwrap();
|
||||
escape_str(wr, "origin").unwrap();
|
||||
@ -1267,7 +1267,7 @@ fn manual_serialize_escape<W: Writer>(wr: &mut W, log: &Log) {
|
||||
|
||||
escape_str(wr, "ip").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.origin.ip.as_slice()).unwrap();
|
||||
escape_str(wr, &log.origin.ip).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "port").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
@ -1275,7 +1275,7 @@ fn manual_serialize_escape<W: Writer>(wr: &mut W, log: &Log) {
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "hostname").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.origin.hostname.as_slice()).unwrap();
|
||||
escape_str(wr, &log.origin.hostname).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "protocol").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
@ -1292,15 +1292,15 @@ fn manual_serialize_escape<W: Writer>(wr: &mut W, log: &Log) {
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "server_ip").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.server_ip.as_slice()).unwrap();
|
||||
escape_str(wr, &log.server_ip).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "server_name").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.server_name.as_slice()).unwrap();
|
||||
escape_str(wr, &log.server_name).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "remote_ip").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.remote_ip.as_slice()).unwrap();
|
||||
escape_str(wr, &log.remote_ip).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "bytes_dlv").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
@ -1309,7 +1309,7 @@ fn manual_serialize_escape<W: Writer>(wr: &mut W, log: &Log) {
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(wr, "ray_id").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
escape_str(wr, log.ray_id.as_slice()).unwrap();
|
||||
escape_str(wr, &log.ray_id).unwrap();
|
||||
wr.write_str("}").unwrap();
|
||||
}
|
||||
|
||||
|
@ -315,7 +315,7 @@ fn deserialize_substructure(cx: &mut ExtCtxt, span: Span,
|
||||
cx,
|
||||
span,
|
||||
substr.type_ident,
|
||||
fields.as_slice(),
|
||||
&fields,
|
||||
deserializer,
|
||||
token)
|
||||
}
|
||||
@ -460,7 +460,7 @@ fn deserialize_struct_from_map(
|
||||
{
|
||||
let key = match token {
|
||||
::serde2::de::Str(s) => s,
|
||||
::serde2::de::String(ref s) => s.as_slice(),
|
||||
::serde2::de::String(ref s) => &s,
|
||||
token => {
|
||||
let expected_tokens = [
|
||||
::serde2::de::StrKind,
|
||||
@ -552,7 +552,7 @@ fn deserialize_static_fields(
|
||||
getarg(
|
||||
cx,
|
||||
span,
|
||||
token::intern_and_get_ident(format!("_field{}", i).as_slice())
|
||||
token::intern_and_get_ident(&format!("_field{}", i))
|
||||
)
|
||||
}).collect();
|
||||
|
||||
|
@ -139,7 +139,7 @@ pub trait Visitor {
|
||||
fn visit_string<
|
||||
E: Error,
|
||||
>(&mut self, v: String) -> Result<Self::Value, E> {
|
||||
self.visit_str(&v[])
|
||||
self.visit_str(&v)
|
||||
}
|
||||
|
||||
fn visit_unit<
|
||||
|
@ -106,7 +106,7 @@ impl<Iter: Iterator<Item=u8>> Parser<Iter> {
|
||||
b'0' ... b'9' | b'-' => self.parse_number(visitor),
|
||||
b'"' => {
|
||||
try!(self.parse_string());
|
||||
let s = str::from_utf8(self.buf.as_slice()).unwrap();
|
||||
let s = str::from_utf8(&self.buf).unwrap();
|
||||
visitor.visit_str(s)
|
||||
}
|
||||
b'[' => {
|
||||
@ -334,7 +334,7 @@ impl<Iter: Iterator<Item=u8>> Parser<Iter> {
|
||||
}
|
||||
|
||||
let buf = &[n1, try!(self.decode_hex_escape())];
|
||||
match ::unicode::str::utf16_items(buf.as_slice()).next() {
|
||||
match ::unicode::str::utf16_items(buf).next() {
|
||||
Some(Utf16Item::ScalarValue(c)) => c,
|
||||
_ => {
|
||||
return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape));
|
||||
|
@ -93,7 +93,7 @@ impl error::Error for Error {
|
||||
Error::SyntaxError(..) => "syntax error",
|
||||
Error::IoError(ref error) => error.description(),
|
||||
/*
|
||||
Error::ExpectedError(ref expected, _) => expected.as_slice(),
|
||||
Error::ExpectedError(ref expected, _) => &expected,
|
||||
Error::MissingFieldError(_) => "missing field",
|
||||
Error::UnknownVariantError(_) => "unknown variant",
|
||||
*/
|
||||
|
@ -35,7 +35,7 @@ impl ser::Serialize for Value {
|
||||
visitor.visit_f64(v)
|
||||
}
|
||||
Value::String(ref v) => {
|
||||
visitor.visit_str(v.as_slice())
|
||||
visitor.visit_str(&v)
|
||||
}
|
||||
Value::Array(ref v) => {
|
||||
v.visit(visitor)
|
||||
|
@ -143,7 +143,7 @@ fn serialize_substructure(cx: &ExtCtxt,
|
||||
let name = match (serial_name, name) {
|
||||
(Some(serial), _) => serial.clone(),
|
||||
(None, Some(id)) => token::get_ident(id),
|
||||
(None, None) => token::intern_and_get_ident(format!("_field{}", i).as_slice()),
|
||||
(None, None) => token::intern_and_get_ident(&format!("_field{}", i)),
|
||||
};
|
||||
|
||||
let name = cx.expr_str(span, name);
|
||||
@ -261,7 +261,7 @@ fn deserialize_substructure(cx: &mut ExtCtxt,
|
||||
cx,
|
||||
span,
|
||||
substr.type_ident,
|
||||
definition.fields.as_slice(),
|
||||
&definition.fields,
|
||||
fields,
|
||||
deserializer.clone(),
|
||||
token)
|
||||
@ -271,8 +271,8 @@ fn deserialize_substructure(cx: &mut ExtCtxt,
|
||||
cx,
|
||||
span,
|
||||
substr.type_ident,
|
||||
definition.variants.as_slice(),
|
||||
fields.as_slice(),
|
||||
&definition.variants,
|
||||
&fields,
|
||||
deserializer,
|
||||
token)
|
||||
}
|
||||
@ -300,7 +300,7 @@ fn deserialize_struct(
|
||||
let field_idents: Vec<ast::Ident> = fields.iter()
|
||||
.enumerate()
|
||||
.map(|(idx, _)| {
|
||||
cx.ident_of(format!("field{}", idx).as_slice())
|
||||
cx.ident_of(&format!("field{}", idx))
|
||||
})
|
||||
.collect();
|
||||
|
||||
@ -415,7 +415,7 @@ fn deserialize_enum(
|
||||
cx,
|
||||
span,
|
||||
path,
|
||||
serial_names.as_slice(),
|
||||
&serial_names,
|
||||
parts,
|
||||
|&: cx, _, _| {
|
||||
quote_expr!(cx, try!($deserializer.expect_enum_elt()))
|
||||
@ -461,7 +461,7 @@ fn deserialize_static_fields<F>(
|
||||
getarg(
|
||||
cx,
|
||||
span,
|
||||
token::intern_and_get_ident(format!("_field{}", i).as_slice())
|
||||
token::intern_and_get_ident(&format!("_field{}", i))
|
||||
)
|
||||
}).collect();
|
||||
|
||||
|
@ -381,7 +381,7 @@ impl<Iter: Iterator<Item=u8>> Parser<Iter> {
|
||||
}
|
||||
|
||||
let buf = &[n1, try!(self.decode_hex_escape())];
|
||||
match ::unicode::str::utf16_items(buf.as_slice()).next() {
|
||||
match ::unicode::str::utf16_items(buf).next() {
|
||||
Some(Utf16Item::ScalarValue(c)) => c,
|
||||
_ => {
|
||||
return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape));
|
||||
@ -410,7 +410,7 @@ impl<Iter: Iterator<Item=u8>> Parser<Iter> {
|
||||
match ch {
|
||||
b'"' => {
|
||||
self.bump();
|
||||
return Ok(str::from_utf8(self.buf.as_slice()).unwrap());
|
||||
return Ok(str::from_utf8(&self.buf).unwrap());
|
||||
}
|
||||
b'\\' => {
|
||||
escape = true;
|
||||
|
@ -93,7 +93,7 @@ impl error::Error for Error {
|
||||
match *self {
|
||||
Error::SyntaxError(..) => "syntax error",
|
||||
Error::IoError(ref error) => error.description(),
|
||||
Error::ExpectedError(ref expected, _) => expected.as_slice(),
|
||||
Error::ExpectedError(ref expected, _) => &expected,
|
||||
Error::MissingFieldError(_) => "missing field",
|
||||
Error::UnknownVariantError(_) => "unknown variant",
|
||||
}
|
||||
|
@ -232,7 +232,7 @@ fn main() {
|
||||
|
||||
// To deserialize use the `json::from_str` function.
|
||||
|
||||
let deserialized_object: TestStruct1 = match json::from_str(serialized_str.as_slice()) {
|
||||
let deserialized_object: TestStruct1 = match json::from_str(&serialized_str) {
|
||||
Ok(deserialized_object) => deserialized_object,
|
||||
Err(e) => panic!("json deserialization error: {:?}", e),
|
||||
};
|
||||
@ -1379,7 +1379,7 @@ mod tests {
|
||||
hm.serialize(&mut serializer).unwrap();
|
||||
}
|
||||
let bytes = mem_buf.unwrap();
|
||||
let json_str = from_utf8(bytes.as_slice()).unwrap();
|
||||
let json_str = from_utf8(&bytes).unwrap();
|
||||
let _json_value: Value = from_str(json_str).unwrap();
|
||||
}
|
||||
#[test]
|
||||
@ -1394,7 +1394,7 @@ mod tests {
|
||||
hm.serialize(&mut serializer).unwrap()
|
||||
}
|
||||
let bytes = mem_buf.unwrap();
|
||||
let json_str = from_utf8(bytes.as_slice()).unwrap();
|
||||
let json_str = from_utf8(&bytes).unwrap();
|
||||
let _json_value: Value = from_str(json_str).unwrap();
|
||||
}
|
||||
|
||||
@ -1419,7 +1419,7 @@ mod tests {
|
||||
None => { break; }
|
||||
};
|
||||
let (ref expected_evt, ref expected_stack) = expected[i];
|
||||
if !parser.stack().is_equal_to(expected_stack.as_slice()) {
|
||||
if !parser.stack().is_equal_to(&expected_stack) {
|
||||
panic!("Parser stack is not equal to {}", expected_stack);
|
||||
}
|
||||
assert_eq!(&evt, expected_evt);
|
||||
@ -1827,7 +1827,7 @@ mod bench {
|
||||
let src = json_str(count);
|
||||
let json = encoder_json(count);
|
||||
b.iter(|| {
|
||||
assert_eq!(json, serialize::json::from_str(src.as_slice()).unwrap());
|
||||
assert_eq!(json, serialize::json::from_str(&src).unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
@ -1835,7 +1835,7 @@ mod bench {
|
||||
let src = json_str(count);
|
||||
let json = encoder_json(count);
|
||||
b.iter(|| {
|
||||
assert_eq!(json, serialize::json::from_str(src.as_slice()).unwrap());
|
||||
assert_eq!(json, serialize::json::from_str(&src).unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -53,14 +53,14 @@ fn escape_char<W: Writer>(wr: &mut W, v: char) -> IoResult<()> {
|
||||
fn fmt_f32_or_null<W: Writer>(wr: &mut W, v: f32) -> IoResult<()> {
|
||||
match v.classify() {
|
||||
FpCategory::Nan | FpCategory::Infinite => wr.write_str("null"),
|
||||
_ => wr.write_str(f32::to_str_digits(v, 6).as_slice()),
|
||||
_ => wr.write_str(&f32::to_str_digits(v, 6)),
|
||||
}
|
||||
}
|
||||
|
||||
fn fmt_f64_or_null<W: Writer>(wr: &mut W, v: f64) -> IoResult<()> {
|
||||
match v.classify() {
|
||||
FpCategory::Nan | FpCategory::Infinite => wr.write_str("null"),
|
||||
_ => wr.write_str(f64::to_str_digits(v, 6).as_slice()),
|
||||
_ => wr.write_str(&f64::to_str_digits(v, 6)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ impl Value {
|
||||
pub fn to_pretty_string(&self) -> String {
|
||||
let mut wr = Vec::new();
|
||||
self.to_pretty_writer(wr.by_ref()).unwrap();
|
||||
str::from_utf8(wr.as_slice()).unwrap().to_string()
|
||||
str::from_utf8(&wr).unwrap().to_string()
|
||||
}
|
||||
|
||||
/// If the Json value is an Object, returns the value associated with the provided key.
|
||||
@ -130,7 +130,7 @@ impl Value {
|
||||
/// Returns None otherwise.
|
||||
pub fn as_string<'a>(&'a self) -> Option<&'a str> {
|
||||
match *self {
|
||||
Value::String(ref s) => Some(s.as_slice()),
|
||||
Value::String(ref s) => Some(&s),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
@ -212,7 +212,7 @@ impl ToString for Value {
|
||||
fn to_string(&self) -> String {
|
||||
let mut wr = Vec::new();
|
||||
self.to_writer(wr.by_ref()).unwrap();
|
||||
str::from_utf8(wr.as_slice()).unwrap().to_string()
|
||||
str::from_utf8(&wr).unwrap().to_string()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,6 +26,6 @@ fn test_json_struct() {
|
||||
let s = serde::json::to_string(&input).unwrap();
|
||||
assert_eq!(s.as_slice(), r#"{"$schema":"a","title":"b","type":3}"#);
|
||||
|
||||
let output: Test = serde::json::from_str(s.as_slice()).unwrap();
|
||||
let output: Test = serde::json::from_str(&s).unwrap();
|
||||
assert_eq!(input, output);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user