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

future: provide infrastructure to print shared-ownership types in a way that reflects the shared graph structure #6187

Closed
pnkfelix opened this issue May 2, 2013 · 8 comments
Labels
A-pretty Area: Pretty printing (including `-Z unpretty`)

Comments

@pnkfelix
Copy link
Member

pnkfelix commented May 2, 2013

Old title: future: extend %? printing to reflect shared graph structure of @-referenced objects

Previously this bug was just covering @mut T (now Gc<RefCell<T>>) and described overriding the reflection-based {:?} printer. Old text below.

Some types like Rc and Gc provide the ability to share state, it would be neat if we could print values that reflected this (see below for some examples). Providing a general interface that types can use to get equivalent printing would be optimal, but, if necessary, we can restrict it to just being implemented for Rc and Gc (although of course others would be welcome to borrow/adjust the implementation for their use case).

This may end up requiring some special formatting traits (or something like ~[Rc<T>] will not be printed properly), and so may be better suited to an external library.

Old text.

Discussions on Issue #3768 made me wonder about how we might extend Rust to precisely (and succinctly) print out structure with shared state.

Consider for example this code:

fn main() {
    struct two<L,R> { l:L, r:R };
    let a = two{ l:0, r:0 };
    let b = @mut a;
    let c = two{ l:&b, r: b};
    let g = two{ l:&c, r: @mut a };
    io::println(fmt!(" pre g: %?", g));
    io::println("now set g.l.l.l = 1;");
    g.l.l.l = 1;
    io::println(fmt!("post g: %?", g));
}

the print out you get is:

 pre g: {l: &{l: &@mut {l: 0, r: 0}, r: @mut {l: 0, r: 0}}, r: @mut {l: 0, r: 0}}
now set g.l.l.l = 1;
post g: {l: &{l: &@mut {l: 1, r: 0}, r: @mut {l: 1, r: 0}}, r: @mut {l: 0, r: 0}}

It might be nice if we extended the notation for @-references so that if there is shared substructure, it is annotated to reflect the shared objects' identities. As inspired by say Lisp/Scheme, many dialects of which use #n=<foo> as a binder and #n# as a reference, we might print:

 pre g: {l: &{l: &@mut #1={l: 0, r: 0}, r: @mut #1#}, r: @mut {l: 0, r: 0}}
now set g.l.l.l = 1;
post g: {l: &{l: &@mut #1={l: 1, r: 0}, r: @mut #1#}, r: @mut {l: 0, r: 0}}

Caveat: I'm not wedded to any particular notation, and perhaps this job is better left to an end-user library to handle, rather than coupling it to %?.

But since some people do want %? to be robust in the face of cycles, we might consider also keeping it honest? Just a thought.

@catamorphism
Copy link
Contributor

(bug triage) Still open. Far-future seems right.

@emberian
Copy link
Member

Visiting for triage. cc @alexcrichton

@alexcrichton
Copy link
Member

This would definitely be pretty cool, and the relevant stuff you'd have to change would be mostly in repr/reflect. The ifmt changes don't look like they'll conflict with this other than just changing what the output of {:?} is

@huonw
Copy link
Member

huonw commented Aug 20, 2013

As a minor consideration, this means that ? won't print valid Rust code (assuming that this is a goal). Although it currently doesn't do this anyway.

@huonw
Copy link
Member

huonw commented Aug 4, 2014

Triage, no change that I know of.

@steveklabnik
Copy link
Member

I wonder what @aturon thinks about this in the light of the new Show/String changes

@aturon
Copy link
Member

aturon commented Feb 2, 2015

This was originally talking about reflection infrastructure which has since been removed. I think it should be moved to the RFCs issue tracker as a wishlist item.

@steveklabnik
Copy link
Member

I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized.

This issue has been moved to the RFCs repo: rust-lang/rfcs#781

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-pretty Area: Pretty printing (including `-Z unpretty`)
Projects
None yet
Development

No branches or pull requests

7 participants