Skip to content

Commit eb69fb3

Browse files
committed
Delete section on dyn compatibility of default implementations
1 parent ec9665a commit eb69fb3

File tree

2 files changed

+0
-174
lines changed

2 files changed

+0
-174
lines changed

README.md

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -189,77 +189,6 @@ trait Test {
189189

190190
<br>
191191

192-
## Dyn traits
193-
194-
Traits with async methods can be used as trait objects as long as they meet the
195-
usual requirements for dyn -- no methods with type parameters, no self by value,
196-
no associated types, etc.
197-
198-
```rust
199-
#[async_trait]
200-
pub trait ObjectSafe {
201-
async fn f(&self);
202-
async fn g(&mut self);
203-
}
204-
205-
impl ObjectSafe for MyType {...}
206-
207-
let value: MyType = ...;
208-
let object = &value as &dyn ObjectSafe; // make trait object
209-
```
210-
211-
The one wrinkle is in traits that provide default implementations of async
212-
methods. In order for the default implementation to produce a future that is
213-
Send, the async\_trait macro must emit a bound of `Self: Sync` on trait methods
214-
that take `&self` and a bound `Self: Send` on trait methods that take `&mut
215-
self`. An example of the former is visible in the expanded code in the
216-
explanation section above.
217-
218-
If you make a trait with async methods that have default implementations,
219-
everything will work except that the trait cannot be used as a trait object.
220-
Creating a value of type `&dyn Trait` will produce an error that looks like
221-
this:
222-
223-
```console
224-
error: the trait `Test` cannot be made into an object
225-
--> src/main.rs:8:5
226-
|
227-
8 | async fn cannot_dyn(&self) {}
228-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
229-
```
230-
231-
For traits that need to be object safe and need to have default implementations
232-
for some async methods, there are two resolutions. Either you can add Send
233-
and/or Sync as supertraits (Send if there are `&mut self` methods with default
234-
implementations, Sync if there are `&self` methods with default implementations)
235-
to constrain all implementors of the trait such that the default implementations
236-
are applicable to them:
237-
238-
```rust
239-
#[async_trait]
240-
pub trait ObjectSafe: Sync { // added supertrait
241-
async fn can_dyn(&self) {}
242-
}
243-
244-
let object = &value as &dyn ObjectSafe;
245-
```
246-
247-
or you can strike the problematic methods from your trait object by bounding
248-
them with `Self: Sized`:
249-
250-
```rust
251-
#[async_trait]
252-
pub trait ObjectSafe {
253-
async fn cannot_dyn(&self) where Self: Sized {}
254-
255-
// presumably other methods
256-
}
257-
258-
let object = &value as &dyn ObjectSafe;
259-
```
260-
261-
<br>
262-
263192
#### License
264193

265194
<sup>

src/lib.rs

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -213,109 +213,6 @@
213213
//! async fn test(elided: Elided<'_>) {}
214214
//! }
215215
//! ```
216-
//!
217-
//! <br><br>
218-
//!
219-
//! # Dyn traits
220-
//!
221-
//! Traits with async methods can be used as trait objects as long as they meet
222-
//! the usual requirements for dyn -- no methods with type parameters, no self
223-
//! by value, no associated types, etc.
224-
//!
225-
//! ```
226-
//! # use async_trait::async_trait;
227-
//! #
228-
//! #[async_trait]
229-
//! pub trait ObjectSafe {
230-
//! async fn f(&self);
231-
//! async fn g(&mut self);
232-
//! }
233-
//!
234-
//! # const IGNORE: &str = stringify! {
235-
//! impl ObjectSafe for MyType {...}
236-
//!
237-
//! let value: MyType = ...;
238-
//! # };
239-
//! #
240-
//! # struct MyType;
241-
//! #
242-
//! # #[async_trait]
243-
//! # impl ObjectSafe for MyType {
244-
//! # async fn f(&self) {}
245-
//! # async fn g(&mut self) {}
246-
//! # }
247-
//! #
248-
//! # let value: MyType = MyType;
249-
//! let object = &value as &dyn ObjectSafe; // make trait object
250-
//! ```
251-
//!
252-
//! The one wrinkle is in traits that provide default implementations of async
253-
//! methods. In order for the default implementation to produce a future that is
254-
//! Send, the async_trait macro must emit a bound of `Self: Sync` on trait
255-
//! methods that take `&self` and a bound `Self: Send` on trait methods that
256-
//! take `&mut self`. An example of the former is visible in the expanded code
257-
//! in the explanation section above.
258-
//!
259-
//! If you make a trait with async methods that have default implementations,
260-
//! everything will work except that the trait cannot be used as a trait object.
261-
//! Creating a value of type `&dyn Trait` will produce an error that looks like
262-
//! this:
263-
//!
264-
//! ```text
265-
//! error: the trait `Test` cannot be made into an object
266-
//! --> src/main.rs:8:5
267-
//! |
268-
//! 8 | async fn cannot_dyn(&self) {}
269-
//! | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
270-
//! ```
271-
//!
272-
//! For traits that need to be object safe and need to have default
273-
//! implementations for some async methods, there are two resolutions. Either
274-
//! you can add Send and/or Sync as supertraits (Send if there are `&mut self`
275-
//! methods with default implementations, Sync if there are `&self` methods with
276-
//! default implementations) to constrain all implementors of the trait such that
277-
//! the default implementations are applicable to them:
278-
//!
279-
//! ```
280-
//! # use async_trait::async_trait;
281-
//! #
282-
//! #[async_trait]
283-
//! pub trait ObjectSafe: Sync { // added supertrait
284-
//! async fn can_dyn(&self) {}
285-
//! }
286-
//! #
287-
//! # struct MyType;
288-
//! #
289-
//! # #[async_trait]
290-
//! # impl ObjectSafe for MyType {}
291-
//! #
292-
//! # let value = MyType;
293-
//!
294-
//! let object = &value as &dyn ObjectSafe;
295-
//! ```
296-
//!
297-
//! or you can strike the problematic methods from your trait object by
298-
//! bounding them with `Self: Sized`:
299-
//!
300-
//! ```
301-
//! # use async_trait::async_trait;
302-
//! #
303-
//! #[async_trait]
304-
//! pub trait ObjectSafe {
305-
//! async fn cannot_dyn(&self) where Self: Sized {}
306-
//!
307-
//! // presumably other methods
308-
//! }
309-
//! #
310-
//! # struct MyType;
311-
//! #
312-
//! # #[async_trait]
313-
//! # impl ObjectSafe for MyType {}
314-
//! #
315-
//! # let value = MyType;
316-
//!
317-
//! let object = &value as &dyn ObjectSafe;
318-
//! ```
319216
320217
#![doc(html_root_url = "https://docs.rs/async-trait/0.1.85")]
321218
#![allow(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy