]> git.lizzy.rs Git - rust.git/commitdiff
Convert libstd to use the Drop trait
authorBen Striegel <ben.striegel@gmail.com>
Wed, 14 Nov 2012 02:38:18 +0000 (21:38 -0500)
committerBrian Anderson <banderson@mozilla.com>
Thu, 15 Nov 2012 00:15:34 +0000 (16:15 -0800)
src/libstd/arc.rs
src/libstd/arena.rs
src/libstd/c_vec.rs
src/libstd/future.rs
src/libstd/net_tcp.rs
src/libstd/sort.rs
src/libstd/sync.rs
src/libstd/thread_pool.rs

index 0033a4eaccd07acf59dfd78cb498d2e671f0d15b..503c15b46c2cd135bdf7903f9c4534d83575be3f 100644 (file)
@@ -217,7 +217,10 @@ fn check_poison(is_mutex: bool, failed: bool) {
 #[doc(hidden)]
 struct PoisonOnFail {
     failed: &mut bool,
-    drop {
+}
+
+impl PoisonOnFail : Drop {
+    fn finalize() {
         /* assert !*self.failed; -- might be false in case of cond.wait() */
         if task::failing() { *self.failed = true; }
     }
index 9f40794b28a7915cfef45d2f4abfb9c2859a909f..cf8bbc628f0c11f28c1192cf92071f120ba09121 100644 (file)
@@ -55,7 +55,10 @@ pub struct Arena {
     priv mut head: Chunk,
     priv mut pod_head: Chunk,
     priv mut chunks: @List<Chunk>,
-    drop {
+}
+
+impl Arena : Drop {
+    fn finalize() {
         unsafe {
             destroy_chunk(&self.head);
             for list::each(self.chunks) |chunk| {
index 06d56ed1ae529c5d2322c5d23971ba5506b49b63..2e94f07d2d88df54750c807175391f542de2eb63 100644 (file)
@@ -39,12 +39,15 @@ pub enum CVec<T> {
 
 struct DtorRes {
   dtor: Option<fn@()>,
-  drop {
+}
+
+impl DtorRes : Drop {
+    fn finalize() {
     match self.dtor {
       option::None => (),
       option::Some(f) => f()
     }
-  }
+    }
 }
 
 fn DtorRes(dtor: Option<fn@()>) -> DtorRes {
index 503e915cf8783f78b191864bf2ce047005c648bb..17b487f16dee26fbcdf4f7ffe86bc3f24b9749e3 100644 (file)
 #[doc = "The future type"]
 pub struct Future<A> {
     /*priv*/ mut state: FutureState<A>,
+}
 
-    // FIXME(#2829) -- futures should not be copyable, because they close
-    // over fn~'s that have pipes and so forth within!
-    drop {}
+// FIXME(#2829) -- futures should not be copyable, because they close
+// over fn~'s that have pipes and so forth within!
+impl<A> Future<A> : Drop {
+    fn finalize() {}
 }
 
 priv enum FutureState<A> {
index d58ab844f117a5cdabd86cedef0f6fbcbd577da2..182ec2a233c97219eeebe18d9a45802cc801a615 100644 (file)
  */
 struct TcpSocket {
   socket_data: @TcpSocketData,
-  drop {
+}
+
+impl TcpSocket : Drop {
+    fn finalize() {
     unsafe {
         tear_down_socket_data(self.socket_data)
     }
-  }
+    }
 }
 
 pub fn TcpSocket(socket_data: @TcpSocketData) -> TcpSocket {
index 40c81951b5e9c4e4801c69678bc9f21ad9c31c4c..4f06cc40c229c253ed29691f4c4ff82b9430bcb2 100644 (file)
@@ -1133,7 +1133,10 @@ struct LVal {
         val: uint,
         key: fn(@uint),
 
-        drop {
+    }
+
+    impl LVal : Drop {
+        fn finalize() {
             let x = unsafe { task::local_data::local_data_get(self.key) };
             match x {
                 Some(@y) => {
index 43d1c9664a51f652c36b40db5a3ad9b697f4e064..55f458c6f46c49608eacce3bcd693c32517e9a53 100644 (file)
@@ -150,7 +150,12 @@ fn access<U>(blk: fn() -> U) -> U {
 #[doc(hidden)]
 struct SemRelease {
     sem: &Sem<()>,
-    drop { self.sem.release(); }
+}
+
+impl SemRelease : Drop {
+    fn finalize() {
+        self.sem.release();
+    }
 }
 
 fn SemRelease(sem: &r/Sem<()>) -> SemRelease/&r {
@@ -162,7 +167,12 @@ fn SemRelease(sem: &r/Sem<()>) -> SemRelease/&r {
 #[doc(hidden)]
 struct SemAndSignalRelease {
     sem: &Sem<~[mut Waitqueue]>,
-    drop { self.sem.release(); }
+}
+
+impl SemAndSignalRelease : Drop {
+    fn finalize() {
+        self.sem.release();
+    }
 }
 
 fn SemAndSignalRelease(sem: &r/Sem<~[mut Waitqueue]>)
@@ -173,7 +183,9 @@ fn SemAndSignalRelease(sem: &r/Sem<~[mut Waitqueue]>)
 }
 
 /// A mechanism for atomic-unlock-and-deschedule blocking and signalling.
-pub struct Condvar { priv sem: &Sem<~[mut Waitqueue]>, drop { } }
+pub struct Condvar { priv sem: &Sem<~[mut Waitqueue]> }
+
+impl Condvar : Drop { fn finalize() {} }
 
 impl &Condvar {
     /**
@@ -242,10 +254,15 @@ fn wait_on(condvar_id: uint) {
         // bounded in when it gets released, this shouldn't hang forever.
         struct SemAndSignalReacquire {
             sem: &Sem<~[mut Waitqueue]>,
-            drop unsafe {
-                // Needs to succeed, instead of itself dying.
-                do task::unkillable {
-                    self.sem.acquire();
+        }
+
+        impl SemAndSignalReacquire : Drop {
+            fn finalize() {
+                unsafe {
+                    // Needs to succeed, instead of itself dying.
+                    do task::unkillable {
+                        self.sem.acquire();
+                    }
                 }
             }
         }
@@ -581,20 +598,25 @@ fn downgrade(token: RWlockWriteMode/&a) -> RWlockReadMode/&a {
 #[doc(hidden)]
 struct RWlockReleaseRead {
     lock: &RWlock,
-    drop unsafe {
-        do task::unkillable {
-            let mut last_reader = false;
-            do self.lock.state.with |state| {
-                assert state.read_mode;
-                assert state.read_count > 0;
-                state.read_count -= 1;
-                if state.read_count == 0 {
-                    last_reader = true;
-                    state.read_mode = false;
+}
+
+impl RWlockReleaseRead : Drop {
+    fn finalize() {
+        unsafe {
+            do task::unkillable {
+                let mut last_reader = false;
+                do self.lock.state.with |state| {
+                    assert state.read_mode;
+                    assert state.read_count > 0;
+                    state.read_count -= 1;
+                    if state.read_count == 0 {
+                        last_reader = true;
+                        state.read_mode = false;
+                    }
+                }
+                if last_reader {
+                    (&self.lock.access_lock).release();
                 }
-            }
-            if last_reader {
-                (&self.lock.access_lock).release();
             }
         }
     }
@@ -610,27 +632,33 @@ fn RWlockReleaseRead(lock: &r/RWlock) -> RWlockReleaseRead/&r {
 #[doc(hidden)]
 struct RWlockReleaseDowngrade {
     lock: &RWlock,
-    drop unsafe {
-        do task::unkillable {
-            let mut writer_or_last_reader = false;
-            do self.lock.state.with |state| {
-                if state.read_mode {
-                    assert state.read_count > 0;
-                    state.read_count -= 1;
-                    if state.read_count == 0 {
-                        // Case 1: Writer downgraded & was the last reader
-                        writer_or_last_reader = true;
-                        state.read_mode = false;
+}
+
+impl RWlockReleaseDowngrade : Drop {
+    fn finalize() {
+        unsafe {
+            do task::unkillable {
+                let mut writer_or_last_reader = false;
+                do self.lock.state.with |state| {
+                    if state.read_mode {
+                        assert state.read_count > 0;
+                        state.read_count -= 1;
+                        if state.read_count == 0 {
+                            // Case 1: Writer downgraded & was the last reader
+                            writer_or_last_reader = true;
+                            state.read_mode = false;
+                        } else {
+                            // Case 2: Writer downgraded & was not the last
+                            // reader
+                        }
                     } else {
-                        // Case 2: Writer downgraded & was not the last reader
+                        // Case 3: Writer did not downgrade
+                        writer_or_last_reader = true;
                     }
-                } else {
-                    // Case 3: Writer did not downgrade
-                    writer_or_last_reader = true;
                 }
-            }
-            if writer_or_last_reader {
-                (&self.lock.access_lock).release();
+                if writer_or_last_reader {
+                    (&self.lock.access_lock).release();
+                }
             }
         }
     }
@@ -643,9 +671,11 @@ fn RWlockReleaseDowngrade(lock: &r/RWlock) -> RWlockReleaseDowngrade/&r {
 }
 
 /// The "write permission" token used for rwlock.write_downgrade().
-pub struct RWlockWriteMode { /* priv */ lock: &RWlock, drop { } }
+pub struct RWlockWriteMode { /* priv */ lock: &RWlock }
+impl RWlockWriteMode : Drop { fn finalize() {} }
 /// The "read permission" token used for rwlock.write_downgrade().
-pub struct RWlockReadMode  { priv lock: &RWlock, drop { } }
+pub struct RWlockReadMode  { priv lock: &RWlock }
+impl RWlockReadMode : Drop { fn finalize() {} }
 
 impl &RWlockWriteMode {
     /// Access the pre-downgrade rwlock in write mode.
@@ -954,7 +984,12 @@ fn test_mutex_killed_broadcast() {
         }
         struct SendOnFailure {
             c: pipes::Chan<()>,
-            drop { self.c.send(()); }
+        }
+
+        impl SendOnFailure : Drop {
+            fn finalize() {
+                self.c.send(());
+            }
         }
 
         fn SendOnFailure(c: pipes::Chan<()>) -> SendOnFailure {
index 4bded2093d0fccb520f875706f2c60ca6c0c6ff6..f5d1a6edbf13e48a43d581851c1533cd06fc656c 100644 (file)
@@ -13,7 +13,10 @@ pub struct ThreadPool<T> {
     channels: ~[Chan<Msg<T>>],
     mut next_index: uint,
 
-    drop {
+}
+
+impl<T> ThreadPool<T> {
+    fn finalize() {
         for self.channels.each |channel| {
             channel.send(Quit);
         }