Skip to content

Implement Debug for RwLock and arc::Weak #22573

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

Merged
merged 1 commit into from
Feb 27, 2015

Conversation

nwin
Copy link
Contributor

@nwin nwin commented Feb 20, 2015

Implements Debug for RwLock and arc::Weak in the same way it is implemented for rc::Weak (basically copy & paste).

The lack of this implementation prevents the automatic implementation of Debug for structs containing members of these types.

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @huonw (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see CONTRIBUTING.md for more information.

@huonw
Copy link
Member

huonw commented Feb 20, 2015

Maybe you could add an implementation for Mutex too?

I wonder if this and RwLock could mirror RefCell by calling try_lock or try_read (respectively) to print the data if a handle can be acquired, otherwise falling back to locked or something:

use std::cell::RefCell;
fn main() {
    let cell = RefCell::new(1);
    println!("{:?}", cell);
    let _b = cell.borrow_mut();
    println!("{:?}", cell);
}
RefCell { value: 1 }
RefCell { <borrowed> }

@huonw
Copy link
Member

huonw commented Feb 20, 2015

Thanks for the patch!

@nwin
Copy link
Contributor Author

nwin commented Feb 20, 2015

The same could also work for (a)rc::Weak. I’m just wondering if it is a good idea to acquire these resources just for Debug-printing.

@nwin
Copy link
Contributor Author

nwin commented Feb 20, 2015

I added Debug for Mutex and made the changes you suggested to RwLock. I didn’t do that for Weak (possible cycles).

fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.try_lock() {
Ok(guard) => write!(f, "Mutex {{ data: {:?} }}", *guard),
Err(_) => write!(f, "Mutex {{ <locked> }}")
Copy link
Member

Choose a reason for hiding this comment

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

This could also test for each variant of Err as one case is that the mutex is already locked but another case is that the mutex is poisoned (but the lock was successfully acquired).

@alexcrichton
Copy link
Member

@nwin looks good to me, thanks! Could you squash the commits together as well?

Also feel free to comment on the PR whenever you update it, unfortunately github doesn't send out notifications for force-pushes.

@nwin nwin force-pushed the impl-debug-rwlock-weak branch from 87cefcd to 8c778f2 Compare February 25, 2015 07:54
@nwin
Copy link
Contributor Author

nwin commented Feb 25, 2015

Ok, done.

@alexcrichton
Copy link
Member

@bors: r+ 8c778f2

Thanks!

@bors
Copy link
Collaborator

bors commented Feb 25, 2015

⌛ Testing commit 8c778f2 with merge d885154...

@bors
Copy link
Collaborator

bors commented Feb 25, 2015

💔 Test failed - auto-linux-64-x-android-t

@Manishearth
Copy link
Member

@bors: retry

@bors
Copy link
Collaborator

bors commented Feb 25, 2015

2 similar comments
@bors
Copy link
Collaborator

bors commented Feb 25, 2015

@bors
Copy link
Collaborator

bors commented Feb 25, 2015

@bors
Copy link
Collaborator

bors commented Feb 25, 2015

💔 Test failed - auto-win-64-nopt-t

@Manishearth
Copy link
Member

@bors: retry

(Please ignore the failures for now)

@bors
Copy link
Collaborator

bors commented Feb 25, 2015

3 similar comments
@bors
Copy link
Collaborator

bors commented Feb 25, 2015

@bors
Copy link
Collaborator

bors commented Feb 25, 2015

@bors
Copy link
Collaborator

bors commented Feb 25, 2015

@bors
Copy link
Collaborator

bors commented Feb 25, 2015

💔 Test failed - auto-win-32-opt

@alexcrichton
Copy link
Member

@bors: retry

@bors
Copy link
Collaborator

bors commented Feb 25, 2015

@bors
Copy link
Collaborator

bors commented Feb 25, 2015

💔 Test failed - auto-win-64-nopt-t

@alexcrichton
Copy link
Member

@bors: retry

@bors
Copy link
Collaborator

bors commented Feb 25, 2015

@bors
Copy link
Collaborator

bors commented Feb 25, 2015

💔 Test failed - auto-linux-64-x-android-t

@nwin
Copy link
Contributor Author

nwin commented Feb 25, 2015

bors seems to be in a mood today… ;)

@Manishearth
Copy link
Member

Oh, no, that's just me manually cancellig (forcing) the build for the rollup. Ignore all bors comments (you're in the rollup) :)

@Manishearth
Copy link
Member

@bors: retry

@Manishearth
Copy link
Member

So sorry, thought I had added this to the rollup.

@bors
Copy link
Collaborator

bors commented Feb 26, 2015

⌛ Testing commit 8c778f2 with merge 044ee8b...

@bors
Copy link
Collaborator

bors commented Feb 26, 2015

💔 Test failed - auto-mac-64-opt

