Skip to content

Commit b02ed86

Browse files
committed
Polish up docs
1 parent 745e750 commit b02ed86

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

README.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
# InterTrait
1+
# Intertrait
22

33
![Build Status](https://github.com/CodeChain-io/intertrait/workflows/ci/badge.svg)
44
[![Latest Version](https://img.shields.io/crates/v/intertrait.svg)](https://crates.io/crates/intertrait)
55
[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/intertrait)
66

77
This library provides direct casting among trait objects implemented by a type.
88

9-
In Rust, an object of a sub-trait of [`std::any::Any`] can be downcast to a concrete type at runtime if the type is known. But no direct casting between two trait objects (i.e. without involving the concrete type of the backing value) are possible (even no coercion from a trait object to that of its super-trait yet).
9+
In Rust, a trait object for a sub-trait of [`std::any::Any`] can be downcast to a concrete type at runtime
10+
if the type is known. But no direct casting between two trait objects (i.e. without involving the concrete type
11+
of the backing value) are possible (even no coercion from a trait object for a trait to that for its super-trait yet).
1012

11-
With this crate, any trait object with [`CastFrom`] as its super-trait can be cast directly to another trait object implemented by the underlying type if the target traits are registered beforehand with the macros provided by this crate.
13+
With this crate, any trait object for a sub-trait of [`CastFrom`] can be cast directly to a trait object
14+
for another trait implemented by the underlying type if the target traits are registered beforehand
15+
with the macros provided by this crate.
1216

1317
# Usage
1418
```rust
@@ -39,7 +43,7 @@ fn main() {
3943
}
4044
```
4145

42-
Target traits must be explicitly designated beforehand. There are three ways to do it:
46+
Target traits must be explicitly designated beforehand. There are three ways of doing it:
4347

4448
## `#[cast_to]` to `impl` item
4549
The trait implemented is designated as a target trait.
@@ -92,18 +96,28 @@ fn main() {}
9296
```
9397

9498
# How it works
95-
First of all, [`CastFrom`] trait makes it possible to retrieve an object of [`std::any::Any`] from an object of a sub-trait of [`CastFrom`].
99+
First of all, [`CastFrom`] trait makes it possible to retrieve an object of [`std::any::Any`]
100+
from an object for a sub-trait of [`CastFrom`].
96101

97-
> [`CastFrom`] will become obsolete and be replaced with [`std::any::Any`] once the [unsized coercion](https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions) from a trait object to an object of its super-trait is implemented in the stable Rust.
102+
> [`CastFrom`] will become obsolete and be replaced with [`std::any::Any`]
103+
> once [unsized coercion](https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions)
104+
> from a trait object to another trait object for its super-trait is implemented in the stable Rust.
98105
99-
And the macros provided by `intertrait` generates trampoline functions for downcasting a trait object of [`std::any::Any`] back to its concrete type and then creating the target trait object from it.
106+
And the macros provided by `intertrait` generates trampoline functions for downcasting a trait object
107+
for [`std::any::Any`] back to its concrete type and then creating a trait object for the target trait from it.
100108

101-
Those trampoline functions are aggregated into a global registry using [`linkme`](https://github.com/dtolnay/linkme/) crate, which involves no (generally discouraged) life-before-main trick. The registry is keyed with a pair of [`TypeId`]s, which are for the concrete type backing an object of a sub-trait of [`CastFrom`] and the target trait (the actual implementation is a bit different here, but conceptually so).
109+
Those trampoline functions are aggregated into a global registry
110+
using [`linkme`](https://github.com/dtolnay/linkme/) crate, which involves no (generally discouraged)
111+
life-before-main trick. The registry is keyed with a pair of [`TypeId`]s, which are those of the concrete type
112+
backing a trait object for a sub-trait of [`CastFrom`] and the target trait (the actual implementation
113+
is a bit different here, but conceptually so).
102114

103-
In the course, it doesn't rely on any unstable Rust implementation details such as the layout of trait objects that may be changed in the future.
115+
In the course, it doesn't rely on any unstable Rust implementation details such as the layout of trait objects
116+
that may be changed in the future.
104117

105118
# Credits
106-
`intertrait` has taken much of its core ideas from the great [`traitcast`](https://github.com/bch29/traitcast) crate. This crate enhances mainly in the ergonomics.
119+
`intertrait` has taken much of its core ideas from the great [`traitcast`](https://github.com/bch29/traitcast) crate.
120+
This crate enhances mainly in the ergonomics.
107121

108122
# License
109123
Licensed under either of

macros/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ mod item_type;
3030
/// fn greet(&self);
3131
/// }
3232
///
33-
/// // Greet can be cast into from any CastFrom sub-trait object backed by Data.
33+
/// // Greet can be cast into from any sub-trait of CastFrom implemented by Data.
3434
/// #[cast_to]
3535
/// impl Greet for Data {
3636
/// fn greet(&self) {
@@ -42,7 +42,7 @@ mod item_type;
4242
/// ## On a type definition
4343
/// Use when a target trait is derived or implemented in an external crate.
4444
/// ```
45-
/// // Debug can be cast into from any CastFrom sub-trait object backed by Data.
45+
/// // Debug can be cast into from any sub-trait of CastFrom implemented by Data
4646
/// #[cast_to(std::fmt::Debug)]
4747
/// #[derive(std::fmt::Debug)]
4848
/// struct Data;

src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,24 @@ static CASTER_MAP: Lazy<HashMap<(TypeId, TypeId), BoxedCaster, BuildFastHasher>>
9696
.collect()
9797
});
9898

99-
/// A `Caster` knows how to cast a reference to or `Box` of a trait object of type `Any`
100-
/// to a trait object of type `T`. Each `Caster` instance is specific to a concrete type.
99+
/// A `Caster` knows how to cast a reference to or `Box` of a trait object for `Any`
100+
/// to a trait object of trait `T`. Each `Caster` instance is specific to a concrete type.
101101
/// That is, it knows how to cast to single specific trait implemented by single specific type.
102102
///
103103
/// An implementation of a trait for a concrete type doesn't need to manually provide
104104
/// a `Caster`. Instead attach `#[cast_to]` to the `impl` block.
105105
#[doc(hidden)]
106106
pub struct Caster<T: ?Sized + 'static> {
107-
/// Casts a reference to a trait object of type `Any` from a concrete type `S`
108-
/// to a reference to a trait object of type `T`.
107+
/// Casts a reference to a trait object for `Any` from a concrete type `S`
108+
/// to a reference to a trait object for trait `T`.
109109
pub cast_ref: fn(from: &dyn Any) -> Option<&T>,
110110

111-
/// Casts a mutable reference to a trait object of type `Any` from a concrete type `S`
112-
/// to a mutable reference to a trait object of type `T`.
111+
/// Casts a mutable reference to a trait object for `Any` from a concrete type `S`
112+
/// to a mutable reference to a trait object for trait `T`.
113113
pub cast_mut: fn(from: &mut dyn Any) -> Option<&mut T>,
114114

115-
/// Casts a `Box` holding a trait object of type `Any` from a concrete type `S`
116-
/// to another `Box` holding a trait object of type `T`.
115+
/// Casts a `Box` holding a trait object for `Any` from a concrete type `S`
116+
/// to another `Box` holding a trait object for trait `T`.
117117
pub cast_box: fn(from: Box<dyn Any>) -> CastBoxResult<T>,
118118
}
119119

@@ -138,7 +138,7 @@ fn caster<T: ?Sized + 'static>(type_id: TypeId) -> Option<&'static Caster<T>> {
138138
///
139139
/// **Note**: [`CastFrom`] will become obsolete and be replaced with [`std::any::Any`]
140140
/// once the [unsized coercion](https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions)
141-
/// from a trait object to an object of its super-trait is implemented in the stable Rust.
141+
/// from a trait object to another for its super-trait is implemented in the stable Rust.
142142
///
143143
/// [`Any`]: https://doc.rust-lang.org/std/any/trait.Any.html
144144
pub trait CastFrom {

0 commit comments

Comments
 (0)