@@ -1159,8 +1159,8 @@ impl<T> Drop for Async<T> {
1159
1159
/// traits take `&mut`, there is no guarantee that the implementor of those traits won't move the
1160
1160
/// source out while the method is being run.
1161
1161
///
1162
- /// This trait is an antidote to this predicament. By implementing this trait, it is guaranteed
1163
- /// that using any I/O traits won't desroy the source. This way, [`Async`] can implement the
1162
+ /// This trait is an antidote to this predicament. By implementing this trait, the user pledges
1163
+ /// that using any I/O traits won't destroy the source. This way, [`Async`] can implement the
1164
1164
/// `async` version of these I/O traits, like [`AsyncRead`], [`AsyncWrite`] and [`AsyncSeek`].
1165
1165
///
1166
1166
/// # Safety
@@ -1182,10 +1182,32 @@ impl<T> Drop for Async<T> {
1182
1182
/// [`AsyncWrite`]: https://docs.rs/futures-io/latest/futures_io/trait.AsyncWrite.html
1183
1183
pub unsafe trait IoSafe { }
1184
1184
1185
- // Reference types can't be mutated.
1185
+ /// Reference types can't be mutated.
1186
+ ///
1187
+ /// The worst thing that can happen is that external state is used to change what kind of pointer
1188
+ /// `as_fd()` returns. For instance:
1189
+ ///
1190
+ /// ```no_compile
1191
+ /// struct Bar {
1192
+ /// flag: Cell<bool>,
1193
+ /// a: TcpStream,
1194
+ /// b: TcpStream
1195
+ /// }
1196
+ ///
1197
+ /// impl AsFd for Bar {
1198
+ /// fn as_fd(&self) -> BorrowedFd<'_> {
1199
+ /// if self.flag.replace(!self.flag.get()) {
1200
+ /// &self.a
1201
+ /// } else {
1202
+ /// &self.b
1203
+ /// }
1204
+ /// }
1205
+ /// }
1206
+ /// ```
1207
+ ///
1208
+ /// We solve this problem by only calling `as_fd()` once to get the original source. Implementations
1209
+ /// like this are considered buggy (but not unsound) and are thus not really supported by `async-io`.
1186
1210
unsafe impl < T : ?Sized > IoSafe for & T { }
1187
- unsafe impl < T : ?Sized > IoSafe for std:: rc:: Rc < T > { }
1188
- unsafe impl < T : ?Sized > IoSafe for Arc < T > { }
1189
1211
1190
1212
// Can be implemented on top of libstd types.
1191
1213
unsafe impl IoSafe for std:: fs:: File { }
0 commit comments