Просмотр исходного кода

Implement Eq and PartialEq on Url structs.

Simon Sapin 12 лет назад
Родитель
Сommit
744f496c27
1 измененных файлов с 20 добавлено и 6 удалено
  1. 20 6
      src/url.rs

+ 20 - 6
src/url.rs

@@ -41,13 +41,13 @@ pub struct Url {
     encoding_override: Option<EncodingRef>,
 }
 
-#[deriving(Clone)]
+#[deriving(PartialEq, Eq, Clone)]
 pub enum SchemeData {
     RelativeSchemeData(SchemeRelativeUrl),
     OtherSchemeData(String),  // data: URLs, mailto: URLs, etc.
 }
 
-#[deriving(Clone)]
+#[deriving(PartialEq, Eq, Clone)]
 pub struct SchemeRelativeUrl {
     pub username: String,
     pub password: Option<String>,
@@ -56,7 +56,7 @@ pub struct SchemeRelativeUrl {
     pub path: Vec<String>,
 }
 
-#[deriving(Clone)]
+#[deriving(PartialEq, Eq, Clone)]
 pub enum Host {
     Domain(String),
     Ipv6(Ipv6Address)
@@ -72,9 +72,11 @@ impl Clone for Ipv6Address {
     }
 }
 
-impl ::std::fmt::Show for Ipv6Address {
-    fn fmt(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
-        self.pieces.as_slice().fmt(formatter)
+impl Eq for Ipv6Address {}
+
+impl PartialEq for Ipv6Address {
+    fn eq(&self, other: &Ipv6Address) -> bool {
+        self.pieces == other.pieces
     }
 }
 
@@ -90,6 +92,18 @@ impl Clone for Url {
     }
 }
 
+impl Eq for Url {}
+
+impl PartialEq for Url {
+    fn eq(&self, other: &Url) -> bool {
+        self.scheme == other.scheme &&
+        self.scheme_data == other.scheme_data &&
+        self.query == other.query &&
+        self.fragment == other.fragment &&
+        self.encoding_override.map(|e| e.name()) == other.encoding_override.map(|e| e.name())
+    }
+}
+
 macro_rules! is_match(
     ($value:expr, $($pattern:pat)|+) => (
         match $value { $($pattern)|+ => true, _ => false }