Stylistic choice for this trait method's name?


#1

I’m defining an unsafe variant of the Default trait which accepts a raw pointer and fills that memory instead of returning a value. I’m wondering what the best name for this method should be. I see two options: default is pithy, but it conflicts with the name used by Default, which isn’t actually a problem, but could make code harder to read. unsafe_default is less pithy, but avoids the previous problem. Is there a standard idiomatic approach here? To clarify, the two options are:

unsafe trait UnsafeDefault {
    unsafe fn default(ptr: *mut Self);
}
unsafe trait UnsafeDefault {
    unsafe fn unsafe_default(ptr: *mut Self);
}

#2

I would go for unsafe_default - the names of single method traits typically match the name of the method. In addition for this case in particular, Default is in the prelude so you may end up running into conflicts when calling SomeType::default.


#3

Unrelated: the method should probably be marked unsafe, not the trait. Anyone could create a random pointer using safe code and call the function.


#4

OK sounds good; I’ll do that. Thanks!


#5

Ah, right you are. I actually had it that way in my code, but accidentally omitted it when I transcribed the trait definition here.