Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fmt::Debug to produce new info #86

Merged
merged 27 commits into from
Oct 22, 2023
Merged

Conversation

mamaicode
Copy link
Contributor

Closes #79
Let me know what to change

@notgull notgull self-assigned this Oct 12, 2023
Copy link
Member

@notgull notgull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely like Event including the number of notified listeners and the number of total listeners. However, the debug output fields should use snake_case, e.g. notified_listeners: 7.

For EventListener, State indicates the states that the listener can be in. I think that this state should be reflected in the debug output.

src/lib.rs Outdated
Comment on lines 119 to 126
pub notified: AtomicUsize,

/// Inner queue of event listeners.
///
/// On `std` platforms, this is an intrusive linked list. On `no_std` platforms, this is a
/// more traditional `Vec` of listeners, with an atomic queue used as a backup for high
/// contention.
list: sys::List<T>,
pub list: sys::List<T>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These fields don't need to be pub, I think.

src/lib.rs Outdated
@@ -171,7 +171,15 @@ impl<T> core::panic::RefUnwindSafe for Event<T> {}

impl<T> fmt::Debug for Event<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Event { .. }")
let inner = self.inner();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling inner(), call try_inner(), and if it returns None output Event(<uninitialized>) using debug_tuple. See here for an example.

src/lib.rs Outdated
Comment on lines 661 to 665
if self.is_listening() {
ds.field("Listening", &"Yes");
} else {
ds.field("Listening", &"No");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of yes and no, I would just use true and false, which can be done by just using self.is_listening() as the field here.

src/lib.rs Outdated
@@ -202,7 +210,6 @@ impl<T> Event<T> {
inner: AtomicPtr::new(ptr::null_mut()),
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add back these lines to reduce diff noise?

src/lib.rs Outdated
@@ -456,7 +463,6 @@ impl Event<()> {
inner: AtomicPtr::new(ptr::null_mut()),
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

src/no_std.rs Outdated
Comment on lines 243 to 250
pub fn total_listeners(&self) -> usize {
self.inner
.try_lock()
.as_ref()
.map(|lock| &**lock)
.map(|listener_slab| listener_slab.listeners.len())
.unwrap_or(0)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns something different from the total_listeners in std.rs.

src/no_std.rs Outdated
Comment on lines 247 to 248
.map(|lock| &**lock)
.map(|listener_slab| listener_slab.listeners.len())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use map() twice here, just get listeners from lock itself.

src/lib.rs Outdated
Comment on lines 178 to 182
write!(
f,
"Event {{\n Number of notified listeners: {:?}\n Total number of listeners: {:?}\n}}",
notified_count, total_count
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To simplify, use debug_struct to construct the output instead of manually formatting like this.

@notgull
Copy link
Member

notgull commented Oct 12, 2023

Rebase on the current master to fix the current CI error (#84)

@mamaicode
Copy link
Contributor Author

Let me know if I missed something

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated
@@ -202,7 +210,6 @@ impl<T> Event<T> {
inner: AtomicPtr::new(ptr::null_mut()),
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add back these lines to reduce diff noise?

src/std.rs Outdated
@@ -44,6 +44,10 @@ impl<T> List<T> {
notified: 0,
}))
}
// Accessor method because fields are private, not sure how to go around it
pub fn total_listeners(&self) -> Result<usize, &str> {
self.0.lock().map(|mutex| mutex.len).map_err(|_| "<locked>")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do try_lock() to avoid contention?

Copy link
Member

@notgull notgull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost good, just a few nitpicks.

src/lib.rs Outdated
.finish()
}
None => f
.debug_tuple("event")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalize "Event"

src/lib.rs Outdated Show resolved Hide resolved
@mamaicode
Copy link
Contributor Author

mamaicode commented Oct 21, 2023

fixed it all
Sorry for the issues and wait time, fixed it all

Copy link
Member

@notgull notgull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@notgull notgull merged commit 74e8231 into smol-rs:master Oct 22, 2023
9 checks passed
@notgull notgull mentioned this pull request Oct 29, 2023
notgull added a commit that referenced this pull request Apr 14, 2024
cc #86, it's harder to get this info on no_std so I've ignored it for
now

Signed-off-by: John Nunley <[email protected]>
notgull added a commit that referenced this pull request Apr 16, 2024
cc #86, it's harder to get this info on no_std so I've ignored it for
now

Signed-off-by: John Nunley <[email protected]>
notgull added a commit that referenced this pull request Apr 18, 2024
cc #86, it's harder to get this info on no_std so I've ignored it for
now

Signed-off-by: John Nunley <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

fmt::Debug should produce actually useful output
2 participants