Skip to content

Commit 28c7a25

Browse files
bstriebrson
authored andcommitted
Convert libstd to use the Drop trait
1 parent 8336cad commit 28c7a25

File tree

8 files changed

+107
-52
lines changed

8 files changed

+107
-52
lines changed

src/libstd/arc.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,10 @@ fn check_poison(is_mutex: bool, failed: bool) {
217217
#[doc(hidden)]
218218
struct PoisonOnFail {
219219
failed: &mut bool,
220-
drop {
220+
}
221+
222+
impl PoisonOnFail : Drop {
223+
fn finalize() {
221224
/* assert !*self.failed; -- might be false in case of cond.wait() */
222225
if task::failing() { *self.failed = true; }
223226
}

src/libstd/arena.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ pub struct Arena {
5555
priv mut head: Chunk,
5656
priv mut pod_head: Chunk,
5757
priv mut chunks: @List<Chunk>,
58-
drop {
58+
}
59+
60+
impl Arena : Drop {
61+
fn finalize() {
5962
unsafe {
6063
destroy_chunk(&self.head);
6164
for list::each(self.chunks) |chunk| {

src/libstd/c_vec.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ pub enum CVec<T> {
3939

4040
struct DtorRes {
4141
dtor: Option<fn@()>,
42-
drop {
42+
}
43+
44+
impl DtorRes : Drop {
45+
fn finalize() {
4346
match self.dtor {
4447
option::None => (),
4548
option::Some(f) => f()
4649
}
47-
}
50+
}
4851
}
4952

5053
fn DtorRes(dtor: Option<fn@()>) -> DtorRes {

src/libstd/future.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ use cast::copy_lifetime;
2323
#[doc = "The future type"]
2424
pub struct Future<A> {
2525
/*priv*/ mut state: FutureState<A>,
26+
}
2627

27-
// FIXME(#2829) -- futures should not be copyable, because they close
28-
// over fn~'s that have pipes and so forth within!
29-
drop {}
28+
// FIXME(#2829) -- futures should not be copyable, because they close
29+
// over fn~'s that have pipes and so forth within!
30+
impl<A> Future<A> : Drop {
31+
fn finalize() {}
3032
}
3133

3234
priv enum FutureState<A> {

src/libstd/net_tcp.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ extern mod rustrt {
2727
*/
2828
struct TcpSocket {
2929
socket_data: @TcpSocketData,
30-
drop {
30+
}
31+
32+
impl TcpSocket : Drop {
33+
fn finalize() {
3134
unsafe {
3235
tear_down_socket_data(self.socket_data)
3336
}
34-
}
37+
}
3538
}
3639

3740
pub fn TcpSocket(socket_data: @TcpSocketData) -> TcpSocket {

src/libstd/sort.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,10 @@ mod big_tests {
11331133
val: uint,
11341134
key: fn(@uint),
11351135

1136-
drop {
1136+
}
1137+
1138+
impl LVal : Drop {
1139+
fn finalize() {
11371140
let x = unsafe { task::local_data::local_data_get(self.key) };
11381141
match x {
11391142
Some(@y) => {

src/libstd/sync.rs

Lines changed: 76 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ impl &Sem<~[mut Waitqueue]> {
150150
#[doc(hidden)]
151151
struct SemRelease {
152152
sem: &Sem<()>,
153-
drop { self.sem.release(); }
153+
}
154+
155+
impl SemRelease : Drop {
156+
fn finalize() {
157+
self.sem.release();
158+
}
154159
}
155160

156161
fn SemRelease(sem: &r/Sem<()>) -> SemRelease/&r {
@@ -162,7 +167,12 @@ fn SemRelease(sem: &r/Sem<()>) -> SemRelease/&r {
162167
#[doc(hidden)]
163168
struct SemAndSignalRelease {
164169
sem: &Sem<~[mut Waitqueue]>,
165-
drop { self.sem.release(); }
170+
}
171+
172+
impl SemAndSignalRelease : Drop {
173+
fn finalize() {
174+
self.sem.release();
175+
}
166176
}
167177

168178
fn SemAndSignalRelease(sem: &r/Sem<~[mut Waitqueue]>)
@@ -173,7 +183,9 @@ fn SemAndSignalRelease(sem: &r/Sem<~[mut Waitqueue]>)
173183
}
174184

175185
/// A mechanism for atomic-unlock-and-deschedule blocking and signalling.
176-
pub struct Condvar { priv sem: &Sem<~[mut Waitqueue]>, drop { } }
186+
pub struct Condvar { priv sem: &Sem<~[mut Waitqueue]> }
187+
188+
impl Condvar : Drop { fn finalize() {} }
177189

178190
impl &Condvar {
179191
/**
@@ -242,10 +254,15 @@ impl &Condvar {
242254
// bounded in when it gets released, this shouldn't hang forever.
243255
struct SemAndSignalReacquire {
244256
sem: &Sem<~[mut Waitqueue]>,
245-
drop unsafe {
246-
// Needs to succeed, instead of itself dying.
247-
do task::unkillable {
248-
self.sem.acquire();
257+
}
258+
259+
impl SemAndSignalReacquire : Drop {
260+
fn finalize() {
261+
unsafe {
262+
// Needs to succeed, instead of itself dying.
263+
do task::unkillable {
264+
self.sem.acquire();
265+
}
249266
}
250267
}
251268
}
@@ -581,20 +598,25 @@ impl &RWlock {
581598
#[doc(hidden)]
582599
struct RWlockReleaseRead {
583600
lock: &RWlock,
584-
drop unsafe {
585-
do task::unkillable {
586-
let mut last_reader = false;
587-
do self.lock.state.with |state| {
588-
assert state.read_mode;
589-
assert state.read_count > 0;
590-
state.read_count -= 1;
591-
if state.read_count == 0 {
592-
last_reader = true;
593-
state.read_mode = false;
601+
}
602+
603+
impl RWlockReleaseRead : Drop {
604+
fn finalize() {
605+
unsafe {
606+
do task::unkillable {
607+
let mut last_reader = false;
608+
do self.lock.state.with |state| {
609+
assert state.read_mode;
610+
assert state.read_count > 0;
611+
state.read_count -= 1;
612+
if state.read_count == 0 {
613+
last_reader = true;
614+
state.read_mode = false;
615+
}
616+
}
617+
if last_reader {
618+
(&self.lock.access_lock).release();
594619
}
595-
}
596-
if last_reader {
597-
(&self.lock.access_lock).release();
598620
}
599621
}
600622
}
@@ -610,27 +632,33 @@ fn RWlockReleaseRead(lock: &r/RWlock) -> RWlockReleaseRead/&r {
610632
#[doc(hidden)]
611633
struct RWlockReleaseDowngrade {
612634
lock: &RWlock,
613-
drop unsafe {
614-
do task::unkillable {
615-
let mut writer_or_last_reader = false;
616-
do self.lock.state.with |state| {
617-
if state.read_mode {
618-
assert state.read_count > 0;
619-
state.read_count -= 1;
620-
if state.read_count == 0 {
621-
// Case 1: Writer downgraded & was the last reader
622-
writer_or_last_reader = true;
623-
state.read_mode = false;
635+
}
636+
637+
impl RWlockReleaseDowngrade : Drop {
638+
fn finalize() {
639+
unsafe {
640+
do task::unkillable {
641+
let mut writer_or_last_reader = false;
642+
do self.lock.state.with |state| {
643+
if state.read_mode {
644+
assert state.read_count > 0;
645+
state.read_count -= 1;
646+
if state.read_count == 0 {
647+
// Case 1: Writer downgraded & was the last reader
648+
writer_or_last_reader = true;
649+
state.read_mode = false;
650+
} else {
651+
// Case 2: Writer downgraded & was not the last
652+
// reader
653+
}
624654
} else {
625-
// Case 2: Writer downgraded & was not the last reader
655+
// Case 3: Writer did not downgrade
656+
writer_or_last_reader = true;
626657
}
627-
} else {
628-
// Case 3: Writer did not downgrade
629-
writer_or_last_reader = true;
630658
}
631-
}
632-
if writer_or_last_reader {
633-
(&self.lock.access_lock).release();
659+
if writer_or_last_reader {
660+
(&self.lock.access_lock).release();
661+
}
634662
}
635663
}
636664
}
@@ -643,9 +671,11 @@ fn RWlockReleaseDowngrade(lock: &r/RWlock) -> RWlockReleaseDowngrade/&r {
643671
}
644672
645673
/// The "write permission" token used for rwlock.write_downgrade().
646-
pub struct RWlockWriteMode { /* priv */ lock: &RWlock, drop { } }
674+
pub struct RWlockWriteMode { /* priv */ lock: &RWlock }
675+
impl RWlockWriteMode : Drop { fn finalize() {} }
647676
/// The "read permission" token used for rwlock.write_downgrade().
648-
pub struct RWlockReadMode { priv lock: &RWlock, drop { } }
677+
pub struct RWlockReadMode { priv lock: &RWlock }
678+
impl RWlockReadMode : Drop { fn finalize() {} }
649679

650680
impl &RWlockWriteMode {
651681
/// Access the pre-downgrade rwlock in write mode.
@@ -954,7 +984,12 @@ mod tests {
954984
}
955985
struct SendOnFailure {
956986
c: pipes::Chan<()>,
957-
drop { self.c.send(()); }
987+
}
988+
989+
impl SendOnFailure : Drop {
990+
fn finalize() {
991+
self.c.send(());
992+
}
958993
}
959994

960995
fn SendOnFailure(c: pipes::Chan<()>) -> SendOnFailure {

src/libstd/thread_pool.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ pub struct ThreadPool<T> {
1313
channels: ~[Chan<Msg<T>>],
1414
mut next_index: uint,
1515

16-
drop {
16+
}
17+
18+
impl<T> ThreadPool<T> {
19+
fn finalize() {
1720
for self.channels.each |channel| {
1821
channel.send(Quit);
1922
}

0 commit comments

Comments
 (0)