@@ -37,7 +37,7 @@ use crate::clean::inline;
37
37
use crate :: clean:: types:: Type :: { QPath , ResolvedPath } ;
38
38
use crate :: clean:: Clean ;
39
39
use crate :: core:: DocContext ;
40
- use crate :: formats:: cache:: cache ;
40
+ use crate :: formats:: cache:: Cache ;
41
41
use crate :: formats:: item_type:: ItemType ;
42
42
use crate :: html:: render:: cache:: ExternalLocation ;
43
43
@@ -169,8 +169,8 @@ impl Item {
169
169
self . attrs . collapsed_doc_value ( )
170
170
}
171
171
172
- crate fn links ( & self ) -> Vec < RenderedLink > {
173
- self . attrs . links ( & self . def_id . krate )
172
+ crate fn links ( & self , cache : & Cache ) -> Vec < RenderedLink > {
173
+ self . attrs . links ( & self . def_id . krate , cache )
174
174
}
175
175
176
176
crate fn is_crate ( & self ) -> bool {
@@ -826,7 +826,7 @@ impl Attributes {
826
826
/// Gets links as a vector
827
827
///
828
828
/// Cache must be populated before call
829
- crate fn links ( & self , krate : & CrateNum ) -> Vec < RenderedLink > {
829
+ crate fn links ( & self , krate : & CrateNum , cache : & Cache ) -> Vec < RenderedLink > {
830
830
use crate :: html:: format:: href;
831
831
use crate :: html:: render:: CURRENT_DEPTH ;
832
832
@@ -835,7 +835,7 @@ impl Attributes {
835
835
. filter_map ( |ItemLink { link : s, link_text, did, fragment } | {
836
836
match * did {
837
837
Some ( did) => {
838
- if let Some ( ( mut href, ..) ) = href ( did) {
838
+ if let Some ( ( mut href, ..) ) = href ( did, cache ) {
839
839
if let Some ( ref fragment) = * fragment {
840
840
href. push ( '#' ) ;
841
841
href. push_str ( fragment) ;
@@ -851,7 +851,6 @@ impl Attributes {
851
851
}
852
852
None => {
853
853
if let Some ( ref fragment) = * fragment {
854
- let cache = cache ( ) ;
855
854
let url = match cache. extern_locations . get ( krate) {
856
855
Some ( & ( _, _, ExternalLocation :: Local ) ) => {
857
856
let depth = CURRENT_DEPTH . with ( |l| l. get ( ) ) ;
@@ -1177,6 +1176,13 @@ impl GetDefId for FnRetTy {
1177
1176
DefaultReturn => None ,
1178
1177
}
1179
1178
}
1179
+
1180
+ fn def_id_full ( & self , cache : & Cache ) -> Option < DefId > {
1181
+ match * self {
1182
+ Return ( ref ty) => ty. def_id_full ( cache) ,
1183
+ DefaultReturn => None ,
1184
+ }
1185
+ }
1180
1186
}
1181
1187
1182
1188
#[ derive( Clone , Debug ) ]
@@ -1299,13 +1305,31 @@ crate enum TypeKind {
1299
1305
}
1300
1306
1301
1307
crate trait GetDefId {
1308
+ /// Use this method to get the [`DefId`] of a [`clean`] AST node.
1309
+ /// This will return [`None`] when called on a primitive [`clean::Type`].
1310
+ /// Use [`Self::def_id_full`] if you want to include primitives.
1311
+ ///
1312
+ /// [`clean`]: crate::clean
1313
+ /// [`clean::Type`]: crate::clean::Type
1314
+ // FIXME: get rid of this function and always use `def_id_full`
1302
1315
fn def_id ( & self ) -> Option < DefId > ;
1316
+
1317
+ /// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s.
1318
+ ///
1319
+ /// See [`Self::def_id`] for more.
1320
+ ///
1321
+ /// [clean]: crate::clean
1322
+ fn def_id_full ( & self , cache : & Cache ) -> Option < DefId > ;
1303
1323
}
1304
1324
1305
1325
impl < T : GetDefId > GetDefId for Option < T > {
1306
1326
fn def_id ( & self ) -> Option < DefId > {
1307
1327
self . as_ref ( ) . and_then ( |d| d. def_id ( ) )
1308
1328
}
1329
+
1330
+ fn def_id_full ( & self , cache : & Cache ) -> Option < DefId > {
1331
+ self . as_ref ( ) . and_then ( |d| d. def_id_full ( cache) )
1332
+ }
1309
1333
}
1310
1334
1311
1335
impl Type {
@@ -1393,30 +1417,40 @@ impl Type {
1393
1417
}
1394
1418
}
1395
1419
1396
- impl GetDefId for Type {
1397
- fn def_id ( & self ) -> Option < DefId > {
1398
- match * self {
1399
- ResolvedPath { did, .. } => Some ( did) ,
1400
- Primitive ( p) => cache ( ) . primitive_locations . get ( & p) . cloned ( ) ,
1401
- BorrowedRef { type_ : box Generic ( ..) , .. } => {
1402
- Primitive ( PrimitiveType :: Reference ) . def_id ( )
1403
- }
1404
- BorrowedRef { ref type_, .. } => type_. def_id ( ) ,
1420
+ impl Type {
1421
+ fn inner_def_id ( & self , cache : Option < & Cache > ) -> Option < DefId > {
1422
+ let t: PrimitiveType = match * self {
1423
+ ResolvedPath { did, .. } => return Some ( did) ,
1424
+ Primitive ( p) => return cache. and_then ( |c| c. primitive_locations . get ( & p) . cloned ( ) ) ,
1425
+ BorrowedRef { type_ : box Generic ( ..) , .. } => PrimitiveType :: Reference ,
1426
+ BorrowedRef { ref type_, .. } => return type_. inner_def_id ( cache) ,
1405
1427
Tuple ( ref tys) => {
1406
1428
if tys. is_empty ( ) {
1407
- Primitive ( PrimitiveType :: Unit ) . def_id ( )
1429
+ PrimitiveType :: Unit
1408
1430
} else {
1409
- Primitive ( PrimitiveType :: Tuple ) . def_id ( )
1431
+ PrimitiveType :: Tuple
1410
1432
}
1411
1433
}
1412
- BareFunction ( ..) => Primitive ( PrimitiveType :: Fn ) . def_id ( ) ,
1413
- Never => Primitive ( PrimitiveType :: Never ) . def_id ( ) ,
1414
- Slice ( ..) => Primitive ( PrimitiveType :: Slice ) . def_id ( ) ,
1415
- Array ( ..) => Primitive ( PrimitiveType :: Array ) . def_id ( ) ,
1416
- RawPointer ( ..) => Primitive ( PrimitiveType :: RawPointer ) . def_id ( ) ,
1417
- QPath { ref self_type, .. } => self_type. def_id ( ) ,
1418
- _ => None ,
1419
- }
1434
+ BareFunction ( ..) => PrimitiveType :: Fn ,
1435
+ Never => PrimitiveType :: Never ,
1436
+ Slice ( ..) => PrimitiveType :: Slice ,
1437
+ Array ( ..) => PrimitiveType :: Array ,
1438
+ RawPointer ( ..) => PrimitiveType :: RawPointer ,
1439
+ QPath { ref self_type, .. } => return self_type. inner_def_id ( cache) ,
1440
+ // FIXME: remove this wildcard
1441
+ _ => return None ,
1442
+ } ;
1443
+ cache. and_then ( |c| Primitive ( t) . def_id_full ( c) )
1444
+ }
1445
+ }
1446
+
1447
+ impl GetDefId for Type {
1448
+ fn def_id ( & self ) -> Option < DefId > {
1449
+ self . inner_def_id ( None )
1450
+ }
1451
+
1452
+ fn def_id_full ( & self , cache : & Cache ) -> Option < DefId > {
1453
+ self . inner_def_id ( Some ( cache) )
1420
1454
}
1421
1455
}
1422
1456
@@ -1817,6 +1851,10 @@ impl GetDefId for Typedef {
1817
1851
fn def_id ( & self ) -> Option < DefId > {
1818
1852
self . type_ . def_id ( )
1819
1853
}
1854
+
1855
+ fn def_id_full ( & self , cache : & Cache ) -> Option < DefId > {
1856
+ self . type_ . def_id_full ( cache)
1857
+ }
1820
1858
}
1821
1859
1822
1860
#[ derive( Clone , Debug ) ]
0 commit comments