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

Add some impls of Show (RefCell, Weak, some resolve types) #19388

Merged
merged 2 commits into from
Jan 1, 2015

Conversation

nrc
Copy link
Member

@nrc nrc commented Nov 29, 2014

r? @huonw or @alexcrichton

Apparently, we have previously rejected an RFC like this. However, since then we removed {:?} and so without this debugging gets really difficult as soon as there is a RefCell anywhere, so I believe there is more benefit to adding these impls than there was before. By using "try_borrow" we can avoid panicing in Show (I think).

@ huon in response to a comment in #19254: I noticed that drop() checks for the ptr being null, so I checked here too. Now I am checking for both, if you're confident I can change to only checking strong().

@huonw
Copy link
Member

huonw commented Nov 29, 2014

cc #14759, #14811, #14883, #16714.

Drop checks for null to detect the zeroing that occurs for moved/dropped values to avoid running the destructor twice (necessarily due to the #[unsafe_no_drop_flag] annotation), so I don't believe it's necessary here.

@nrc
Copy link
Member Author

nrc commented Nov 29, 2014

@huonw ah cool, thanks for the explanation, I'll update...

@nrc
Copy link
Member Author

nrc commented Nov 29, 2014

updated

@alexcrichton
Copy link
Member

I'd personally like to continue to push back on these impls for now. I do realize that it's super painful to not be able to use #[deriving(Show)] all the time (Cargo has many Paths in structs), but I'd want to develop a guideline for what precisely Show is intended for. If it's purely meant for debugging in general, then these are totally fine (modulo what precisely they print), but if it's more semantically meaningful then these may not be intended.

@nrc
Copy link
Member Author

nrc commented Dec 1, 2014

@alexcrichton I agree, we need to decide on this (my view is we want at least two traits here for debug and user facing representations), but given that this will require some design work, it is not going to happen soon. In the meantime, we removed :? with the understanding that we could always just use Show instead, if we can't do that through RefCell then it is not much of a replacement.

@nrc
Copy link
Member Author

nrc commented Dec 2, 2014

cc @aturon

@aturon
Copy link
Member

aturon commented Dec 4, 2014

We really need to make progress on the Show/to_string policy issues. This has been sitting on my TODO list for a while but I don't really have a clear proposal yet. @nick29581 would you be interested in hammering on this together?

@alexcrichton
Copy link
Member

Closing in favor of rust-lang/rfcs#504 temporarily, we can reopen once the outcome of that RFC has been decided.

@nrc
Copy link
Member Author

nrc commented Dec 30, 2014

@alexcrichton now the rfc is accepted, can we merge this?

@alexcrichton
Copy link
Member

Sure!

@nrc
Copy link
Member Author

nrc commented Dec 30, 2014

r? @alexcrichton

write!(f, "NULL")
} else {
(*self._ptr).fmt(f)
}
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this need an unsafe block? Additionally, can this use upgrade instead of duplicating logic?

Copy link
Member Author

Choose a reason for hiding this comment

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

Surprisingly it does not. I used to have one in the pre-rebase version of this code, but one level of indirection disappeared and now I don't need it. I can use upgrade though.

@alexcrichton
Copy link
Member

r=me with mostly just using upgrade for Weak, the other is pretty minor.

@@ -719,6 +719,17 @@ impl<T> Clone for Weak<T> {
}
}

#[experimental = "Show is experimental."]
impl<T: fmt::Show> fmt::Show for Weak<T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure this instance is a good idea, because Weak pointers can form a cycle with Rc pointers, making fmt go into an infinite loop.

Copy link
Contributor

Choose a reason for hiding this comment

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

In particular, while the RFC does state that Weak should have an implementation for Show, it might be better to just print something like (still valid Weak). As the RFC says, "implementations of the Show trait are expected to never panic! and always
produce valid UTF-8 data," and an infinite loop does not satisfy this.

An alternative to this would be using some global variable (thread_local is in std, unfortunately) to keep track of recursion depth and printing some special message if that gets too high.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, I guess that by definition, there should be another path to an object if it is held in a weak reference, so printing a place holder is OK. Since we might expect cycles in the presence of weak pointers, this seems like a better move than going down some arbitrary depth of stack around a cycle.

bors added a commit that referenced this pull request Jan 1, 2015
r? @huonw or @alexcrichton

Apparently, we have previously rejected an RFC like this. However, since then we removed `{:?}` and so without this debugging gets really difficult as soon as there is a RefCell anywhere, so I believe there is more benefit to adding these impls than there was before. By using "try_borrow" we can avoid panicing in `Show` (I think).

@ huon in response to a comment in #19254: I noticed that `drop()` checks for the ptr being null, so I checked here too. Now I am checking for both, if you're confident I can change to only checking `strong()`.
@bors bors merged commit f0976e2 into rust-lang:master Jan 1, 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