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
Also implement AsUniformValue for textures directly #1956
Also implement AsUniformValue for textures directly #1956
Conversation
Currently this fails to compile with: ``` error[E0515]: cannot return value referencing function parameter `texture` --> tests/texture_creation.rs:103:9 | 103 | texture.as_uniform_value() | -------^^^^^^^^^^^^^^^^^^^ | | | returns a value referencing data owned by the current function | `texture` is borrowed here error: aborting due to previous error For more information about this error, try `rustc --explain E0515`. ```
In addition to implementing if for references, i.e. `impl<'a> AsUniformValue for &'a {myname}`. The method `as_uniform_value(&self)` takes self by reference, so there should be no need to require the second-level reference. This allows a longer lifetime to be automatically inferred. Fixes compilation of the texture_2d_as_uniform_value_lifetime() test from the previous commit. This commit doesn't remove the original impl above in order not to introduce breaking changes. Removing would mean every texture passing in `uniform! {}` would have to change.
Thank you @strohel! We're currently using this fork for a portion our pipeline, this change makes it easier to work with uniforms in an idiomatic Rust manner. Let's get this merged! |
Why would it need a change? As far as I'm aware, when a Trait is implemented for T, then it's also implemented for &'a T, no? |
Yes when used with a dot operator (method call resolution), but not when used as other function argument, per https://doc.rust-lang.org/nightly/nomicon/coercions.html:
The culprit here is
Passing textures by value might not be an option when the caller does not want to lose ownership. If anyone has ideas how to resolve this without keeping the 2 similar AsUniformValue implementations, I'll happily try them out. |
@strohel fair enough. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Thanks, @est31, that was quick! |
@est31, can we please request cutting a patch release with this fix? It would allow us to publish a package that depends on it. |
@strohel sure, will do! |
In addition to implementing if for references, i.e.
impl<'a> AsUniformValue for &'a {TextureType}
.The method
as_uniform_value(&self)
takes self by reference, so thereshould be no need to require the second-level reference.
This allows a longer lifetime to be automatically inferred. Fixes
compilation of the texture_2d_as_uniform_value_lifetime() test from the
previous commit.
This commit doesn't remove the original impl above in order not to introduce
breaking changes. Removing would mean every texture passing in
uniform! {}
would have to change.
Should fix #1400. We should also be able to close #1054 (resolved in a slightly different manner).
Related closed issues/PRs: #1629, #1632.
Also related: #1850 - not fixed in this PR, but a similar change to impl for
Sampler
could fix it.CC @slightknack.