formatting

This commit is contained in:
Quinn Sinclair 2024-02-13 01:36:54 +01:00
parent a6fc64bb7b
commit 58d9c4707e
2 changed files with 15 additions and 5 deletions

View File

@ -96,7 +96,11 @@ impl LateLintPass<'_> for CheckTokioAsyncReadExtTrait {
## Creating traits programmatically ## Creating traits programmatically
Traits are often generic over a type e.g. `Borrow<T>` is generic over `T`, and rust allows us to implement a trait for a specific type. For example, we can implement `Borrow<str>` for a hypothetical type `Foo`. Let's suppose that we would like to find whether our type actually implements `Borrow<[u8]>`. To do so, we need to supply a type that represents `[u8]`, but `[u8]` is also a generic, it's a slice over `u8`. We can create this type using Ty::new_slice method. The following code demonstrates how to do this: Traits are often generic over a type e.g. `Borrow<T>` is generic over `T`, and rust allows us to implement a trait for
a specific type. For example, we can implement `Borrow<str>` for a hypothetical type `Foo`. Let's suppose that we
would like to find whether our type actually implements `Borrow<[u8]>`. To do so, we need to supply a type that
represents `[u8]`, but `[u8]` is also a generic, it's a slice over `u8`. We can create this type using Ty::new_slice
method. The following code demonstrates how to do this:
```rust ```rust
@ -111,7 +115,9 @@ if implements_trait(cx, ty, borrow_id, &[Ty::new_slice(cx.tcx, cx.tcx.types.u8).
} }
``` ```
Here, we use `Ty::new_slice` to create a type that represents `[T]` and supply `u8` as a type parameter, and then we go on normally with `implements_trait` function. The [Ty] struct allows us to create types programmatically, and it's useful when we need to create types that we can't obtain through the usual means. Here, we use `Ty::new_slice` to create a type that represents `[T]` and supply `u8` as a type parameter, and then we go
on normally with `implements_trait` function. The [Ty] struct allows us to create types programmatically, and it's
useful when we need to create types that we can't obtain through the usual means.

View File

@ -125,11 +125,15 @@ the [`TypeckResults::node_type()`][node_type] method inside of bodies.
## Creating Types programmatically ## Creating Types programmatically
A common usecase for creating types programmatically is when we want to check if a type implements a trait. We have a section on this in the [Trait Checking](trait_checking.md) chapter, but given the importance of this topic, we will also cover it here. A common usecase for creating types programmatically is when we want to check if a type implements a trait. We have
a section on this in the [Trait Checking](trait_checking.md) chapter, but given the importance of this topic, we will
also cover it a bit here.
When we refer to "type" in this context, we refer to `ty::Ty`. To create a `ty::Ty` programmatically, we rely on `Ty::new_*` methods. These methods create a `TyKind` and then wrap it in a `Ty` struct. When we refer to "type" in this context, we refer to `ty::Ty`. To create a `ty::Ty` programmatically, we rely on
`Ty::new_*` methods. These methods create a `TyKind` and then wrap it in a `Ty` struct.
This means we have access to all the primitive types, such as `Ty::new_char`, `Ty::new_bool`, `Ty::new_int`, etc. We can also create more complex types, such as slices, tuples, and references. This means we have access to all the primitive types, such as `Ty::new_char`, `Ty::new_bool`, `Ty::new_int`, etc.
We can also create more complex types, such as slices, tuples, and references.
Here's an example of how to create a `Ty` for a slice of `u8`, i.e. `[u8]` Here's an example of how to create a `Ty` for a slice of `u8`, i.e. `[u8]`