mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
rustc_serialize: specialize opaque encoding of some u8 sequences
This commit is contained in:
parent
417fe47065
commit
a4daa63a90
@ -203,7 +203,7 @@ fn encodable_body(
|
||||
#field_name,
|
||||
#field_idx,
|
||||
|__encoder|
|
||||
::rustc_serialize::Encodable::encode(#bind_ident, __encoder),
|
||||
::rustc_serialize::Encodable::<#encoder_ty>::encode(#bind_ident, __encoder),
|
||||
) {
|
||||
::std::result::Result::Ok(()) => (),
|
||||
::std::result::Result::Err(__err)
|
||||
@ -237,7 +237,7 @@ fn encodable_body(
|
||||
__encoder,
|
||||
#field_idx,
|
||||
|__encoder|
|
||||
::rustc_serialize::Encodable::encode(#bind_ident, __encoder),
|
||||
::rustc_serialize::Encodable::<#encoder_ty>::encode(#bind_ident, __encoder),
|
||||
) {
|
||||
::std::result::Result::Ok(()) => (),
|
||||
::std::result::Result::Err(__err)
|
||||
|
@ -1149,6 +1149,16 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// This ensures that the `Encodable<opaque::Encoder>::encode` specialization for byte slices
|
||||
// is used when a `CacheEncoder` having an `opaque::Encoder` is passed to `Encodable::encode`.
|
||||
// Unfortunately, we have to manually opt into specializations this way, given how `CacheEncoder`
|
||||
// and the encoding traits currently work.
|
||||
impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx, opaque::Encoder>> for [u8] {
|
||||
fn encode(&self, e: &mut CacheEncoder<'a, 'tcx, opaque::Encoder>) -> opaque::EncodeResult {
|
||||
self.encode(e.encoder)
|
||||
}
|
||||
}
|
||||
|
||||
// An integer that will always encode to 8 bytes.
|
||||
struct IntEncodedWithFixedSize(u64);
|
||||
|
||||
|
@ -11,12 +11,8 @@ use smallvec::{Array, SmallVec};
|
||||
|
||||
impl<S: Encoder, A: Array<Item: Encodable<S>>> Encodable<S> for SmallVec<A> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (i, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(i, |s| e.encode(s))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
let slice: &[A::Item] = self;
|
||||
slice.encode(s)
|
||||
}
|
||||
}
|
||||
|
||||
@ -292,12 +288,8 @@ where
|
||||
|
||||
impl<E: Encoder, T: Encodable<E>> Encodable<E> for Rc<[T]> {
|
||||
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (index, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(index, |s| e.encode(s))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
let slice: &[T] = self;
|
||||
slice.encode(s)
|
||||
}
|
||||
}
|
||||
|
||||
@ -315,12 +307,8 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<[T]> {
|
||||
|
||||
impl<E: Encoder, T: Encodable<E>> Encodable<E> for Arc<[T]> {
|
||||
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (index, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(index, |s| e.encode(s))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
let slice: &[T] = self;
|
||||
slice.encode(s)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,7 @@ Core encoding and decoding interfaces.
|
||||
#![feature(nll)]
|
||||
#![feature(associated_type_bounds)]
|
||||
#![cfg_attr(bootstrap, feature(min_const_generics))]
|
||||
#![feature(min_specialization)]
|
||||
#![cfg_attr(test, feature(test))]
|
||||
#![allow(rustc::internal)]
|
||||
|
||||
|
@ -316,3 +316,15 @@ impl<'a> serialize::Decoder for Decoder<'a> {
|
||||
err.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
// Specialize encoding byte slices. The default implementation for slices encodes and emits each
|
||||
// element individually. This isn't necessary for `u8` slices encoded with an `opaque::Encoder`,
|
||||
// because each `u8` is emitted as-is. Therefore, we can use a more efficient implementation. This
|
||||
// specialization applies to encoding `Vec<u8>`s, etc., since they call `encode` on their slices.
|
||||
impl serialize::Encodable<Encoder> for [u8] {
|
||||
fn encode(&self, e: &mut Encoder) -> EncodeResult {
|
||||
serialize::Encoder::emit_usize(e, self.len())?;
|
||||
e.emit_raw_bytes(self);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -527,7 +527,7 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<T> {
|
||||
}
|
||||
|
||||
impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T] {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
default fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_seq(self.len(), |s| {
|
||||
for (i, e) in self.iter().enumerate() {
|
||||
s.emit_seq_elt(i, |s| e.encode(s))?
|
||||
|
Loading…
Reference in New Issue
Block a user