This proposal introduces a new trait, IntoView, inspired by a feature in the Vulkan programming language (my own language — otherwise, you might think I'm talking about the Vulkan API). The core idea is to allow types to define a contextual view of themselves as another type, which can be implicitly used in argument positions.
Rust traits like Deref, AsRef, From, and Into are either limited in scope or require explicit invocation:
Deref only applies to method resolution and *expr, but not in function arguments (e.g. fn f(x: i32) wont accept T: Deref<Target=i32>).
AsRef and Into require explicit calls like .as_ref() or .into(), even when conversions are unambiguous.
Method resolution uses &self, &mut self, or self, but is bound to syntax and ownership semantics — not general conversion.
In Vulkan, IntoView trait (not an actual trait, but a protocol-like construct in Vulkan language) allows types to define how they present themselves as another type:
class Foo {
val value: i32;
impl IntoView<i32> {
fn view(&self): i32 {
self.value
}
}
}
fn take_i32(v: i32) { /* ... */ }
fn main() {
val a = Foo(999);
take_i32(a); // Automatically calls .view()
}
Rust Equivalent (hypothetical)
struct Foo(i32)
impl IntoView<i32> for Foo {
fn view(&self) -> i32 {
self.0
}
}
fn take_i32(v: i32) { /* ... */ }
fn main() {
let a = Foo(999);
take_i32(a); // Automatically uses view()
}
The compiler would use view(&self), view(&mut self), or view(self) based on context and ownership, similar to how method resolution already works, but across argument expectations.
In addition to function arguments, IntoView can also enable automatic conversion of values during variable assignment:
struct Foo(u32);
impl IntoView<u32> for Foo {
fn view(self) -> u32 {
self.0
}
}
fn main() {
let a = Foo(999);
let num: u32 = a; // compiler implicitly calls a.view();
}