Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make LinkedList and its read-only iterators covariant #30663

Merged
merged 1 commit into from
Jan 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#![feature(num_bits_bytes)]
#![feature(oom)]
#![feature(pattern)]
#![feature(ptr_as_ref)]
#![feature(shared)]
#![feature(slice_bytes)]
#![feature(slice_patterns)]
#![feature(staged_api)]
Expand Down
20 changes: 14 additions & 6 deletions src/libcollections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use core::fmt;
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")]
Expand All @@ -40,7 +40,7 @@ pub struct LinkedList<T> {
type Link<T> = Option<Box<Node<T>>>;

struct Rawlink<T> {
p: *mut T,
p: Option<Shared<T>>,
}

impl<T> Copy for Rawlink<T> {}
Expand Down Expand Up @@ -93,12 +93,12 @@ pub struct IntoIter<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
Expand All @@ -108,7 +108,7 @@ impl<T> Rawlink<T> {
/// - 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
Expand All @@ -118,7 +118,7 @@ impl<T> Rawlink<T> {
/// - 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()`
Expand Down Expand Up @@ -984,6 +984,14 @@ impl<A: Hash> Hash for LinkedList<A> {
}
}

// 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;
Expand Down