forked from glaebhoerl/rust-frp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweak.rs
193 lines (165 loc) · 5.08 KB
/
weak.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// borrowed from https://github.com/thestinger/rust/blob/6d8a7fbfe6dd862fbf8166b8d56eda55c1e6fa51/src/libstd/weak.rs
// Copyright 2013 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.
/*! Task-local reference counted boxes with weak pointer support
The `Strong` type is an extension of `std::rc::Rc` with a `downgrade` method returning a `Weak`
pointer type. Ownership of the contained value is shared amongst the `Strong` pointers, and the
value will be destroyed as soon as the last one is gone. A `Weak` pointer can be upgraded to a
`Strong` pointer, but will return `None` if the value has already been freed. It can be used to
avoid creating reference cycles.
For example, a tree with parent pointers can be represented by putting the nodes behind `Strong`
pointers, and then storing the parent pointers as `Weak` pointers.
*/
use std::cast::transmute;
use std::ops::Drop;
use std::cmp::{Eq, Ord};
use std::clone::{Clone, DeepClone};
use std::rt::global_heap::exchange_free;
use std::ptr::read_ptr;
use std::option::{Option, Some, None};
struct RcBox<T> {
value: T,
strong: uint,
weak: uint
}
/// Immutable reference counted pointer type
#[unsafe_no_drop_flag]
#[no_send]
pub struct Strong<T> {
priv ptr: *mut RcBox<T>
}
impl<T> Strong<T> {
/// Construct a new reference-counted box
pub fn new(value: T) -> Strong<T> {
unsafe {
Strong { ptr: transmute(~RcBox { value: value, strong: 1, weak: 0 }) }
}
}
}
impl<T> Strong<T> {
/// Borrow the value contained in the reference-counted box
#[inline(always)]
pub fn borrow<'a>(&'a self) -> &'a T {
unsafe { &(*self.ptr).value }
}
/// Downgrade the reference-counted pointer to a weak reference
pub fn downgrade(&self) -> Weak<T> {
unsafe {
(*self.ptr).weak += 1;
Weak { ptr: self.ptr }
}
}
}
#[unsafe_destructor]
impl<T> Drop for Strong<T> {
fn drop(&mut self) {
unsafe {
if self.ptr != 0 as *mut RcBox<T> {
(*self.ptr).strong -= 1;
if (*self.ptr).strong == 0 {
read_ptr(self.borrow()); // destroy the contained object
if (*self.ptr).weak == 0 {
exchange_free(self.ptr as *mut u8 as *i8)
}
}
}
}
}
}
impl<T> Clone for Strong<T> {
#[inline]
fn clone(&self) -> Strong<T> {
unsafe {
(*self.ptr).strong += 1;
Strong { ptr: self.ptr }
}
}
}
impl<T: DeepClone> DeepClone for Strong<T> {
#[inline]
fn deep_clone(&self) -> Strong<T> {
Strong::new(self.borrow().deep_clone())
}
}
impl<T: Eq> Eq for Strong<T> {
#[inline(always)]
fn eq(&self, other: &Strong<T>) -> bool { *self.borrow() == *other.borrow() }
#[inline(always)]
fn ne(&self, other: &Strong<T>) -> bool { *self.borrow() != *other.borrow() }
}
impl<T: Ord> Ord for Strong<T> {
#[inline(always)]
fn lt(&self, other: &Strong<T>) -> bool { *self.borrow() < *other.borrow() }
#[inline(always)]
fn le(&self, other: &Strong<T>) -> bool { *self.borrow() <= *other.borrow() }
#[inline(always)]
fn gt(&self, other: &Strong<T>) -> bool { *self.borrow() > *other.borrow() }
#[inline(always)]
fn ge(&self, other: &Strong<T>) -> bool { *self.borrow() >= *other.borrow() }
}
/// Weak reference to a reference-counted box
#[unsafe_no_drop_flag]
#[no_send]
pub struct Weak<T> {
priv ptr: *mut RcBox<T>
}
impl<T> Weak<T> {
/// Upgrade a weak reference to a strong reference
pub fn upgrade(&self) -> Option<Strong<T>> {
unsafe {
if (*self.ptr).strong == 0 {
None
} else {
(*self.ptr).strong += 1;
Some(Strong { ptr: self.ptr })
}
}
}
}
#[unsafe_destructor]
impl<T> Drop for Weak<T> {
fn drop(&mut self) {
unsafe {
if self.ptr != 0 as *mut RcBox<T> {
(*self.ptr).weak -= 1;
if (*self.ptr).weak == 0 && (*self.ptr).strong == 0 {
exchange_free(self.ptr as *mut u8 as *i8)
}
}
}
}
}
impl<T> Clone for Weak<T> {
#[inline]
fn clone(&self) -> Weak<T> {
unsafe {
(*self.ptr).weak += 1;
Weak { ptr: self.ptr }
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use prelude::drop;
#[test]
fn test_live() {
let x = Strong::new(5);
let y = x.downgrade();
assert!(y.upgrade().is_some());
}
#[test]
fn test_dead() {
let x = Strong::new(5);
let y = x.downgrade();
drop(x);
assert!(y.upgrade().is_none());
}
}