@@ -1867,9 +1867,9 @@ impl<'tcx> Region<'tcx> {
1867
1867
1868
1868
/// Constructors for `Ty`
1869
1869
impl < ' tcx > Ty < ' tcx > {
1870
- // Avoid this in favour of more specific `new_*` methods, where possible.
1871
1870
#[ allow( rustc:: usage_of_ty_tykind) ]
1872
1871
#[ inline]
1872
+ /// Turns a [`TyKind`] into a [`Ty`]. Use more specific `new_*` methods(such as `new_int`), where possible.
1873
1873
pub fn new ( tcx : TyCtxt < ' tcx > , st : TyKind < ' tcx > ) -> Ty < ' tcx > {
1874
1874
tcx. mk_ty_from_kind ( st)
1875
1875
}
@@ -1928,12 +1928,12 @@ impl<'tcx> Ty<'tcx> {
1928
1928
. copied ( )
1929
1929
. unwrap_or_else ( || Ty :: new_infer ( tcx, ty:: FreshFloatTy ( n) ) )
1930
1930
}
1931
-
1931
+ /// Creates a new [`Ty`] representing a parameter, with index `index` and name `name`.
1932
1932
#[ inline]
1933
1933
pub fn new_param ( tcx : TyCtxt < ' tcx > , index : u32 , name : Symbol ) -> Ty < ' tcx > {
1934
1934
tcx. mk_ty_from_kind ( Param ( ParamTy { index, name } ) )
1935
1935
}
1936
-
1936
+ /// Creates an new [`Ty`] representing a bound type variable, with debruijn index `index` and bound type `bound_ty`.
1937
1937
#[ inline]
1938
1938
pub fn new_bound (
1939
1939
tcx : TyCtxt < ' tcx > ,
@@ -2257,22 +2257,22 @@ impl<'tcx> Ty<'tcx> {
2257
2257
_ => false ,
2258
2258
}
2259
2259
}
2260
-
2260
+ /// Checks if this type is of [`TyKind::Never`].
2261
2261
#[ inline]
2262
2262
pub fn is_never ( self ) -> bool {
2263
2263
matches ! ( self . kind( ) , Never )
2264
2264
}
2265
-
2265
+ /// Checks if this type is primitive
2266
2266
#[ inline]
2267
2267
pub fn is_primitive ( self ) -> bool {
2268
2268
self . kind ( ) . is_primitive ( )
2269
2269
}
2270
-
2270
+ /// Checks if this type is an Algebraic Data Type(a struct, union, or enum).
2271
2271
#[ inline]
2272
2272
pub fn is_adt ( self ) -> bool {
2273
2273
matches ! ( self . kind( ) , Adt ( ..) )
2274
2274
}
2275
-
2275
+ /// Checks if a type is a reference.
2276
2276
#[ inline]
2277
2277
pub fn is_ref ( self ) -> bool {
2278
2278
matches ! ( self . kind( ) , Ref ( ..) )
@@ -2295,36 +2295,35 @@ impl<'tcx> Ty<'tcx> {
2295
2295
pub fn is_ty_or_numeric_infer ( self ) -> bool {
2296
2296
matches ! ( self . kind( ) , Infer ( _) )
2297
2297
}
2298
-
2298
+ /// Checks if this type is [`core::marker::PhantomData`].
2299
2299
#[ inline]
2300
2300
pub fn is_phantom_data ( self ) -> bool {
2301
2301
if let Adt ( def, _) = self . kind ( ) { def. is_phantom_data ( ) } else { false }
2302
2302
}
2303
-
2303
+ /// Checks if this type is a boolean.
2304
2304
#[ inline]
2305
2305
pub fn is_bool ( self ) -> bool {
2306
2306
* self . kind ( ) == Bool
2307
2307
}
2308
-
2309
2308
/// Returns `true` if this type is a `str`.
2310
2309
#[ inline]
2311
2310
pub fn is_str ( self ) -> bool {
2312
2311
* self . kind ( ) == Str
2313
2312
}
2314
-
2313
+ /// Checks if a type is a parameter type, with index `index`.
2315
2314
#[ inline]
2316
2315
pub fn is_param ( self , index : u32 ) -> bool {
2317
2316
match self . kind ( ) {
2318
2317
ty:: Param ( ref data) => data. index == index,
2319
2318
_ => false ,
2320
2319
}
2321
2320
}
2322
-
2321
+ /// Checks if this type is a silice type.
2323
2322
#[ inline]
2324
2323
pub fn is_slice ( self ) -> bool {
2325
2324
matches ! ( self . kind( ) , Slice ( _) )
2326
2325
}
2327
-
2326
+ /// Checks if this type is a slice or a reference/pointer to a slice.
2328
2327
#[ inline]
2329
2328
pub fn is_array_slice ( self ) -> bool {
2330
2329
match self . kind ( ) {
@@ -2333,20 +2332,21 @@ impl<'tcx> Ty<'tcx> {
2333
2332
_ => false ,
2334
2333
}
2335
2334
}
2336
-
2335
+ /// Checks if this type is an array.
2337
2336
#[ inline]
2338
2337
pub fn is_array ( self ) -> bool {
2339
2338
matches ! ( self . kind( ) , Array ( ..) )
2340
2339
}
2341
-
2340
+
2342
2341
#[ inline]
2343
2342
pub fn is_simd ( self ) -> bool {
2344
2343
match self . kind ( ) {
2345
2344
Adt ( def, _) => def. repr ( ) . simd ( ) ,
2346
2345
_ => false ,
2347
2346
}
2348
2347
}
2349
-
2348
+ /// If the type is a slice, array, or a string slice, it will returns its element type.
2349
+ /// If this type is not one of those, this function will panic.
2350
2350
pub fn sequence_element_type ( self , tcx : TyCtxt < ' tcx > ) -> Ty < ' tcx > {
2351
2351
match self . kind ( ) {
2352
2352
Array ( ty, _) | Slice ( ty) => * ty,
@@ -2380,7 +2380,7 @@ impl<'tcx> Ty<'tcx> {
2380
2380
_ => bug ! ( "`simd_size_and_type` called on invalid type" ) ,
2381
2381
}
2382
2382
}
2383
-
2383
+ /// Checks if this type is a mutable reference or a mutable pointer.
2384
2384
#[ inline]
2385
2385
pub fn is_mutable_ptr ( self ) -> bool {
2386
2386
matches ! (
@@ -2398,7 +2398,7 @@ impl<'tcx> Ty<'tcx> {
2398
2398
_ => None ,
2399
2399
}
2400
2400
}
2401
-
2401
+ /// Checks if this type is a raw pointer.
2402
2402
#[ inline]
2403
2403
pub fn is_unsafe_ptr ( self ) -> bool {
2404
2404
matches ! ( self . kind( ) , RawPtr ( _) )
@@ -2409,15 +2409,15 @@ impl<'tcx> Ty<'tcx> {
2409
2409
pub fn is_any_ptr ( self ) -> bool {
2410
2410
self . is_ref ( ) || self . is_unsafe_ptr ( ) || self . is_fn_ptr ( )
2411
2411
}
2412
-
2412
+ /// Checks if this type is an [`alloc::boxed::Box`].
2413
2413
#[ inline]
2414
2414
pub fn is_box ( self ) -> bool {
2415
2415
match self . kind ( ) {
2416
2416
Adt ( def, _) => def. is_box ( ) ,
2417
2417
_ => false ,
2418
2418
}
2419
2419
}
2420
-
2420
+ /// Returns the type contained within a `Box<T>`.
2421
2421
/// Panics if called on any type other than `Box<T>`.
2422
2422
pub fn boxed_ty ( self ) -> Ty < ' tcx > {
2423
2423
match self . kind ( ) {
@@ -2449,27 +2449,27 @@ impl<'tcx> Ty<'tcx> {
2449
2449
pub fn is_floating_point ( self ) -> bool {
2450
2450
matches ! ( self . kind( ) , Float ( _) | Infer ( FloatVar ( _) ) )
2451
2451
}
2452
-
2452
+ /// Checks if this type is an unsized trait object(`dyn Trait`).
2453
2453
#[ inline]
2454
2454
pub fn is_trait ( self ) -> bool {
2455
2455
matches ! ( self . kind( ) , Dynamic ( _, _, ty:: Dyn ) )
2456
2456
}
2457
-
2457
+ /// Checks if this type is an sized trait object(`dyn* Trait`).
2458
2458
#[ inline]
2459
2459
pub fn is_dyn_star ( self ) -> bool {
2460
2460
matches ! ( self . kind( ) , Dynamic ( _, _, ty:: DynStar ) )
2461
2461
}
2462
-
2462
+ /// Checks if this type is an enum.
2463
2463
#[ inline]
2464
2464
pub fn is_enum ( self ) -> bool {
2465
2465
matches ! ( self . kind( ) , Adt ( adt_def, _) if adt_def. is_enum( ) )
2466
2466
}
2467
-
2467
+ /// Checks if this type is an union.
2468
2468
#[ inline]
2469
2469
pub fn is_union ( self ) -> bool {
2470
2470
matches ! ( self . kind( ) , Adt ( adt_def, _) if adt_def. is_union( ) )
2471
2471
}
2472
-
2472
+ /// Checks if this type is a closure.
2473
2473
#[ inline]
2474
2474
pub fn is_closure ( self ) -> bool {
2475
2475
matches ! ( self . kind( ) , Closure ( ..) )
@@ -2479,7 +2479,7 @@ impl<'tcx> Ty<'tcx> {
2479
2479
pub fn is_coroutine ( self ) -> bool {
2480
2480
matches ! ( self . kind( ) , Coroutine ( ..) )
2481
2481
}
2482
-
2482
+ /// Checks if this type is an intgeral type(signed, unsigned on infered).
2483
2483
#[ inline]
2484
2484
pub fn is_integral ( self ) -> bool {
2485
2485
matches ! ( self . kind( ) , Infer ( IntVar ( _) ) | Int ( _) | Uint ( _) )
@@ -2494,22 +2494,22 @@ impl<'tcx> Ty<'tcx> {
2494
2494
pub fn is_fresh ( self ) -> bool {
2495
2495
matches ! ( self . kind( ) , Infer ( FreshTy ( _) | FreshIntTy ( _) | FreshFloatTy ( _) ) )
2496
2496
}
2497
-
2497
+ /// Checks if this type is a `char`.
2498
2498
#[ inline]
2499
2499
pub fn is_char ( self ) -> bool {
2500
2500
matches ! ( self . kind( ) , Char )
2501
2501
}
2502
-
2502
+ /// Checks if this type is numeric(integral or floating point).
2503
2503
#[ inline]
2504
2504
pub fn is_numeric ( self ) -> bool {
2505
2505
self . is_integral ( ) || self . is_floating_point ( )
2506
2506
}
2507
-
2507
+ /// Checks if this type is a signed intiger.
2508
2508
#[ inline]
2509
2509
pub fn is_signed ( self ) -> bool {
2510
2510
matches ! ( self . kind( ) , Int ( _) )
2511
2511
}
2512
-
2512
+ /// Checks if this type is `usize` or `isize`.
2513
2513
#[ inline]
2514
2514
pub fn is_ptr_sized_integral ( self ) -> bool {
2515
2515
matches ! ( self . kind( ) , Int ( ty:: IntTy :: Isize ) | Uint ( ty:: UintTy :: Usize ) )
@@ -2582,7 +2582,7 @@ impl<'tcx> Ty<'tcx> {
2582
2582
_ => None ,
2583
2583
}
2584
2584
}
2585
-
2585
+ /// If this type is a function definition or a function pointer, this will retunrn its signature. Otherwise, this function will panic.
2586
2586
pub fn fn_sig ( self , tcx : TyCtxt < ' tcx > ) -> PolyFnSig < ' tcx > {
2587
2587
match self . kind ( ) {
2588
2588
FnDef ( def_id, args) => tcx. fn_sig ( * def_id) . instantiate ( tcx, args) ,
@@ -2597,12 +2597,12 @@ impl<'tcx> Ty<'tcx> {
2597
2597
_ => bug ! ( "Ty::fn_sig() called on non-fn type: {:?}" , self ) ,
2598
2598
}
2599
2599
}
2600
-
2600
+ /// Checks if this type is a function definition or a function pointer.
2601
2601
#[ inline]
2602
2602
pub fn is_fn ( self ) -> bool {
2603
2603
matches ! ( self . kind( ) , FnDef ( ..) | FnPtr ( _) )
2604
2604
}
2605
-
2605
+ /// Checks if this type is a function pointer.
2606
2606
#[ inline]
2607
2607
pub fn is_fn_ptr ( self ) -> bool {
2608
2608
matches ! ( self . kind( ) , FnPtr ( _) )
@@ -2612,7 +2612,7 @@ impl<'tcx> Ty<'tcx> {
2612
2612
pub fn is_impl_trait ( self ) -> bool {
2613
2613
matches ! ( self . kind( ) , Alias ( ty:: Opaque , ..) )
2614
2614
}
2615
-
2615
+ /// Returns the `AdtDef` defining this type, if it is an ADT(struct,union or enum). If this type is not an ADT, returns `None`.
2616
2616
#[ inline]
2617
2617
pub fn ty_adt_def ( self ) -> Option < AdtDef < ' tcx > > {
2618
2618
match self . kind ( ) {
0 commit comments