]> git.lizzy.rs Git - rust.git/commitdiff
rustdoc: Render associated type bindings
authorTom Jakubowski <tom@crystae.net>
Thu, 8 Jan 2015 00:10:40 +0000 (16:10 -0800)
committerTom Jakubowski <tom@crystae.net>
Thu, 8 Jan 2015 00:22:04 +0000 (16:22 -0800)
e.g. `Foo<Output=A>`

This does not work cross-crate unfortunately.

Part of #20646

src/librustdoc/clean/mod.rs
src/librustdoc/html/format.rs

index bf2664bba6ad3b973442ccb637bf4b905c5506da..ea6bfc64c2227d806a62bff8f312c28d13fb7122 100644 (file)
@@ -530,7 +530,8 @@ fn external_path_params(cx: &DocContext, trait_did: Option<ast::DefId>,
                 _ => {
                     return PathParameters::AngleBracketed {
                         lifetimes: lifetimes,
-                        types: types.clean(cx)
+                        types: types.clean(cx),
+                        bindings: vec![]
                     }
                 }
             };
@@ -547,6 +548,7 @@ fn external_path_params(cx: &DocContext, trait_did: Option<ast::DefId>,
             PathParameters::AngleBracketed {
                 lifetimes: lifetimes,
                 types: types.clean(cx),
+                bindings: vec![] // FIXME(#20646)
             }
         }
     }
@@ -1766,6 +1768,7 @@ pub enum PathParameters {
     AngleBracketed {
         lifetimes: Vec<Lifetime>,
         types: Vec<Type>,
+        bindings: Vec<TypeBinding>
     },
     Parenthesized {
         inputs: Vec<Type>,
@@ -1779,7 +1782,8 @@ fn clean(&self, cx: &DocContext) -> PathParameters {
             ast::AngleBracketedParameters(ref data) => {
                 PathParameters::AngleBracketed {
                     lifetimes: data.lifetimes.clean(cx),
-                    types: data.types.clean(cx)
+                    types: data.types.clean(cx),
+                    bindings: data.bindings.clean(cx)
                 }
             }
 
@@ -2442,8 +2446,25 @@ fn lang_struct(cx: &DocContext, did: Option<ast::DefId>,
                 params: PathParameters::AngleBracketed {
                     lifetimes: vec![],
                     types: vec![t.clean(cx)],
+                    bindings: vec![]
                 }
             }],
         },
     }
 }
+
+/// An equality constraint on an associated type, e.g. `A=Bar` in `Foo<A=Bar>`
+#[derive(Clone, PartialEq, RustcDecodable, RustcEncodable)]
+pub struct TypeBinding {
+    pub name: String,
+    pub ty: Type
+}
+
+impl Clean<TypeBinding> for ast::TypeBinding {
+    fn clean(&self, cx: &DocContext) -> TypeBinding {
+        TypeBinding {
+            name: self.ident.clean(cx),
+            ty: self.ty.clean(cx)
+        }
+    }
+}
index b24e7a7a4cf81a06d6fb25572293a787c1c56b5f..0757441ed878db9960c5203741d11fdcb2531258 100644 (file)
@@ -250,8 +250,10 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 impl fmt::String for clean::PathParameters {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match *self {
-            clean::PathParameters::AngleBracketed { ref lifetimes, ref types } => {
-                if lifetimes.len() > 0 || types.len() > 0 {
+            clean::PathParameters::AngleBracketed {
+                ref lifetimes, ref types, ref bindings
+            } => {
+                if lifetimes.len() > 0 || types.len() > 0 || bindings.len() > 0 {
                     try!(f.write_str("&lt;"));
                     let mut comma = false;
                     for lifetime in lifetimes.iter() {
@@ -268,6 +270,13 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                         comma = true;
                         try!(write!(f, "{}", *ty));
                     }
+                    for binding in bindings.iter() {
+                        if comma {
+                            try!(f.write_str(", "));
+                        }
+                        comma = true;
+                        try!(write!(f, "{}", *binding));
+                    }
                     try!(f.write_str("&gt;"));
                 }
             }
@@ -855,6 +864,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                         params: clean::PathParameters::AngleBracketed {
                             lifetimes: Vec::new(),
                             types: Vec::new(),
+                            bindings: Vec::new()
                         }
                     })
                 };
@@ -865,6 +875,19 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
     }
 }
 
+//NOTE(stage0): remove impl after snapshot
+impl fmt::Show for clean::TypeBinding {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        fmt::String::fmt(self, f)
+    }
+}
+
+impl fmt::String for clean::TypeBinding {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "{}={}", self.name, self.ty)
+    }
+}
+
 //NOTE(stage0): remove impl after snapshot
 #[cfg(stage0)]
 impl fmt::Show for MutableSpace {