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

fix(trait docs): remove unnecessary where Self: Sized clause #1400

Closed

Conversation

poliorcetics
Copy link
Contributor

This is probably the result of a copy-paste but it's unnecessary for the purpose of the example and leads to the error returned by the playground to be about where Self: Sized first instead of the generic type T, making the example confusing for this error.

@ehuss
Copy link
Contributor

ehuss commented Sep 10, 2023

I believe the original is correct. The example is trying to show that the given trait is valid, it's just that the given associated functions cannot be dispatched on. In other words, if you remove the code that actually tries to dispatch on the functions, it should compile successfully. If the Self: Sized bound is removed, then the trait can't be compiled at all which isn't want the example is trying to illustrate.

@poliorcetics
Copy link
Contributor Author

The example can be compiled fully when removing the dispatch calls, even with my change: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=fc746026dbcce1c39efaf5257d67b5c0

The function is taking &self so where Self: Sized is unnecessary here

@ehuss
Copy link
Contributor

ehuss commented Sep 10, 2023

But it will fail to compile if you also include the line:

let obj: Box<dyn NonDispatchable> = Box::new(S);

That's what it is trying to illustrate. That you can create a trait object, but you can't dispatch on those specific functions. Without the Self: Sized, it prevents it from being able to create a trait object at all. The illustration of non-object safety is below in NotObjectSafe, which shows the generics without Self: Sized prevents being able to create an object.

@poliorcetics
Copy link
Contributor Author

Ah sorry, you're right, I misunderstood the example and surrounding text

@poliorcetics poliorcetics deleted the small-trait-docs-fix branch September 11, 2023 05:06
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.

2 participants