Skip to content

Commit 51aa3f8

Browse files
committed
Add type for slices in ValTrees
1 parent 13c9fc3 commit 51aa3f8

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

compiler/rustc_middle/src/ty/consts/valtree.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use super::ScalarInt;
2-
use rustc_macros::HashStable;
2+
use crate::ty::codec::TyDecoder;
3+
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
4+
use rustc_serialize::{Decodable, Encodable, Encoder};
35

46
#[derive(Copy, Clone, Debug, Hash, TyEncodable, TyDecodable, Eq, PartialEq, Ord, PartialOrd)]
57
#[derive(HashStable)]
@@ -20,6 +22,7 @@ pub enum ValTree<'tcx> {
2022
/// See the `ScalarInt` documentation for how `ScalarInt` guarantees that equal values
2123
/// of these types have the same representation.
2224
Leaf(ScalarInt),
25+
SliceOrStr(ValSlice<'tcx>),
2326
/// The fields of any kind of aggregate. Structs, tuples and arrays are represented by
2427
/// listing their fields' values in order.
2528
/// Enums are represented by storing their discriminant as a field, followed by all
@@ -32,3 +35,28 @@ impl<'tcx> ValTree<'tcx> {
3235
Self::Branch(&[])
3336
}
3437
}
38+
39+
#[derive(Copy, Clone, Debug, HashStable, Hash, Eq, PartialEq, PartialOrd, Ord)]
40+
pub struct ValSlice<'tcx> {
41+
pub bytes: &'tcx [u8],
42+
}
43+
44+
impl<'tcx, S: Encoder> Encodable<S> for ValSlice<'tcx> {
45+
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
46+
s.emit_usize(self.bytes.len())?;
47+
s.emit_raw_bytes(self.bytes)?;
48+
49+
Ok(())
50+
}
51+
}
52+
53+
impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ValSlice<'tcx> {
54+
fn decode(d: &mut D) -> Self {
55+
let tcx = d.tcx();
56+
let len = d.read_usize();
57+
let bytes_raw = d.read_raw_bytes(len);
58+
let bytes = tcx.arena.alloc_slice(&bytes_raw[..]);
59+
60+
ValSlice { bytes }
61+
}
62+
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub use self::closure::{
6262
CAPTURE_STRUCT_LOCAL,
6363
};
6464
pub use self::consts::{
65-
Const, ConstInt, ConstKind, ConstS, InferConst, ScalarInt, Unevaluated, ValTree,
65+
Const, ConstInt, ConstKind, ConstS, InferConst, ScalarInt, Unevaluated, ValSlice, ValTree,
6666
};
6767
pub use self::context::{
6868
tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,

0 commit comments

Comments
 (0)