Skip to content

Implementation of common traits for MaybeOwnedVector #15241

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

Merged
merged 1 commit into from
Jun 29, 2014
Merged
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
57 changes: 57 additions & 0 deletions src/libgraphviz/maybe_owned_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::collections::Collection;
use std::default::Default;
use std::fmt;
use std::iter::FromIterator;
use std::path::BytesContainer;
use std::slice;

// Note 1: It is not clear whether the flexibility of providing both
Expand Down Expand Up @@ -61,6 +64,32 @@ impl<'a,T> MaybeOwnedVector<'a,T> {
}
}

impl<'a, T: PartialEq> PartialEq for MaybeOwnedVector<'a, T> {
fn eq(&self, other: &MaybeOwnedVector<T>) -> bool {
self.as_slice() == other.as_slice()
}
}

impl<'a, T: Eq> Eq for MaybeOwnedVector<'a, T> {}

impl<'a, T: PartialOrd> PartialOrd for MaybeOwnedVector<'a, T> {
fn lt(&self, other: &MaybeOwnedVector<T>) -> bool {
self.as_slice().lt(&other.as_slice())
}
}

impl<'a, T: Ord> Ord for MaybeOwnedVector<'a, T> {
fn cmp(&self, other: &MaybeOwnedVector<T>) -> Ordering {
self.as_slice().cmp(&other.as_slice())
}
}

impl<'a, T: PartialEq, V: Vector<T>> Equiv<V> for MaybeOwnedVector<'a, T> {
fn equiv(&self, other: &V) -> bool {
self.as_slice() == other.as_slice()
}
}

// The `Vector` trait is provided in the prelude and is implemented on
// both `&'a [T]` and `Vec<T>`, so it makes sense to try to support it
// seamlessly. The other vector related traits from the prelude do
Expand Down Expand Up @@ -108,6 +137,34 @@ impl<'a,T:Clone> CloneableVector<T> for MaybeOwnedVector<'a,T> {
}
}

impl<'a, T: Clone> Clone for MaybeOwnedVector<'a, T> {
fn clone(&self) -> MaybeOwnedVector<'a, T> {
match *self {
Growable(ref v) => Growable(v.to_owned()),
Borrowed(v) => Borrowed(v)
}
}
}


impl<'a, T> Default for MaybeOwnedVector<'a, T> {
fn default() -> MaybeOwnedVector<'a, T> {
Growable(Vec::new())
}
}

impl<'a, T> Collection for MaybeOwnedVector<'a, T> {
fn len(&self) -> uint {
self.as_slice().len()
}
}

impl<'a> BytesContainer for MaybeOwnedVector<'a, u8> {
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
self.as_slice()
}
}

impl<'a,T:Clone> MaybeOwnedVector<'a,T> {
/// Convert `self` into a growable `Vec`, not making a copy if possible.
pub fn into_vec(self) -> Vec<T> {
Expand Down