1
Fork 0

Add type for slices in ValTrees

This commit is contained in:
b-naber 2022-03-29 11:18:40 +02:00
parent 13c9fc38c9
commit 51aa3f86a0
2 changed files with 30 additions and 2 deletions

View file

@ -1,5 +1,7 @@
use super::ScalarInt;
use rustc_macros::HashStable;
use crate::ty::codec::TyDecoder;
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
use rustc_serialize::{Decodable, Encodable, Encoder};
#[derive(Copy, Clone, Debug, Hash, TyEncodable, TyDecodable, Eq, PartialEq, Ord, PartialOrd)]
#[derive(HashStable)]
@ -20,6 +22,7 @@ pub enum ValTree<'tcx> {
/// See the `ScalarInt` documentation for how `ScalarInt` guarantees that equal values
/// of these types have the same representation.
Leaf(ScalarInt),
SliceOrStr(ValSlice<'tcx>),
/// The fields of any kind of aggregate. Structs, tuples and arrays are represented by
/// listing their fields' values in order.
/// Enums are represented by storing their discriminant as a field, followed by all
@ -32,3 +35,28 @@ impl<'tcx> ValTree<'tcx> {
Self::Branch(&[])
}
}
#[derive(Copy, Clone, Debug, HashStable, Hash, Eq, PartialEq, PartialOrd, Ord)]
pub struct ValSlice<'tcx> {
pub bytes: &'tcx [u8],
}
impl<'tcx, S: Encoder> Encodable<S> for ValSlice<'tcx> {
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_usize(self.bytes.len())?;
s.emit_raw_bytes(self.bytes)?;
Ok(())
}
}
impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ValSlice<'tcx> {
fn decode(d: &mut D) -> Self {
let tcx = d.tcx();
let len = d.read_usize();
let bytes_raw = d.read_raw_bytes(len);
let bytes = tcx.arena.alloc_slice(&bytes_raw[..]);
ValSlice { bytes }
}
}

View file

@ -62,7 +62,7 @@ pub use self::closure::{
CAPTURE_STRUCT_LOCAL,
};
pub use self::consts::{
Const, ConstInt, ConstKind, ConstS, InferConst, ScalarInt, Unevaluated, ValTree,
Const, ConstInt, ConstKind, ConstS, InferConst, ScalarInt, Unevaluated, ValSlice, ValTree,
};
pub use self::context::{
tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,