aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/util')
-rw-r--r--src/components/util/cache.rs10
-rw-r--r--src/components/util/debug.rs4
-rw-r--r--src/components/util/geometry.rs4
-rw-r--r--src/components/util/io.rs4
-rw-r--r--src/components/util/slot.rs141
-rw-r--r--src/components/util/time.rs72
-rw-r--r--src/components/util/url.rs2
-rw-r--r--src/components/util/util.rc6
-rw-r--r--src/components/util/vec.rs17
9 files changed, 66 insertions, 194 deletions
diff --git a/src/components/util/cache.rs b/src/components/util/cache.rs
index 02859014bbd..b8c89a3ac90 100644
--- a/src/components/util/cache.rs
+++ b/src/components/util/cache.rs
@@ -7,7 +7,7 @@ use std::hashmap::HashMap;
pub trait Cache<K: Eq, V: Clone> {
fn insert(&mut self, key: K, value: V);
fn find(&mut self, key: &K) -> Option<V>;
- fn find_or_create(&mut self, key: &K, blk: &fn(&K) -> V) -> V;
+ fn find_or_create(&mut self, key: &K, blk: |&K| -> V) -> V;
fn evict_all(&mut self);
}
@@ -33,7 +33,7 @@ impl<K: Clone + Eq, V: Clone> Cache<K,V> for MonoCache<K,V> {
}
}
- fn find_or_create(&mut self, key: &K, blk: &fn(&K) -> V) -> V {
+ fn find_or_create(&mut self, key: &K, blk: |&K| -> V) -> V {
match self.find(key) {
Some(value) => value,
None => {
@@ -87,7 +87,7 @@ impl<K: Clone + Eq + Hash, V: Clone> Cache<K,V> for HashCache<K,V> {
}
}
- fn find_or_create(&mut self, key: &K, blk: &fn(&K) -> V) -> V {
+ fn find_or_create(&mut self, key: &K, blk: |&K| -> V) -> V {
self.entries.find_or_insert_with(key.clone(), blk).clone()
}
@@ -149,7 +149,7 @@ impl<K: Clone + Eq, V: Clone> Cache<K,V> for LRUCache<K,V> {
}
}
- fn find_or_create(&mut self, key: &K, blk: &fn(&K) -> V) -> V {
+ fn find_or_create(&mut self, key: &K, blk: |&K| -> V) -> V {
match self.entries.iter().position(|&(ref k, _)| *k == *key) {
Some(pos) => self.touch(pos),
None => {
@@ -191,7 +191,7 @@ fn test_lru_cache() {
assert!(cache.find(&4).is_some()); // (2, 4) (no change)
// Test find_or_create.
- do cache.find_or_create(&1) |_| { one }; // (4, 1)
+ cache.find_or_create(&1, |_| { one }); // (4, 1)
assert!(cache.find(&1).is_some()); // (4, 1) (no change)
assert!(cache.find(&2).is_none()); // (4, 1) (no change)
diff --git a/src/components/util/debug.rs b/src/components/util/debug.rs
index 5c9aa700d5b..029db75d129 100644
--- a/src/components/util/debug.rs
+++ b/src/components/util/debug.rs
@@ -2,8 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-use std::rt::io;
-use std::rt::io::Writer;
+use std::io;
+use std::io::Writer;
use std::vec::raw::buf_as_slice;
use std::cast::transmute;
use std::mem::size_of;
diff --git a/src/components/util/geometry.rs b/src/components/util/geometry.rs
index 4b2cb0c2e43..71184e5eec1 100644
--- a/src/components/util/geometry.rs
+++ b/src/components/util/geometry.rs
@@ -124,10 +124,6 @@ impl ToPrimitive for Au {
}
}
-pub fn box<T:Clone + Ord + Add<T,T> + Sub<T,T>>(x: T, y: T, w: T, h: T) -> Rect<T> {
- Rect(Point2D(x, y), Size2D(w, h))
-}
-
impl Au {
/// FIXME(pcwalton): Workaround for lack of cross crate inlining of newtype structs!
#[inline]
diff --git a/src/components/util/io.rs b/src/components/util/io.rs
index 45009723774..b034682cf16 100644
--- a/src/components/util/io.rs
+++ b/src/components/util/io.rs
@@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-use std::rt::io::{io_error, IoError};
+use std::io::{io_error, IoError};
/// Helper for catching an I/O error and wrapping it in a Result object. The
/// return result will be the last I/O error that happened or the result of the
@@ -10,7 +10,7 @@ use std::rt::io::{io_error, IoError};
///
/// FIXME: This is a copy of std::rt::io::result which doesn't exist yet in our
/// version of Rust. We should switch after the next Rust upgrade.
-pub fn result<T>(cb: &fn() -> T) -> Result<T, IoError> {
+pub fn result<T>(cb: || -> T) -> Result<T, IoError> {
let mut err = None;
let ret = io_error::cond.trap(|e| err = Some(e)).inside(cb);
match err {
diff --git a/src/components/util/slot.rs b/src/components/util/slot.rs
deleted file mode 100644
index 34c12f2a0f5..00000000000
--- a/src/components/util/slot.rs
+++ /dev/null
@@ -1,141 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-//! An in-place, dynamically borrowable slot. Useful for "mutable fields". Assuming this works out
-//! well, this type should be upstreamed to the Rust standard library.
-
-use std::cast;
-use std::util;
-
-#[unsafe_no_drop_flag]
-#[no_freeze]
-pub struct Slot<T> {
- // NB: Must be priv, or else someone could borrow it.
- priv value: T,
- priv immutable_borrow_count: u8,
- priv mutably_borrowed: bool,
-}
-
-impl<T:Clone> Clone for Slot<T> {
- #[inline]
- fn clone(&self) -> Slot<T> {
- Slot {
- value: self.value.clone(),
- immutable_borrow_count: 0,
- mutably_borrowed: false,
- }
- }
-}
-
-#[unsafe_destructor]
-impl<T> Drop for Slot<T> {
- fn drop(&mut self) {
- // Noncopyable.
- }
-}
-
-pub struct SlotRef<'self,T> {
- ptr: &'self T,
- priv immutable_borrow_count: *mut u8,
-}
-
-#[unsafe_destructor]
-impl<'self,T> Drop for SlotRef<'self,T> {
- #[inline]
- fn drop(&mut self) {
- unsafe {
- *self.immutable_borrow_count -= 1
- }
- }
-}
-
-pub struct MutSlotRef<'self,T> {
- ptr: &'self mut T,
- priv mutably_borrowed: *mut bool,
-}
-
-#[unsafe_destructor]
-impl<'self,T> Drop for MutSlotRef<'self,T> {
- #[inline]
- fn drop(&mut self) {
- unsafe {
- *self.mutably_borrowed = false
- }
- }
-}
-
-impl<T> Slot<T> {
- #[inline]
- pub fn init(value: T) -> Slot<T> {
- Slot {
- value: value,
- immutable_borrow_count: 0,
- mutably_borrowed: false,
- }
- }
-
- /// Borrows the data immutably. This function is thread-safe, but *bad things will happen if
- /// you try to mutate the data while one of these pointers is held*.
- #[inline]
- pub unsafe fn borrow_unchecked<'a>(&'a self) -> &'a T {
- &self.value
- }
-
- #[inline]
- pub fn borrow<'a>(&'a self) -> SlotRef<'a,T> {
- unsafe {
- if self.immutable_borrow_count == 255 || self.mutably_borrowed {
- self.fail()
- }
- let immutable_borrow_count = cast::transmute_mut(&self.immutable_borrow_count);
- *immutable_borrow_count += 1;
- SlotRef {
- ptr: &self.value,
- immutable_borrow_count: immutable_borrow_count,
- }
- }
- }
-
- #[inline]
- pub fn mutate<'a>(&'a self) -> MutSlotRef<'a,T> {
- unsafe {
- if self.immutable_borrow_count > 0 || self.mutably_borrowed {
- self.fail()
- }
- let mutably_borrowed = cast::transmute_mut(&self.mutably_borrowed);
- *mutably_borrowed = true;
- MutSlotRef {
- ptr: cast::transmute_mut(&self.value),
- mutably_borrowed: mutably_borrowed,
- }
- }
- }
-
- #[inline]
- pub fn set(&self, value: T) {
- *self.mutate().ptr = value
- }
-
- /// Replaces the slot's value with the given value and returns the old value.
- #[inline]
- pub fn replace(&self, value: T) -> T {
- util::replace(self.mutate().ptr, value)
- }
-
- #[inline(never)]
- pub fn fail(&self) -> ! {
- fail!("slot is borrowed")
- }
-}
-
-impl<T:Clone> Slot<T> {
- #[inline]
- pub fn get(&self) -> T {
- self.value.clone()
- }
-}
-
-
-
-
diff --git a/src/components/util/time.rs b/src/components/util/time.rs
index bfc1a1bd43c..59b01d264e7 100644
--- a/src/components/util/time.rs
+++ b/src/components/util/time.rs
@@ -4,25 +4,36 @@
//! Timing functions.
-use extra::sort::tim_sort;
use extra::time::precise_time_ns;
use extra::treemap::TreeMap;
-use std::comm::{Port, SendDeferred, SharedChan};
+use std::comm::{Port, SharedChan};
use std::iter::AdditiveIterator;
-use std::rt::io::timer::Timer;
-use std::task::spawn_with;
+
+
+// TODO: This code should be changed to use the commented code that uses timers
+// directly, once native timers land in Rust.
+extern {
+ pub fn usleep(secs: u64) -> u32;
+}
+
+pub struct Timer;
+impl Timer {
+ pub fn sleep(ms: u64) {
+ //
+ // let mut timer = Timer::new().unwrap();
+ // timer.sleep(period);
+ unsafe { usleep((ms * 1000)); }
+ }
+}
+
// front-end representation of the profiler used to communicate with the profiler
#[deriving(Clone)]
pub struct ProfilerChan(SharedChan<ProfilerMsg>);
impl ProfilerChan {
- pub fn new(chan: Chan<ProfilerMsg>) -> ProfilerChan {
- ProfilerChan(SharedChan::new(chan))
- }
-
- pub fn send_deferred(&self, msg: ProfilerMsg) {
- (**self).send_deferred(msg);
+ pub fn send(&self, msg: ProfilerMsg) {
+ (**self).send(msg);
}
}
@@ -100,32 +111,35 @@ pub struct Profiler {
}
impl Profiler {
- pub fn create(port: Port<ProfilerMsg>, chan: ProfilerChan, period: Option<f64>) {
+ pub fn create(period: Option<f64>) -> ProfilerChan {
+ let (port, chan) = SharedChan::new();
match period {
Some(period) => {
let period = (period * 1000f64) as u64;
- do spawn {
- let mut timer = Timer::new().unwrap();
+ let chan = chan.clone();
+ spawn(proc() {
loop {
- timer.sleep(period);
+ Timer::sleep(period);
if !chan.try_send(PrintMsg) {
break;
}
}
- }
+ });
// Spawn the profiler
- do spawn_with(port) |port| {
+ spawn(proc() {
let mut profiler = Profiler::new(port);
profiler.start();
- }
+ });
}
None => {
// no-op to handle profiler messages when the profiler is inactive
- do spawn_with(port) |port| {
- while port.try_recv().is_some() {}
- }
+ spawn(proc() {
+ while port.recv_opt().is_some() {}
+ });
}
}
+
+ ProfilerChan(chan)
}
pub fn new(port: Port<ProfilerMsg>) -> Profiler {
@@ -138,7 +152,7 @@ impl Profiler {
pub fn start(&mut self) {
loop {
- let msg = self.port.try_recv();
+ let msg = self.port.recv_opt();
match msg {
Some (msg) => self.handle_msg(msg),
None => break
@@ -151,7 +165,7 @@ impl Profiler {
TimeMsg(category, t) => self.buckets.find_mut(&category).unwrap().push(t),
PrintMsg => match self.last_msg {
// only print if more data has arrived since the last printout
- Some(TimeMsg(*)) => self.print_buckets(),
+ Some(TimeMsg(..)) => self.print_buckets(),
_ => ()
},
};
@@ -165,7 +179,13 @@ impl Profiler {
for (category, data) in self.buckets.iter() {
// FIXME(XXX): TreeMap currently lacks mut_iter()
let mut data = data.clone();
- tim_sort(data);
+ data.sort_by(|a, b| {
+ if a < b {
+ Less
+ } else {
+ Greater
+ }
+ });
let data_len = data.len();
if data_len > 0 {
let (mean, median, &min, &max) =
@@ -184,17 +204,17 @@ impl Profiler {
pub fn profile<T>(category: ProfilerCategory,
profiler_chan: ProfilerChan,
- callback: &fn() -> T)
+ callback: || -> T)
-> T {
let start_time = precise_time_ns();
let val = callback();
let end_time = precise_time_ns();
let ms = ((end_time - start_time) as f64 / 1000000f64);
- profiler_chan.send_deferred(TimeMsg(category, ms));
+ profiler_chan.send(TimeMsg(category, ms));
return val;
}
-pub fn time<T>(msg: &str, callback: &fn() -> T) -> T{
+pub fn time<T>(msg: &str, callback: || -> T) -> T{
let start_time = precise_time_ns();
let val = callback();
let end_time = precise_time_ns();
diff --git a/src/components/util/url.rs b/src/components/util/url.rs
index 74571f84f5b..a3677dd6415 100644
--- a/src/components/util/url.rs
+++ b/src/components/util/url.rs
@@ -73,7 +73,7 @@ pub fn make_url(str_url: ~str, current_url: Option<Url>) -> Url {
// Drop whitespace within data: URLs, e.g. newlines within a base64
// src="..." block. Whitespace intended as content should be
// %-encoded or base64'd.
- str_url.iter().filter(|&c| !c.is_whitespace()).collect()
+ str_url.chars().filter(|&c| !c.is_whitespace()).collect()
},
_ => str_url
}
diff --git a/src/components/util/util.rc b/src/components/util/util.rc
index 95e3b4e4662..9830de81d1a 100644
--- a/src/components/util/util.rc
+++ b/src/components/util/util.rc
@@ -2,10 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-#[link(name = "util",
- vers = "0.1",
- uuid = "48421f49-17cf-41c5-a68e-ff669ff2ecd5",
- url = "http://servo.org/")];
+#[crate_id = "github.com/mozilla/servo#util:0.1"];
#[crate_type = "lib"];
#[feature(macro_rules, managed_boxes)];
@@ -16,7 +13,6 @@ extern mod geom;
pub mod cache;
pub mod geometry;
pub mod range;
-pub mod slot;
pub mod time;
pub mod url;
pub mod vec;
diff --git a/src/components/util/vec.rs b/src/components/util/vec.rs
index 7682eeab2ae..2c525c701cc 100644
--- a/src/components/util/vec.rs
+++ b/src/components/util/vec.rs
@@ -4,13 +4,13 @@
use std::cmp::{Ord, Eq};
-pub trait BinarySearchMethods<'self, T: Ord + Eq> {
- fn binary_search(&self, key: &T) -> Option<&'self T>;
+pub trait BinarySearchMethods<'a, T: Ord + Eq> {
+ fn binary_search(&self, key: &T) -> Option<&'a T>;
fn binary_search_index(&self, key: &T) -> Option<uint>;
}
-impl<'self, T: Ord + Eq> BinarySearchMethods<'self, T> for &'self [T] {
- fn binary_search(&self, key: &T) -> Option<&'self T> {
+impl<'a, T: Ord + Eq> BinarySearchMethods<'a, T> for &'a [T] {
+ fn binary_search(&self, key: &T) -> Option<&'a T> {
self.binary_search_index(key).map(|i| &self[i])
}
@@ -39,6 +39,7 @@ impl<'self, T: Ord + Eq> BinarySearchMethods<'self, T> for &'self [T] {
}
}
+#[cfg(test)]
fn test_find_all_elems<T: Eq + Ord>(arr: &[T]) {
let mut i = 0;
while i < arr.len() {
@@ -47,6 +48,7 @@ fn test_find_all_elems<T: Eq + Ord>(arr: &[T]) {
}
}
+#[cfg(test)]
fn test_miss_all_elems<T: Eq + Ord>(arr: &[T], misses: &[T]) {
let mut i = 0;
while i < misses.len() {
@@ -57,6 +59,7 @@ fn test_miss_all_elems<T: Eq + Ord>(arr: &[T], misses: &[T]) {
}
}
+#[cfg(test)]
fn test_match<T: Eq>(b: &T, a: Option<&T>) -> bool {
match a {
None => false,
@@ -70,9 +73,8 @@ pub fn zip_copies<A: Clone, B: Clone>(avec: &[A], bvec: &[B]) -> ~[(A,B)] {
.collect()
}
+#[test]
fn should_find_all_elements() {
- #[test];
-
let arr_odd = [1, 2, 4, 6, 7, 8, 9];
let arr_even = [1, 2, 5, 6, 7, 8, 9, 42];
let arr_double = [1, 1, 2, 2, 6, 8, 22];
@@ -88,9 +90,8 @@ fn should_find_all_elements() {
test_find_all_elems(arr_three);
}
+#[test]
fn should_not_find_missing_elements() {
- #[test];
-
let arr_odd = [1, 2, 4, 6, 7, 8, 9];
let arr_even = [1, 2, 5, 6, 7, 8, 9, 42];
let arr_double = [1, 1, 2, 2, 6, 8, 22];