sass_rs/bindings/ptr.rs
1#![allow(dead_code)]
2// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
3// file at the top-level directory of this distribution and at
4// http://rust-lang.org/COPYRIGHT.
5//
6// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9// option. This file may not be copied, modified, or distributed
10// except according to those terms.
11
12
13use std::marker::{PhantomData, Send, Sized, Sync};
14
15
16/// A wrapper around a raw `*mut T` that indicates that the possessor
17/// of this wrapper owns the referent. This in turn implies that the
18/// `Unique<T>` is `Send`/`Sync` if `T` is `Send`/`Sync`, unlike a raw
19/// `*mut T` (which conveys no particular ownership semantics). It
20/// also implies that the referent of the pointer should not be
21/// modified without a unique path to the `Unique` reference. Useful
22/// for building abstractions like `Vec<T>` or `Box<T>`, which
23/// internally use raw pointers to manage the memory that they own.
24#[derive(Debug)]
25pub struct Unique<T: Sized> {
26 pointer: *mut T,
27 _marker: PhantomData<T>,
28}
29
30/// `Unique` pointers are `Send` if `T` is `Send` because the data they
31/// reference is unaliased. Note that this aliasing invariant is
32/// unenforced by the type system; the abstraction using the
33/// `Unique` must enforce it.
34unsafe impl<T: Send + Sized> Send for Unique<T> {}
35
36/// `Unique` pointers are `Sync` if `T` is `Sync` because the data they
37/// reference is unaliased. Note that this aliasing invariant is
38/// unenforced by the type system; the abstraction using the
39/// `Unique` must enforce it.
40unsafe impl<T: Sync + Sized> Sync for Unique<T> {}
41
42impl<T: Sized> Unique<T> {
43 /// Creates a new `Unique`.
44 pub unsafe fn new(ptr: *mut T) -> Unique<T> {
45 Unique { pointer: ptr, _marker: PhantomData }
46 }
47
48 /// Dereferences the content.
49 pub unsafe fn get(&self) -> &T {
50 &*self.pointer
51 }
52
53 /// Mutably dereferences the content.
54 pub unsafe fn get_mut(&mut self) -> &mut T {
55 //&mut ***self
56 &mut *self.pointer
57 }
58}