use core::hash::{Hasher, Hash};
use core::iter::FromIterator;
use core::mem;
-use core::ptr;
+use core::ptr::Shared;
/// A doubly-linked list.
#[stable(feature = "rust1", since = "1.0.0")]
type Link<T> = Option<Box<Node<T>>>;
struct Rawlink<T> {
- p: *mut T,
+ p: Option<Shared<T>>,
}
impl<T> Copy for Rawlink<T> {}
impl<T> Rawlink<T> {
/// Like Option::None for Rawlink
fn none() -> Rawlink<T> {
- Rawlink { p: ptr::null_mut() }
+ Rawlink { p: None }
}
/// Like Option::Some for Rawlink
fn some(n: &mut T) -> Rawlink<T> {
- Rawlink { p: n }
+ unsafe { Rawlink { p: Some(Shared::new(n)) } }
}
/// Convert the `Rawlink` into an Option value
/// - Dereference of raw pointer.
/// - Returns reference of arbitrary lifetime.
unsafe fn resolve<'a>(&self) -> Option<&'a T> {
- self.p.as_ref()
+ self.p.map(|p| &**p)
}
/// Convert the `Rawlink` into an Option value
/// - Dereference of raw pointer.
/// - Returns reference of arbitrary lifetime.
unsafe fn resolve_mut<'a>(&mut self) -> Option<&'a mut T> {
- self.p.as_mut()
+ self.p.map(|p| &mut **p)
}
/// Return the `Rawlink` and replace with `Rawlink::none()`
}
}
+// Ensure that `LinkedList` and its read-only iterators are covariant in their type parameters.
+#[allow(dead_code)]
+fn assert_covariance() {
+ fn a<'a>(x: LinkedList<&'static str>) -> LinkedList<&'a str> { x }
+ fn b<'i, 'a>(x: Iter<'i, &'static str>) -> Iter<'i, &'a str> { x }
+ fn c<'a>(x: IntoIter<&'static str>) -> IntoIter<&'a str> { x }
+}
+
#[cfg(test)]
mod tests {
use std::clone::Clone;