@bors
Copy link
Collaborator

bors commented Feb 26, 2015

💔 Test failed - auto-mac-32-opt

@Manishearth
Copy link
Member

shakes fist at stage2

/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/mutex.rs:262:17: 262:27 warning: pattern binding `WouldBlock` is named the same as one of the variants of the type `sync::poison::TryLockError<sync::mutex::MutexGuard<'_, T>>` [E0170]
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/mutex.rs:262             Err(WouldBlock) => write!(f, "Mutex {{ <locked> }}")
                                                                                                            ^~~~~~~~~~
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/mutex.rs:262:17: 262:27 help: if you meant to match on a variant, consider making the path in the pattern qualified: `sync::poison::TryLockError<sync::mutex::MutexGuard<'_, T>>::WouldBlock`
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/mutex.rs:262             Err(WouldBlock) => write!(f, "Mutex {{ <locked> }}")
                                                                                                            ^~~~~~~~~~
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/rwlock.rs:267:17: 267:27 warning: pattern binding `WouldBlock` is named the same as one of the variants of the type `sync::poison::TryLockError<sync::rwlock::RwLockReadGuard<'_, T>>` [E0170]
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/rwlock.rs:267             Err(WouldBlock) => write!(f, "RwLock {{ <locked> }}")
                                                                                                             ^~~~~~~~~~
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/rwlock.rs:267:17: 267:27 help: if you meant to match on a variant, consider making the path in the pattern qualified: `sync::poison::TryLockError<sync::rwlock::RwLockReadGuard<'_, T>>::WouldBlock`
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/rwlock.rs:267             Err(WouldBlock) => write!(f, "RwLock {{ <locked> }}")
                                                                                                             ^~~~~~~~~~
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/mutex.rs:262:17: 262:27 error: unused variable: `WouldBlock`, #[deny(unused_variables)] on by default
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/mutex.rs:262             Err(WouldBlock) => write!(f, "Mutex {{ <locked> }}")
                                                                                                            ^~~~~~~~~~
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/mutex.rs:262:17: 262:27 error: variable `WouldBlock` should have a snake case name such as `would_block`, #[deny(non_snake_case)] on by default
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/mutex.rs:262             Err(WouldBlock) => write!(f, "Mutex {{ <locked> }}")
                                                                                                            ^~~~~~~~~~
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/rwlock.rs:267:17: 267:27 error: unused variable: `WouldBlock`, #[deny(unused_variables)] on by default
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/rwlock.rs:267             Err(WouldBlock) => write!(f, "RwLock {{ <locked> }}")
                                                                                                             ^~~~~~~~~~
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/rwlock.rs:267:17: 267:27 error: variable `WouldBlock` should have a snake case name such as `would_block`, #[deny(non_snake_case)] on by default
/Users/rustbuild/src/rust-buildbot/slave/auto-mac-64-opt/build/src/libstd/sync/rwlock.rs:267             Err(WouldBlock) => write!(f, "RwLock {{ <locked> }}")

@nwin nwin force-pushed the impl-debug-rwlock-weak branch from 8c778f2 to 36ba96e Compare February 26, 2015 09:18
@nwin
Copy link
Contributor Author

nwin commented Feb 26, 2015

Stupid me. This time I ran the complete compilation process and updated the PR.

@Manishearth
Copy link
Member

@bors: retry clean

@Manishearth
Copy link
Member

@bors: clean r+

@bors
Copy link
Collaborator

bors commented Feb 26, 2015

@bors r=Manishearth 36ba96e

@bors
Copy link
Collaborator

bors commented Feb 26, 2015

⌛ Testing commit 36ba96e with merge 0021151...

@bors
Copy link
Collaborator

bors commented Feb 26, 2015

💔 Test failed - auto-win-64-nopt-t

@nwin
Copy link
Contributor Author

nwin commented Feb 26, 2015

This looks like a temporary issue to me…can anybody trigger a retry with bors?

@alexcrichton
Copy link
Member

@bors: retry

@bors
Copy link
Collaborator

bors commented Feb 27, 2015

⌛ Testing commit 36ba96e with merge 545b0c4...

@bors
Copy link
Collaborator

bors commented Feb 27, 2015

💔 Test failed - auto-mac-64-nopt-t

@alexcrichton
Copy link
Member

@bors: retry

bors added a commit that referenced this pull request Feb 27, 2015
Implements `Debug`  for `RwLock` and `arc::Weak` in the same way it is implemented for `rc::Weak` (basically copy & paste).

The lack of this implementation prevents the automatic implementation of `Debug` for structs containing members of these types.
@bors
Copy link
Collaborator

bors commented Feb 27, 2015

⌛ Testing commit 36ba96e with merge bd0d8e4...

@bors bors merged commit 36ba96e into rust-lang:master Feb 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants