Skip to content

Commit

Permalink
Review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull committed Apr 2, 2023
1 parent 8208309 commit 72bfe07
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ enum TaskRef<'a> {

impl TaskRef<'_> {
/// Tells if this task will wake up the other task.
#[allow(unreachable_patterns)]
fn will_wake(self, other: Self) -> bool {
match (self, other) {
(Self::Waker(a), Self::Waker(b)) => a.will_wake(b),
Expand All @@ -870,7 +871,6 @@ impl TaskRef<'_> {
// TODO: Use unreleased will_unpark API.
false
}
#[cfg(feature = "std")]
_ => false,
}
}
Expand Down
85 changes: 36 additions & 49 deletions src/no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,38 +287,46 @@ enum Entry {
Sentinel,
}

impl fmt::Debug for Entry {
struct TakenState<'a> {
slot: &'a Cell<State>,
state: State,
}

impl Drop for TakenState<'_> {
fn drop(&mut self) {
self.slot
.set(mem::replace(&mut self.state, State::NotifiedTaken));
}
}

impl fmt::Debug for TakenState<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct TakenState<'a> {
state: Option<State>,
cell: &'a Cell<State>,
}
fmt::Debug::fmt(&self.state, f)
}
}

impl Drop for TakenState<'_> {
fn drop(&mut self) {
self.cell.set(self.state.take().unwrap());
}
}
impl PartialEq for TakenState<'_> {
fn eq(&self, other: &Self) -> bool {
self.state == other.state
}
}

impl fmt::Debug for TakenState<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.state.as_ref().unwrap(), f)
}
}
impl<'a> TakenState<'a> {
fn new(slot: &'a Cell<State>) -> Self {
let state = slot.replace(State::NotifiedTaken);
Self { slot, state }
}
}

impl fmt::Debug for Entry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Entry::Listener { state, next, prev } => {
let taken = TakenState {
state: Some(state.replace(State::Created)),
cell: state,
};

f.debug_struct("Listener")
.field("state", &taken)
.field("prev", prev)
.field("next", next)
.finish()
}
Entry::Listener { state, next, prev } => f
.debug_struct("Listener")
.field("state", &TakenState::new(state))
.field("prev", prev)
.field("next", next)
.finish(),
Entry::Empty(next) => f.debug_tuple("Empty").field(next).finish(),
Entry::Sentinel => f.debug_tuple("Sentinel").finish(),
}
Expand All @@ -327,17 +335,6 @@ impl fmt::Debug for Entry {

impl PartialEq for Entry {
fn eq(&self, other: &Entry) -> bool {
struct RestoreState<'a> {
state: Option<State>,
cell: &'a Cell<State>,
}

impl Drop for RestoreState<'_> {
fn drop(&mut self) {
self.cell.set(self.state.take().unwrap());
}
}

match (self, other) {
(
Self::Listener {
Expand All @@ -351,17 +348,7 @@ impl PartialEq for Entry {
next: next2,
},
) => {
let taken1 = RestoreState {
state: Some(state1.replace(State::Created)),
cell: state1,
};

let taken2 = RestoreState {
state: Some(state2.replace(State::Created)),
cell: state2,
};

if taken1.state != taken2.state {
if TakenState::new(state1) != TakenState::new(state2) {
return false;
}

Expand Down
12 changes: 7 additions & 5 deletions src/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,13 @@ impl crate::Inner {

State::Task(other_task) => {
// Only replace the task if it's different.
if !task.will_wake(other_task.as_task_ref()) {
entry.state.set(State::Task(task.into_task()));
} else {
entry.state.set(State::Task(other_task));
}
entry.state.set(State::Task({
if !task.will_wake(other_task.as_task_ref()) {
task.into_task()
} else {
other_task
}
}));

Some(false)
}
Expand Down

0 comments on commit 72bfe07

Please sign in to comment.