Use Self keyword instead of concrete type name

This commit is contained in:
Taiki Endo
2023-12-24 21:48:40 +09:00
parent 9e5fccf437
commit f84f363299
11 changed files with 58 additions and 65 deletions

View File

@@ -174,10 +174,10 @@ fn stress_try_send_as_receiver_closes() {
}
impl TestTask {
/// Create a new TestTask
fn new() -> (TestTask, mpsc::Sender<TestRx>) {
fn new() -> (Self, mpsc::Sender<TestRx>) {
let (command_tx, command_rx) = mpsc::channel::<TestRx>(0);
(
TestTask {
Self {
command_rx,
test_rx: None,
countdown: 0, // 0 means no countdown is in progress.

View File

@@ -39,9 +39,9 @@ impl<A, B> Either<A, B> {
// SAFETY: We can use `new_unchecked` because the `inner` parts are
// guaranteed to be pinned, as they come from `self` which is pinned.
unsafe {
match *Pin::get_ref(self) {
Either::Left(ref inner) => Either::Left(Pin::new_unchecked(inner)),
Either::Right(ref inner) => Either::Right(Pin::new_unchecked(inner)),
match self.get_ref() {
Self::Left(inner) => Either::Left(Pin::new_unchecked(inner)),
Self::Right(inner) => Either::Right(Pin::new_unchecked(inner)),
}
}
}
@@ -55,9 +55,9 @@ impl<A, B> Either<A, B> {
// offer an unpinned `&mut A` or `&mut B` through `Pin<&mut Self>`. We
// also don't have an implementation of `Drop`, nor manual `Unpin`.
unsafe {
match *Pin::get_unchecked_mut(self) {
Either::Left(ref mut inner) => Either::Left(Pin::new_unchecked(inner)),
Either::Right(ref mut inner) => Either::Right(Pin::new_unchecked(inner)),
match self.get_unchecked_mut() {
Self::Left(inner) => Either::Left(Pin::new_unchecked(inner)),
Self::Right(inner) => Either::Right(Pin::new_unchecked(inner)),
}
}
}
@@ -69,8 +69,8 @@ impl<A, B, T> Either<(T, A), (T, B)> {
/// Here, the homogeneous type is the first element of the pairs.
pub fn factor_first(self) -> (T, Either<A, B>) {
match self {
Either::Left((x, a)) => (x, Either::Left(a)),
Either::Right((x, b)) => (x, Either::Right(b)),
Self::Left((x, a)) => (x, Either::Left(a)),
Self::Right((x, b)) => (x, Either::Right(b)),
}
}
}
@@ -81,8 +81,8 @@ impl<A, B, T> Either<(A, T), (B, T)> {
/// Here, the homogeneous type is the second element of the pairs.
pub fn factor_second(self) -> (Either<A, B>, T) {
match self {
Either::Left((a, x)) => (Either::Left(a), x),
Either::Right((b, x)) => (Either::Right(b), x),
Self::Left((a, x)) => (Either::Left(a), x),
Self::Right((b, x)) => (Either::Right(b), x),
}
}
}
@@ -91,8 +91,7 @@ impl<T> Either<T, T> {
/// Extract the value of an either over two equivalent types.
pub fn into_inner(self) -> T {
match self {
Either::Left(x) => x,
Either::Right(x) => x,
Self::Left(x) | Self::Right(x) => x,
}
}
}
@@ -119,8 +118,8 @@ where
{
fn is_terminated(&self) -> bool {
match self {
Either::Left(x) => x.is_terminated(),
Either::Right(x) => x.is_terminated(),
Self::Left(x) => x.is_terminated(),
Self::Right(x) => x.is_terminated(),
}
}
}
@@ -141,8 +140,8 @@ where
fn size_hint(&self) -> (usize, Option<usize>) {
match self {
Either::Left(x) => x.size_hint(),
Either::Right(x) => x.size_hint(),
Self::Left(x) => x.size_hint(),
Self::Right(x) => x.size_hint(),
}
}
}
@@ -154,8 +153,8 @@ where
{
fn is_terminated(&self) -> bool {
match self {
Either::Left(x) => x.is_terminated(),
Either::Right(x) => x.is_terminated(),
Self::Left(x) => x.is_terminated(),
Self::Right(x) => x.is_terminated(),
}
}
}

View File

@@ -53,7 +53,7 @@ where
match self.as_mut().project() {
MapProj::Incomplete { future, .. } => {
let output = ready!(future.poll(cx));
match self.project_replace(Map::Complete) {
match self.project_replace(Self::Complete) {
MapProjReplace::Incomplete { f, .. } => Poll::Ready(f.call_once(output)),
MapProjReplace::Complete => unreachable!(),
}

View File

@@ -53,7 +53,7 @@ impl<Fut: Future> MaybeDone<Fut> {
pub fn output_mut(self: Pin<&mut Self>) -> Option<&mut Fut::Output> {
unsafe {
match self.get_unchecked_mut() {
MaybeDone::Done(res) => Some(res),
Self::Done(res) => Some(res),
_ => None,
}
}
@@ -69,7 +69,7 @@ impl<Fut: Future> MaybeDone<Fut> {
}
unsafe {
match mem::replace(self.get_unchecked_mut(), Self::Gone) {
MaybeDone::Done(output) => Some(output),
Self::Done(output) => Some(output),
_ => unreachable!(),
}
}
@@ -91,12 +91,12 @@ impl<Fut: Future> Future for MaybeDone<Fut> {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
unsafe {
match self.as_mut().get_unchecked_mut() {
MaybeDone::Future(f) => {
Self::Future(f) => {
let res = ready!(Pin::new_unchecked(f).poll(cx));
self.set(Self::Done(res));
}
MaybeDone::Done(_) => {}
MaybeDone::Gone => panic!("MaybeDone polled after value taken"),
Self::Done(_) => {}
Self::Gone => panic!("MaybeDone polled after value taken"),
}
}
Poll::Ready(())

View File

@@ -38,7 +38,7 @@ impl<Fut: TryFuture> TryMaybeDone<Fut> {
pub fn output_mut(self: Pin<&mut Self>) -> Option<&mut Fut::Ok> {
unsafe {
match self.get_unchecked_mut() {
TryMaybeDone::Done(res) => Some(res),
Self::Done(res) => Some(res),
_ => None,
}
}
@@ -54,7 +54,7 @@ impl<Fut: TryFuture> TryMaybeDone<Fut> {
}
unsafe {
match mem::replace(self.get_unchecked_mut(), Self::Gone) {
TryMaybeDone::Done(output) => Some(output),
Self::Done(output) => Some(output),
_ => unreachable!(),
}
}
@@ -76,15 +76,15 @@ impl<Fut: TryFuture> Future for TryMaybeDone<Fut> {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
unsafe {
match self.as_mut().get_unchecked_mut() {
TryMaybeDone::Future(f) => match ready!(Pin::new_unchecked(f).try_poll(cx)) {
Self::Future(f) => match ready!(Pin::new_unchecked(f).try_poll(cx)) {
Ok(res) => self.set(Self::Done(res)),
Err(e) => {
self.set(Self::Gone);
return Poll::Ready(Err(e));
}
},
TryMaybeDone::Done(_) => {}
TryMaybeDone::Gone => panic!("TryMaybeDone polled after value taken"),
Self::Done(_) => {}
Self::Gone => panic!("TryMaybeDone polled after value taken"),
}
}
Poll::Ready(Ok(()))

View File

@@ -25,13 +25,13 @@ pub struct LineWriter<W: AsyncWrite> {
impl<W: AsyncWrite> LineWriter<W> {
/// Create a new `LineWriter` with default buffer capacity. The default is currently 1KB
/// which was taken from `std::io::LineWriter`
pub fn new(inner: W) -> LineWriter<W> {
LineWriter::with_capacity(1024, inner)
pub fn new(inner: W) -> Self {
Self::with_capacity(1024, inner)
}
/// Creates a new `LineWriter` with the specified buffer capacity.
pub fn with_capacity(capacity: usize, inner: W) -> LineWriter<W> {
LineWriter { buf_writer: BufWriter::with_capacity(capacity, inner) }
pub fn with_capacity(capacity: usize, inner: W) -> Self {
Self { buf_writer: BufWriter::with_capacity(capacity, inner) }
}
/// Flush `buf_writer` if last char is "new line"

View File

@@ -45,7 +45,7 @@ impl<T: Unpin> ReadHalf<T> {
pub fn reunite(self, other: WriteHalf<T>) -> Result<T, ReuniteError<T>> {
self.handle
.reunite(other.handle)
.map_err(|err| ReuniteError(ReadHalf { handle: err.0 }, WriteHalf { handle: err.1 }))
.map_err(|err| ReuniteError(Self { handle: err.0 }, WriteHalf { handle: err.1 }))
}
}

View File

@@ -22,17 +22,17 @@ impl PollNext {
old
}
fn other(&self) -> PollNext {
fn other(&self) -> Self {
match self {
PollNext::Left => PollNext::Right,
PollNext::Right => PollNext::Left,
Self::Left => Self::Right,
Self::Right => Self::Left,
}
}
}
impl Default for PollNext {
fn default() -> Self {
PollNext::Left
Self::Left
}
}
@@ -46,15 +46,14 @@ enum InternalState {
impl InternalState {
fn finish(&mut self, ps: PollNext) {
match (&self, ps) {
(InternalState::Start, PollNext::Left) => {
*self = InternalState::LeftFinished;
(Self::Start, PollNext::Left) => {
*self = Self::LeftFinished;
}
(InternalState::Start, PollNext::Right) => {
*self = InternalState::RightFinished;
(Self::Start, PollNext::Right) => {
*self = Self::RightFinished;
}
(InternalState::LeftFinished, PollNext::Right)
| (InternalState::RightFinished, PollNext::Left) => {
*self = InternalState::BothFinished;
(Self::LeftFinished, PollNext::Right) | (Self::RightFinished, PollNext::Left) => {
*self = Self::BothFinished;
}
_ => {}
}

View File

@@ -56,15 +56,13 @@ struct SharedPollState {
impl SharedPollState {
/// Constructs new `SharedPollState` with the given state.
fn new(value: u8) -> SharedPollState {
SharedPollState { state: Arc::new(AtomicU8::new(value)) }
fn new(value: u8) -> Self {
Self { state: Arc::new(AtomicU8::new(value)) }
}
/// Attempts to start polling, returning stored state in case of success.
/// Returns `None` if either waker is waking at the moment.
fn start_polling(
&self,
) -> Option<(u8, PollStateBomb<'_, impl FnOnce(&SharedPollState) -> u8>)> {
fn start_polling(&self) -> Option<(u8, PollStateBomb<'_, impl FnOnce(&Self) -> u8>)> {
let value = self
.state
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |value| {
@@ -75,7 +73,7 @@ impl SharedPollState {
}
})
.ok()?;
let bomb = PollStateBomb::new(self, SharedPollState::reset);
let bomb = PollStateBomb::new(self, Self::reset);
Some((value, bomb))
}
@@ -87,7 +85,7 @@ impl SharedPollState {
fn start_waking(
&self,
to_poll: u8,
) -> Option<(u8, PollStateBomb<'_, impl FnOnce(&SharedPollState) -> u8>)> {
) -> Option<(u8, PollStateBomb<'_, impl FnOnce(&Self) -> u8>)> {
let value = self
.state
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |value| {
@@ -106,7 +104,7 @@ impl SharedPollState {
// Only start the waking process if we're not in the polling/waking phase and the stream isn't woken already
if value & (WOKEN | POLLING | WAKING) == NONE {
let bomb = PollStateBomb::new(self, SharedPollState::stop_waking);
let bomb = PollStateBomb::new(self, Self::stop_waking);
Some((value, bomb))
} else {
@@ -261,7 +259,7 @@ impl<St> PollStreamFut<St> {
}
impl<St: Stream + Unpin> Future for PollStreamFut<St> {
type Output = Option<(St::Item, PollStreamFut<St>)>;
type Output = Option<(St::Item, Self)>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut stream = self.project().stream;
@@ -271,7 +269,7 @@ impl<St: Stream + Unpin> Future for PollStreamFut<St> {
} else {
None
};
let next_item_fut = PollStreamFut::new(stream.get_mut().take());
let next_item_fut = Self::new(stream.get_mut().take());
let out = item.map(|item| (item, next_item_fut));
Poll::Ready(out)
@@ -320,13 +318,10 @@ where
Fc: FlowController<St::Item, <St::Item as Stream>::Item>,
St::Item: Stream + Unpin,
{
pub(crate) fn new(
stream: St,
limit: Option<usize>,
) -> FlattenUnorderedWithFlowController<St, Fc> {
pub(crate) fn new(stream: St, limit: Option<usize>) -> Self {
let poll_state = SharedPollState::new(NEED_TO_POLL_STREAM);
FlattenUnorderedWithFlowController {
Self {
inner_streams: FuturesUnordered::new(),
stream,
is_stream_done: false,

View File

@@ -37,7 +37,7 @@ impl<T, Fut> UnfoldState<T, Fut> {
pub(crate) fn take_value(self: Pin<&mut Self>) -> Option<T> {
match &*self {
UnfoldState::Value { .. } => match self.project_replace(UnfoldState::Empty) {
Self::Value { .. } => match self.project_replace(Self::Empty) {
UnfoldStateProjReplace::Value { value } => Some(value),
_ => unreachable!(),
},

View File

@@ -14,7 +14,7 @@ fn issue2310() {
impl MyRead {
fn new() -> Self {
MyRead { first: false }
Self { first: false }
}
}
@@ -41,7 +41,7 @@ fn issue2310() {
impl VecWrapper {
fn new() -> Self {
VecWrapper { inner: Vec::new() }
Self { inner: Vec::new() }
}
}