소스 검색

Use the compiler plugin.

Michael Howell 11 년 전
부모
커밋
d036ad454f
3개의 변경된 파일15개의 추가작업 그리고 54개의 파일을 삭제
  1. 5 1
      Cargo.toml
  2. 3 0
      src/host.rs
  3. 7 53
      src/lib.rs

+ 5 - 1
Cargo.toml

@@ -14,12 +14,16 @@ license = "MIT/Apache-2.0"
 [features]
 query_encoding = ["encoding"]
 serde_serialization = ["serde"]
-heap_size = ["heapsize"]
+heap_size = ["heapsize", "heapsize_plugin"]
 
 [dependencies.heapsize]
 git = "https://github.com/servo/heapsize.git"
 optional = true
 
+[dependencies.heapsize_plugin]
+git = "https://github.com/servo/heapsize.git"
+optional = true
+
 [dependencies.encoding]
 version = "0.2"
 optional = true

+ 3 - 0
src/host.rs

@@ -15,6 +15,7 @@ use percent_encoding::{from_hex, percent_decode};
 
 /// The host name of an URL.
 #[derive(PartialEq, Eq, Clone, Debug, Hash, PartialOrd, Ord)]
+#[cfg_attr(feature="heap_size", derive(HeapSizeOf))]
 pub enum Host {
     /// A (DNS) domain name or an IPv4 address.
     ///
@@ -34,6 +35,8 @@ pub enum Host {
 pub struct Ipv6Address {
     pub pieces: [u16; 8]
 }
+#[cfg(feature="heap_size")]
+known_heap_size!(0, Ipv6Address);
 
 
 impl Host {

+ 7 - 53
src/lib.rs

@@ -118,6 +118,9 @@ assert!(css_url.serialize() == "http://servo.github.io/rust-url/main.css".to_str
 
 */
 
+#![cfg_attr(feature="heap_size", feature(plugin, custom_derive))]
+#![cfg_attr(feature="heap_size", plugin(heapsize_plugin))]
+
 extern crate rustc_serialize;
 
 #[macro_use]
@@ -127,7 +130,7 @@ extern crate matches;
 extern crate serde;
 
 #[cfg(feature="heap_size")]
-extern crate heapsize;
+#[macro_use] extern crate heapsize;
 
 use std::fmt::{self, Formatter};
 use std::str;
@@ -136,9 +139,6 @@ use std::path::{Path, PathBuf};
 #[cfg(feature="serde_serialization")]
 use std::str::FromStr;
 
-#[cfg(feature="heap_size")]
-use heapsize::HeapSizeOf;
-
 pub use host::{Host, Ipv6Address};
 pub use parser::{ErrorHandler, ParseResult, ParseError};
 
@@ -162,6 +162,7 @@ mod tests;
 
 /// The parsed representation of an absolute URL.
 #[derive(PartialEq, Eq, Clone, Debug, Hash, PartialOrd, Ord)]
+#[cfg_attr(feature="heap_size", derive(HeapSizeOf))]
 pub struct Url {
     /// The scheme (a.k.a. protocol) of the URL, in ASCII lower case.
     pub scheme: String,
@@ -193,6 +194,7 @@ pub struct Url {
 
 /// The components of the URL whose representation depends on where the scheme is *relative*.
 #[derive(PartialEq, Eq, Clone, Debug, Hash, PartialOrd, Ord)]
+#[cfg_attr(feature="heap_size", derive(HeapSizeOf))]
 pub enum SchemeData {
     /// Components for URLs in a *relative* scheme such as HTTP.
     Relative(RelativeSchemeData),
@@ -207,6 +209,7 @@ pub enum SchemeData {
 
 /// Components for URLs in a *relative* scheme such as HTTP.
 #[derive(PartialEq, Eq, Clone, Debug, Hash, PartialOrd, Ord)]
+#[cfg_attr(feature="heap_size", derive(HeapSizeOf))]
 pub struct RelativeSchemeData {
     /// The username of the URL, as a possibly empty, percent-encoded string.
     ///
@@ -1055,52 +1058,3 @@ fn file_url_path_to_pathbuf_windows(path: &[String]) -> Result<PathBuf, ()> {
     Ok(path)
 }
 
-#[cfg(feature="heap_size")]
-impl HeapSizeOf for Url {
-    fn heap_size_of_children(&self) -> usize {
-        // Using a struct pattern without `..` rather than `foo.bar` field access
-        // makes sure this will be updated if a field is added.
-        let &url::Url { ref scheme, ref scheme_data, ref query, ref fragment } = self;
-        scheme.heap_size_of_children() +
-        scheme_data.heap_size_of_children() +
-        query.heap_size_of_children() +
-        fragment.heap_size_of_children()
-    }
-}
-
-#[cfg(feature="heap_size")]
-impl HeapSizeOf for SchemeData {
-    fn heap_size_of_children(&self) -> usize {
-        match self {
-            &url::SchemeData::Relative(ref data) => data.heap_size_of_children(),
-            &url::SchemeData::NonRelative(ref str) => str.heap_size_of_children()
-        }
-    }
-}
-
-#[cfg(feature="heap_size")]
-impl HeapSizeOf for RelativeSchemeData {
-    fn heap_size_of_children(&self) -> usize {
-        // Using a struct pattern without `..` rather than `foo.bar` field access
-        // makes sure this will be updated if a field is added.
-        let &url::RelativeSchemeData { ref username, ref password, ref host,
-                                       ref port, ref default_port, ref path } = self;
-        username.heap_size_of_children() +
-        password.heap_size_of_children() +
-        host.heap_size_of_children() +
-        port.heap_size_of_children() +
-        default_port.heap_size_of_children() +
-        path.heap_size_of_children()
-    }
-}
-
-#[cfg(feature="heap_size")]
-impl HeapSizeOf for Host {
-    fn heap_size_of_children(&self) -> usize {
-        match self {
-            &url::Host::Domain(ref str) => str.heap_size_of_children(),
-            &url::Host::Ipv6(_) => 0
-        }
-    }
-}
-