pub fn encode_with_shorthand<E, T, M>(encoder: &mut E,
                                      value: &T,
                                      cache: M)
                                      -> Result<(), E::Error>
    where E: TyEncoder,
          M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>,
          T: EncodableWithShorthand,
{
    let existing_shorthand = cache(encoder).get(value).cloned();
    if let Some(shorthand) = existing_shorthand {
        return encoder.emit_usize(shorthand);
    }

    let variant = value.variant();

    let start = encoder.position();
    variant.encode(encoder)?;
    let len = encoder.position() - start;

    // The shorthand encoding uses the same usize as the
    // discriminant, with an offset so they can't conflict.
    let discriminant = unsafe { intrinsics::discriminant_value(variant) };
    assert!(discriminant < SHORTHAND_OFFSET as u64);
    let shorthand = start + SHORTHAND_OFFSET;

    // Get the number of bits that leb128 could fit
    // in the same space as the fully encoded type.
    let leb128_bits = len * 7;

    // Check that the shorthand is a not longer than the
    // full encoding itself, i.e. it's an obvious win.
    if leb128_bits >= 64 || (shorthand as u64) < (1 << leb128_bits) {
        cache(encoder).insert(value.clone(), shorthand);
    }

    Ok(())
}
/// Encode the given value or a previously cached shorthand.
Encode the given value or a previously cached shorthand.
Encode the given value or a previously cached shorthand .
pub fn encode_with_shorthand<E, T, M>(encoder: &mut E,
                                      value: &T,
                                      cache: M)
                                      -> Result<(), E::Error>
    where E: TyEncoder,
          M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>,
          T: EncodableWithShorthand,
{
    let existing_shorthand = cache(encoder).get(value).cloned();
    if let Some(shorthand) = existing_shorthand {
        return encoder.emit_usize(shorthand);
    }
    let variant = value.variant();
    let start = encoder.position();
    variant.encode(encoder)?;
    let len = encoder.position() - start;
    // The shorthand encoding uses the same usize as the
    // discriminant, with an offset so they can't conflict.
    let discriminant = unsafe { intrinsics::discriminant_value(variant) };
    assert!(discriminant < SHORTHAND_OFFSET as u64);
    let shorthand = start + SHORTHAND_OFFSET;
    // Get the number of bits that leb128 could fit
    // in the same space as the fully encoded type.
    let leb128_bits = len * 7;
    // Check that the shorthand is a not longer than the
    // full encoding itself, i.e. it's an obvious win.
    if leb128_bits >= 64 || (shorthand as u64) < (1 << leb128_bits) {
        cache(encoder).insert(value.clone(), shorthand);
    }
    Ok(())
}
pub fn encode_with_shorthand < E , T , M > ( encoder : & mut E , value : & T , cache : M ) -> Result < ( ) , E :: Error > where E : TyEncoder , M : for < ' b > Fn ( & ' b mut E ) -> & ' b mut FxHashMap < T , usize > , T : EncodableWithShorthand , { let existing_shorthand = cache ( encoder ) . get ( value ) . cloned ( ) ; if let Some ( shorthand ) = existing_shorthand { return encoder . emit_usize ( shorthand ) ; } let variant = value . variant ( ) ; let start = encoder . position ( ) ; variant . encode ( encoder ) ? ; let len = encoder . position ( ) - start ; // The shorthand encoding uses the same usize as the // discriminant, with an offset so they can't conflict. let discriminant = unsafe { intrinsics :: discriminant_value ( variant ) } ; assert ! ( discriminant SHORTHAND_OFFSET as u64 ) ; let shorthand = start + SHORTHAND_OFFSET ; // Get the number of bits that leb128 could fit // in the same space as the fully encoded type. let leb128_bits = len * 7 ; // Check that the shorthand is a not longer than the // full encoding itself, i.e. it's an obvious win. if leb128_bits >= 64 || ( shorthand as u64 ) < ( 1 << leb128_bits ) { cache ( encoder ) . insert ( value . clone ( ) , shorthand ) ; } Ok ( ( ) ) }
Encode the given value or a previously cached shorthand.
Encode the given value or a previously cached shorthand .
encoder
&mut E
value
&T
cache
M
encoder
&mut E
value
&T
cache
M