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

Allow crates defining type T to implement external traits for [T] #39328

Closed
rnewman opened this issue Jan 26, 2017 · 2 comments
Closed

Allow crates defining type T to implement external traits for [T] #39328

rnewman opened this issue Jan 26, 2017 · 2 comments

Comments

@rnewman
Copy link

rnewman commented Jan 26, 2017

Scenario:

Expected:

  • I can implement Display for [T], just as I can for T, allowing third-party libraries to call Display-requiring methods with both slices and individual values.

Actual:

  • [T] is a type that isn't defined in my crate. Display is a trait that isn't defined in my crate. The compiler refuses to allow me to implement it.

Example playground:

https://is.gd/lLsx57https://is.gd/lLsx57

The stated workaround (from IRC; thanks talchas) is to define some kind of wrapper type, MySlice, that I own. This wrapper contains a reference to the real slice, and implements every necessary trait as a passthrough to the slice. That fixes the ownership of the type, allowing implementation of Display.

This workaround presupposes that it's possible to get the third-party library to accept a MySlice wherever it calls arbitrary traits, rather than it getting a &[T] out of something else. I haven't assessed whether that's feasible for something like combine, which naturally ends up taking sub-slices of inputs.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 27, 2017

This probably belongs into the rfc repo: https://github.com/rust-lang/rfcs/issues/

@Mark-Simulacrum
Copy link
Member

Please open an issue on the RFCs repo or post a pre-RFC to internals.rust-lang.org. This is a fairly major change to the language (large implications if small implementation, or at least, I think it should be small).

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

No branches or pull requests

3 participants