@@ -78,30 +78,6 @@ impl<'tcx> ValTree<'tcx> {
78
78
Self :: Branch ( _) => None ,
79
79
}
80
80
}
81
-
82
- /// Get the values inside the ValTree as a slice of bytes. This only works for
83
- /// constants with types &str, &[u8], or [u8; _].
84
- pub fn try_to_raw_bytes ( self , tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Option < & ' tcx [ u8 ] > {
85
- match ty. kind ( ) {
86
- ty:: Ref ( _, inner_ty, _) => match inner_ty. kind ( ) {
87
- // `&str` can be interpreted as raw bytes
88
- ty:: Str => { }
89
- // `&[u8]` can be interpreted as raw bytes
90
- ty:: Slice ( slice_ty) if * slice_ty == tcx. types . u8 => { }
91
- // other `&_` can't be interpreted as raw bytes
92
- _ => return None ,
93
- } ,
94
- // `[u8; N]` can be interpreted as raw bytes
95
- ty:: Array ( array_ty, _) if * array_ty == tcx. types . u8 => { }
96
- // Otherwise, type cannot be interpreted as raw bytes
97
- _ => return None ,
98
- }
99
-
100
- Some (
101
- tcx. arena
102
- . alloc_from_iter ( self . unwrap_branch ( ) . into_iter ( ) . map ( |v| v. unwrap_leaf ( ) . to_u8 ( ) ) ) ,
103
- )
104
- }
105
81
}
106
82
107
83
/// A type-level constant value.
@@ -143,6 +119,29 @@ impl<'tcx> Value<'tcx> {
143
119
}
144
120
self . valtree . try_to_scalar_int ( ) . map ( |s| s. to_target_usize ( tcx) )
145
121
}
122
+
123
+ /// Get the values inside the ValTree as a slice of bytes. This only works for
124
+ /// constants with types &str, &[u8], or [u8; _].
125
+ pub fn try_to_raw_bytes ( self , tcx : TyCtxt < ' tcx > ) -> Option < & ' tcx [ u8 ] > {
126
+ match self . ty . kind ( ) {
127
+ ty:: Ref ( _, inner_ty, _) => match inner_ty. kind ( ) {
128
+ // `&str` can be interpreted as raw bytes
129
+ ty:: Str => { }
130
+ // `&[u8]` can be interpreted as raw bytes
131
+ ty:: Slice ( slice_ty) if * slice_ty == tcx. types . u8 => { }
132
+ // other `&_` can't be interpreted as raw bytes
133
+ _ => return None ,
134
+ } ,
135
+ // `[u8; N]` can be interpreted as raw bytes
136
+ ty:: Array ( array_ty, _) if * array_ty == tcx. types . u8 => { }
137
+ // Otherwise, type cannot be interpreted as raw bytes
138
+ _ => return None ,
139
+ }
140
+
141
+ Some ( tcx. arena . alloc_from_iter (
142
+ self . valtree . unwrap_branch ( ) . into_iter ( ) . map ( |v| v. unwrap_leaf ( ) . to_u8 ( ) ) ,
143
+ ) )
144
+ }
146
145
}
147
146
148
147
impl < ' tcx > rustc_type_ir:: inherent:: ValueConst < TyCtxt < ' tcx > > for Value < ' tcx > {
0 commit comments