Get the latest tech news

in Rust, methods should be object safe


I think we should use “method”, in Rust, to refer specifically to associated functions which would be object safe if put in a trait. In this terminology scheme: - associated function refers to any function defined in an implblock, which is how most people use it now1,2 - trait function refers to any function defined in a trait - method specifically refers to associated functions with receivers like those specified in the object safety section of the Reference3 - and we should come up with another name for associated functions that take selfbut are not object-safe, such as taking selfby value.

If we try to name such a collection - say, for instance, by putting our various Increment types on the heap with Box- we get a very interesting compiler error. The compiler doesn’t necessarily know the types of all implementers of Increment at the time increment_all is being generated, so monomorphization isn’t an option. An object-safe version of Increment would take&mut self and modify the Counter, Turnstyle, or whatever other value in place, much like a Java method with similar functionality.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Methods

Methods

Photo of Rust

Rust

Related news:

News photo

Dioxus S23 is hiring a cracked founding engineer (Rust)

News photo

Writing an NVMe Driver in Rust [pdf]

News photo

RISC-V Now Supports Rust In the Linux Kernel