about summary refs log tree commit diff
path: root/src/libsyntax/util
diff options
context:
space:
mode:
authorJeffrey Seyfried <jeffrey.seyfried@gmail.com>2017-01-18 03:27:09 +0000
committerJeffrey Seyfried <jeffrey.seyfried@gmail.com>2017-01-22 21:37:38 +0000
commit2dc60b1180b2974b8966c33100e9541845e1d2e8 (patch)
tree8d5ca1abc7811361e7528325010330143daba668 /src/libsyntax/util
parentec29011346ac91f2acdc0455ad6dc19a6f9614ca (diff)
Refactor `TokenStream`.
Diffstat (limited to 'src/libsyntax/util')
-rw-r--r--src/libsyntax/util/rc_slice.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/libsyntax/util/rc_slice.rs b/src/libsyntax/util/rc_slice.rs
new file mode 100644
index 00000000000..cb3becf83f6
--- /dev/null
+++ b/src/libsyntax/util/rc_slice.rs
@@ -0,0 +1,50 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use std::hash::{self, Hash};
+use std::fmt;
+use std::ops::Deref;
+use std::rc::Rc;
+
+#[derive(Clone)]
+pub struct RcSlice<T> {
+    data: Rc<Box<[T]>>,
+    offset: u32,
+    len: u32,
+}
+
+impl<T> RcSlice<T> {
+    pub fn new(vec: Vec<T>) -> Self {
+        RcSlice {
+            offset: 0,
+            len: vec.len() as u32,
+            data: Rc::new(vec.into_boxed_slice()),
+        }
+    }
+}
+
+impl<T> Deref for RcSlice<T> {
+    type Target = [T];
+    fn deref(&self) -> &[T] {
+        &self.data[self.offset as usize .. (self.offset + self.len) as usize]
+    }
+}
+
+impl<T: Hash> Hash for RcSlice<T> {
+    fn hash<H: hash::Hasher>(&self, state: &mut H) {
+        self.deref().hash(state);
+    }
+}
+
+impl<T: fmt::Debug> fmt::Debug for RcSlice<T> {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        fmt::Debug::fmt(self.deref(), f)
+    }
+}