Maybe unsafe in traits

Hi there! I was working on my unmem crate and faced one problem: i needed index and index_mut methods of unmem::Ptr<T> to be unsafe (they may cause segfaults and other non-rusty stuff) but their signatures don't allow to declare them as unsafe, so i was thinking - can we add something like 'maybe unsafe' to methods/associated functions in traits?

Also is this possible to add unsafe keyword to declarative macros 2.0? Not using unsafe kw inside of them causes compilations error...

Suggestions for additions to the language are better suited to internals.rust-lang.org.

1 Like

Oh, sry. I thought my thoughts are too dumb for IRLO :sweat_smile:

There is no such thing :slight_smile:

2 Likes

Yeah, I just wanted to ask people if they think it’s a good or bad idea or can it be useful :sweat_smile:

Something like this

Say I have a generic T: Foo. It could (but also could not) have marked its bar method as unsafe, so calling it requires unsafe anyway.

It would pretty much be the same as if the trait had an unsafe method, but the implementer was allowed to implement a safe method (which could work if fns were subtypes of unsafe fns, but that's not currently the case). For example Rust Playground

Still, making this change for the Index trait would mean making its index method unsafe, and this would be a major breaking change for anyone using a generic T: Index<I>.

2 Likes

Ok, I understood my idea isn’t really good. Thanks for your point of view.