diff options
| author | b-naber <bn263@gmx.de> | 2022-03-29 11:18:40 +0200 |
|---|---|---|
| committer | b-naber <bn263@gmx.de> | 2022-03-29 11:18:40 +0200 |
| commit | 51aa3f86a040599dad36a75c22fa0321f7de0741 (patch) | |
| tree | 5cd381d1c474ca16216b43984827b3a08a582395 | |
| parent | 13c9fc38c94969ce4b91615bc803d923be8e0f51 (diff) | |
Add type for slices in ValTrees
| -rw-r--r-- | compiler/rustc_middle/src/ty/consts/valtree.rs | 30 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 2 |
2 files changed, 30 insertions, 2 deletions
diff --git a/compiler/rustc_middle/src/ty/consts/valtree.rs b/compiler/rustc_middle/src/ty/consts/valtree.rs index fae22c28628..64a3b80988e 100644 --- a/compiler/rustc_middle/src/ty/consts/valtree.rs +++ b/compiler/rustc_middle/src/ty/consts/valtree.rs @@ -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 } + } +} diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 31db66dc242..9c81a90529c 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -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, |
