]> git.lizzy.rs Git - rust.git/blob - src/librustc_metadata/isolated_encoder.rs
Rollup merge of #56641 - GuillaumeGomez:span-trait-method-invalid-nb-parameters,...
[rust.git] / src / librustc_metadata / isolated_encoder.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use encoder::EncodeContext;
12 use schema::{Lazy, LazySeq};
13 use rustc::ty::TyCtxt;
14 use rustc_serialize::Encodable;
15
16 /// The IsolatedEncoder provides facilities to write to crate metadata while
17 /// making sure that anything going through it is also feed into an ICH hasher.
18 pub struct IsolatedEncoder<'a, 'b: 'a, 'tcx: 'b> {
19     pub tcx: TyCtxt<'b, 'tcx, 'tcx>,
20     ecx: &'a mut EncodeContext<'b, 'tcx>,
21 }
22
23 impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
24
25     pub fn new(ecx: &'a mut EncodeContext<'b, 'tcx>) -> Self {
26         let tcx = ecx.tcx;
27         IsolatedEncoder {
28             tcx,
29             ecx,
30         }
31     }
32
33     pub fn lazy<T>(&mut self, value: &T) -> Lazy<T>
34         where T: Encodable
35     {
36         self.ecx.lazy(value)
37     }
38
39     pub fn lazy_seq<I, T>(&mut self, iter: I) -> LazySeq<T>
40         where I: IntoIterator<Item = T>,
41               T: Encodable
42     {
43         self.ecx.lazy_seq(iter)
44     }
45
46     pub fn lazy_seq_ref<'x, I, T>(&mut self, iter: I) -> LazySeq<T>
47         where I: IntoIterator<Item = &'x T>,
48               T: 'x + Encodable
49     {
50         self.ecx.lazy_seq_ref(iter)
51     }
52
53     pub fn lazy_seq_from_slice<T>(&mut self, slice: &[T]) -> LazySeq<T>
54         where T: Encodable
55     {
56         self.ecx.lazy_seq_ref(slice.iter())
57     }
58 }