Skip to content

Commit f1c918f

Browse files
authored
Rollup merge of #93613 - crlf0710:rename_to_async_iter, r=yaahc
Move `{core,std}::stream::Stream` to `{core,std}::async_iter::AsyncIterator` Following amendments in rust-lang/rfcs#3208. cc #79024 cc ``@yoshuawuyts`` ``@joshtriplett``
2 parents cf3cd4c + 18130a2 commit f1c918f

File tree

8 files changed

+83
-84
lines changed

8 files changed

+83
-84
lines changed

library/alloc/src/boxed.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
#![stable(feature = "rust1", since = "1.0.0")]
134134

135135
use core::any::Any;
136+
use core::async_iter::AsyncIterator;
136137
use core::borrow;
137138
use core::cmp::Ordering;
138139
use core::convert::{From, TryFrom};
@@ -149,7 +150,6 @@ use core::ops::{
149150
};
150151
use core::pin::Pin;
151152
use core::ptr::{self, Unique};
152-
use core::stream::Stream;
153153
use core::task::{Context, Poll};
154154

155155
#[cfg(not(no_global_oom_handling))]
@@ -1992,8 +1992,8 @@ where
19921992
}
19931993
}
19941994

1995-
#[unstable(feature = "async_stream", issue = "79024")]
1996-
impl<S: ?Sized + Stream + Unpin> Stream for Box<S> {
1995+
#[unstable(feature = "async_iterator", issue = "79024")]
1996+
impl<S: ?Sized + AsyncIterator + Unpin> AsyncIterator for Box<S> {
19971997
type Item = S::Item;
19981998

19991999
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {

library/alloc/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
#![feature(array_chunks)]
9292
#![feature(array_methods)]
9393
#![feature(array_windows)]
94-
#![feature(async_stream)]
94+
#![feature(async_iterator)]
9595
#![feature(coerce_unsized)]
9696
#![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))]
9797
#![feature(const_box)]

library/core/src/stream/stream.rs library/core/src/async_iter/async_iter.rs

+28-28
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,50 @@ use crate::task::{Context, Poll};
44

55
/// An interface for dealing with asynchronous iterators.
66
///
7-
/// This is the main stream trait. For more about the concept of streams
7+
/// This is the main async iterator trait. For more about the concept of async iterators
88
/// generally, please see the [module-level documentation]. In particular, you
9-
/// may want to know how to [implement `Stream`][impl].
9+
/// may want to know how to [implement `AsyncIterator`][impl].
1010
///
1111
/// [module-level documentation]: index.html
12-
/// [impl]: index.html#implementing-stream
13-
#[unstable(feature = "async_stream", issue = "79024")]
14-
#[must_use = "streams do nothing unless polled"]
15-
pub trait Stream {
16-
/// The type of items yielded by the stream.
12+
/// [impl]: index.html#implementing-async-iterator
13+
#[unstable(feature = "async_iterator", issue = "79024")]
14+
#[must_use = "async iterators do nothing unless polled"]
15+
pub trait AsyncIterator {
16+
/// The type of items yielded by the async iterator.
1717
type Item;
1818

19-
/// Attempt to pull out the next value of this stream, registering the
19+
/// Attempt to pull out the next value of this async iterator, registering the
2020
/// current task for wakeup if the value is not yet available, and returning
21-
/// `None` if the stream is exhausted.
21+
/// `None` if the async iterator is exhausted.
2222
///
2323
/// # Return value
2424
///
2525
/// There are several possible return values, each indicating a distinct
26-
/// stream state:
26+
/// async iterator state:
2727
///
28-
/// - `Poll::Pending` means that this stream's next value is not ready
28+
/// - `Poll::Pending` means that this async iterator's next value is not ready
2929
/// yet. Implementations will ensure that the current task will be notified
3030
/// when the next value may be ready.
3131
///
32-
/// - `Poll::Ready(Some(val))` means that the stream has successfully
32+
/// - `Poll::Ready(Some(val))` means that the async iterator has successfully
3333
/// produced a value, `val`, and may produce further values on subsequent
3434
/// `poll_next` calls.
3535
///
36-
/// - `Poll::Ready(None)` means that the stream has terminated, and
36+
/// - `Poll::Ready(None)` means that the async iterator has terminated, and
3737
/// `poll_next` should not be invoked again.
3838
///
3939
/// # Panics
4040
///
41-
/// Once a stream has finished (returned `Ready(None)` from `poll_next`), calling its
41+
/// Once an async iterator has finished (returned `Ready(None)` from `poll_next`), calling its
4242
/// `poll_next` method again may panic, block forever, or cause other kinds of
43-
/// problems; the `Stream` trait places no requirements on the effects of
43+
/// problems; the `AsyncIterator` trait places no requirements on the effects of
4444
/// such a call. However, as the `poll_next` method is not marked `unsafe`,
4545
/// Rust's usual rules apply: calls must never cause undefined behavior
4646
/// (memory corruption, incorrect use of `unsafe` functions, or the like),
47-
/// regardless of the stream's state.
47+
/// regardless of the async iterator's state.
4848
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
4949

50-
/// Returns the bounds on the remaining length of the stream.
50+
/// Returns the bounds on the remaining length of the async iterator.
5151
///
5252
/// Specifically, `size_hint()` returns a tuple where the first element
5353
/// is the lower bound, and the second element is the upper bound.
@@ -58,12 +58,12 @@ pub trait Stream {
5858
///
5959
/// # Implementation notes
6060
///
61-
/// It is not enforced that a stream implementation yields the declared
62-
/// number of elements. A buggy stream may yield less than the lower bound
61+
/// It is not enforced that an async iterator implementation yields the declared
62+
/// number of elements. A buggy async iterator may yield less than the lower bound
6363
/// or more than the upper bound of elements.
6464
///
6565
/// `size_hint()` is primarily intended to be used for optimizations such as
66-
/// reserving space for the elements of the stream, but must not be
66+
/// reserving space for the elements of the async iterator, but must not be
6767
/// trusted to e.g., omit bounds checks in unsafe code. An incorrect
6868
/// implementation of `size_hint()` should not lead to memory safety
6969
/// violations.
@@ -72,15 +72,15 @@ pub trait Stream {
7272
/// because otherwise it would be a violation of the trait's protocol.
7373
///
7474
/// The default implementation returns <code>(0, [None])</code> which is correct for any
75-
/// stream.
75+
/// async iterator.
7676
#[inline]
7777
fn size_hint(&self) -> (usize, Option<usize>) {
7878
(0, None)
7979
}
8080
}
8181

82-
#[unstable(feature = "async_stream", issue = "79024")]
83-
impl<S: ?Sized + Stream + Unpin> Stream for &mut S {
82+
#[unstable(feature = "async_iterator", issue = "79024")]
83+
impl<S: ?Sized + AsyncIterator + Unpin> AsyncIterator for &mut S {
8484
type Item = S::Item;
8585

8686
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
@@ -92,16 +92,16 @@ impl<S: ?Sized + Stream + Unpin> Stream for &mut S {
9292
}
9393
}
9494

95-
#[unstable(feature = "async_stream", issue = "79024")]
96-
impl<P> Stream for Pin<P>
95+
#[unstable(feature = "async_iterator", issue = "79024")]
96+
impl<P> AsyncIterator for Pin<P>
9797
where
9898
P: DerefMut,
99-
P::Target: Stream,
99+
P::Target: AsyncIterator,
100100
{
101-
type Item = <P::Target as Stream>::Item;
101+
type Item = <P::Target as AsyncIterator>::Item;
102102

103103
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
104-
<P::Target as Stream>::poll_next(self.as_deref_mut(), cx)
104+
<P::Target as AsyncIterator>::poll_next(self.as_deref_mut(), cx)
105105
}
106106

107107
fn size_hint(&self) -> (usize, Option<usize>) {

library/core/src/stream/from_iter.rs library/core/src/async_iter/from_iter.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
use crate::pin::Pin;
22

3-
use crate::stream::Stream;
3+
use crate::async_iter::AsyncIterator;
44
use crate::task::{Context, Poll};
55

6-
/// A stream that was created from iterator.
6+
/// An async iterator that was created from iterator.
77
///
8-
/// This stream is created by the [`from_iter`] function.
8+
/// This async iterator is created by the [`from_iter`] function.
99
/// See it documentation for more.
1010
///
1111
/// [`from_iter`]: fn.from_iter.html
12-
#[unstable(feature = "stream_from_iter", issue = "81798")]
12+
#[unstable(feature = "async_iter_from_iter", issue = "81798")]
1313
#[derive(Clone, Debug)]
1414
pub struct FromIter<I> {
1515
iter: I,
1616
}
1717

18-
#[unstable(feature = "stream_from_iter", issue = "81798")]
18+
#[unstable(feature = "async_iter_from_iter", issue = "81798")]
1919
impl<I> Unpin for FromIter<I> {}
2020

21-
/// Converts an iterator into a stream.
22-
#[unstable(feature = "stream_from_iter", issue = "81798")]
21+
/// Converts an iterator into an async iterator.
22+
#[unstable(feature = "async_iter_from_iter", issue = "81798")]
2323
pub fn from_iter<I: IntoIterator>(iter: I) -> FromIter<I::IntoIter> {
2424
FromIter { iter: iter.into_iter() }
2525
}
2626

27-
#[unstable(feature = "stream_from_iter", issue = "81798")]
28-
impl<I: Iterator> Stream for FromIter<I> {
27+
#[unstable(feature = "async_iter_from_iter", issue = "81798")]
28+
impl<I: Iterator> AsyncIterator for FromIter<I> {
2929
type Item = I::Item;
3030

3131
fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,81 @@
11
//! Composable asynchronous iteration.
22
//!
3-
//! If futures are asynchronous values, then streams are asynchronous
4-
//! iterators. If you've found yourself with an asynchronous collection of some kind,
3+
//! If you've found yourself with an asynchronous collection of some kind,
54
//! and needed to perform an operation on the elements of said collection,
6-
//! you'll quickly run into 'streams'. Streams are heavily used in idiomatic
7-
//! asynchronous Rust code, so it's worth becoming familiar with them.
5+
//! you'll quickly run into 'async iterators'. Async Iterators are heavily used in
6+
//! idiomatic asynchronous Rust code, so it's worth becoming familiar with them.
87
//!
98
//! Before explaining more, let's talk about how this module is structured:
109
//!
1110
//! # Organization
1211
//!
1312
//! This module is largely organized by type:
1413
//!
15-
//! * [Traits] are the core portion: these traits define what kind of streams
14+
//! * [Traits] are the core portion: these traits define what kind of async iterators
1615
//! exist and what you can do with them. The methods of these traits are worth
1716
//! putting some extra study time into.
18-
//! * Functions provide some helpful ways to create some basic streams.
17+
//! * Functions provide some helpful ways to create some basic async iterators.
1918
//! * Structs are often the return types of the various methods on this
2019
//! module's traits. You'll usually want to look at the method that creates
2120
//! the `struct`, rather than the `struct` itself. For more detail about why,
22-
//! see '[Implementing Stream](#implementing-stream)'.
21+
//! see '[Implementing Async Iterator](#implementing-async-iterator)'.
2322
//!
2423
//! [Traits]: #traits
2524
//!
26-
//! That's it! Let's dig into streams.
25+
//! That's it! Let's dig into async iterators.
2726
//!
28-
//! # Stream
27+
//! # Async Iterators
2928
//!
30-
//! The heart and soul of this module is the [`Stream`] trait. The core of
31-
//! [`Stream`] looks like this:
29+
//! The heart and soul of this module is the [`AsyncIterator`] trait. The core of
30+
//! [`AsyncIterator`] looks like this:
3231
//!
3332
//! ```
3433
//! # use core::task::{Context, Poll};
3534
//! # use core::pin::Pin;
36-
//! trait Stream {
35+
//! trait AsyncIterator {
3736
//! type Item;
3837
//! fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
3938
//! }
4039
//! ```
4140
//!
42-
//! Unlike `Iterator`, `Stream` makes a distinction between the [`poll_next`]
43-
//! method which is used when implementing a `Stream`, and a (to-be-implemented)
44-
//! `next` method which is used when consuming a stream. Consumers of `Stream`
41+
//! Unlike `Iterator`, `AsyncIterator` makes a distinction between the [`poll_next`]
42+
//! method which is used when implementing an `AsyncIterator`, and a (to-be-implemented)
43+
//! `next` method which is used when consuming an async iterator. Consumers of `AsyncIterator`
4544
//! only need to consider `next`, which when called, returns a future which
46-
//! yields `Option<Stream::Item>`.
45+
//! yields `Option<AsyncIterator::Item>`.
4746
//!
4847
//! The future returned by `next` will yield `Some(Item)` as long as there are
4948
//! elements, and once they've all been exhausted, will yield `None` to indicate
5049
//! that iteration is finished. If we're waiting on something asynchronous to
51-
//! resolve, the future will wait until the stream is ready to yield again.
50+
//! resolve, the future will wait until the async iterator is ready to yield again.
5251
//!
53-
//! Individual streams may choose to resume iteration, and so calling `next`
52+
//! Individual async iterators may choose to resume iteration, and so calling `next`
5453
//! again may or may not eventually yield `Some(Item)` again at some point.
5554
//!
56-
//! [`Stream`]'s full definition includes a number of other methods as well,
55+
//! [`AsyncIterator`]'s full definition includes a number of other methods as well,
5756
//! but they are default methods, built on top of [`poll_next`], and so you get
5857
//! them for free.
5958
//!
6059
//! [`Poll`]: super::task::Poll
61-
//! [`poll_next`]: Stream::poll_next
60+
//! [`poll_next`]: AsyncIterator::poll_next
6261
//!
63-
//! # Implementing Stream
62+
//! # Implementing Async Iterator
6463
//!
65-
//! Creating a stream of your own involves two steps: creating a `struct` to
66-
//! hold the stream's state, and then implementing [`Stream`] for that
64+
//! Creating an async iterator of your own involves two steps: creating a `struct` to
65+
//! hold the async iterator's state, and then implementing [`AsyncIterator`] for that
6766
//! `struct`.
6867
//!
69-
//! Let's make a stream named `Counter` which counts from `1` to `5`:
68+
//! Let's make an async iterator named `Counter` which counts from `1` to `5`:
7069
//!
7170
//! ```no_run
72-
//! #![feature(async_stream)]
73-
//! # use core::stream::Stream;
71+
//! #![feature(async_iterator)]
72+
//! # use core::async_iter::AsyncIterator;
7473
//! # use core::task::{Context, Poll};
7574
//! # use core::pin::Pin;
7675
//!
7776
//! // First, the struct:
7877
//!
79-
//! /// A stream which counts from one to five
78+
//! /// An async iterator which counts from one to five
8079
//! struct Counter {
8180
//! count: usize,
8281
//! }
@@ -90,9 +89,9 @@
9089
//! }
9190
//! }
9291
//!
93-
//! // Then, we implement `Stream` for our `Counter`:
92+
//! // Then, we implement `AsyncIterator` for our `Counter`:
9493
//!
95-
//! impl Stream for Counter {
94+
//! impl AsyncIterator for Counter {
9695
//! // we will be counting with usize
9796
//! type Item = usize;
9897
//!
@@ -113,17 +112,17 @@
113112
//!
114113
//! # Laziness
115114
//!
116-
//! Streams are *lazy*. This means that just creating a stream doesn't _do_ a
117-
//! whole lot. Nothing really happens until you call `poll_next`. This is
118-
//! sometimes a source of confusion when creating a stream solely for its side
115+
//! Async iterators are *lazy*. This means that just creating an async iterator doesn't
116+
//! _do_ a whole lot. Nothing really happens until you call `poll_next`. This is
117+
//! sometimes a source of confusion when creating an async iterator solely for its side
119118
//! effects. The compiler will warn us about this kind of behavior:
120119
//!
121120
//! ```text
122-
//! warning: unused result that must be used: streams do nothing unless polled
121+
//! warning: unused result that must be used: async iterators do nothing unless polled
123122
//! ```
124123
124+
mod async_iter;
125125
mod from_iter;
126-
mod stream;
127126

127+
pub use async_iter::AsyncIterator;
128128
pub use from_iter::{from_iter, FromIter};
129-
pub use stream::Stream;

library/core/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ pub mod ops;
305305
pub mod any;
306306
pub mod array;
307307
pub mod ascii;
308+
#[unstable(feature = "async_iterator", issue = "79024")]
309+
pub mod async_iter;
308310
pub mod cell;
309311
pub mod char;
310312
pub mod ffi;
@@ -316,8 +318,6 @@ pub mod panic;
316318
pub mod panicking;
317319
pub mod pin;
318320
pub mod result;
319-
#[unstable(feature = "async_stream", issue = "79024")]
320-
pub mod stream;
321321
pub mod sync;
322322

323323
pub mod fmt;

library/core/src/panic/unwind_safe.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
use crate::async_iter::AsyncIterator;
12
use crate::cell::UnsafeCell;
23
use crate::fmt;
34
use crate::future::Future;
45
use crate::ops::{Deref, DerefMut};
56
use crate::pin::Pin;
67
use crate::ptr::{NonNull, Unique};
7-
use crate::stream::Stream;
88
use crate::task::{Context, Poll};
99

1010
/// A marker trait which represents "panic safe" types in Rust.
@@ -290,8 +290,8 @@ impl<F: Future> Future for AssertUnwindSafe<F> {
290290
}
291291
}
292292

293-
#[unstable(feature = "async_stream", issue = "79024")]
294-
impl<S: Stream> Stream for AssertUnwindSafe<S> {
293+
#[unstable(feature = "async_iterator", issue = "79024")]
294+
impl<S: AsyncIterator> AsyncIterator for AssertUnwindSafe<S> {
295295
type Item = S::Item;
296296

297297
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<S::Item>> {

0 commit comments

Comments
 (0)