-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Unsafe, optimization-oriented traits for precise sizes, orderings, and so forth #1051
Comments
FYI, my preferred solution is having an Current problems:
|
@theemathas an unsafe method seems strictly better. If it's implemented as just Also works with trait objects. Also would completely work today without all of that extra tooling. |
@gankro Are you intending to use a static method or a |
@theemathas I was mostly thinking about trusted_len; for trusted_cmp static probably makes more sense. |
Unsafe methods won't work as intended currently (unless something has changed) because you don't need to write |
I don't think a new language feature is necessary at all. The proof of a trait impl's law-abiding nature can be encoded as an abstract type that can only be conjured through an unsafe method. Here's a demonstration: mod my_trait {
use std::marker::PhantomData;
pub trait MyTrait {
/// Is there a witness to the trustworthiness of this trait?
fn trust() -> Option<TrustMyTrait<Self>> {
None
}
}
/// Proof that the trait impl of `T` is trustworthy.
pub struct TrustMyTrait<T: MyTrait + ?Sized>(PhantomData<T>);
/// Conjure a proof out of thin air (hence the unsafety).
pub unsafe fn trust_my_trait<T: MyTrait + ?Sized>() -> TrustMyTrait<T> {
TrustMyTrait(PhantomData)
}
}
use my_trait::{MyTrait, TrustMyTrait, trust_my_trait};
struct MyType;
impl MyTrait for MyType {
fn trust() -> Option<TrustMyTrait<Self>> {
// assert the impl of MyTrait for MyType is correct
unsafe { Some(trust_my_trait()) }
}
}
fn main() {
match <MyType as MyTrait>::trust() {
None => println!("MyTrait of MyType is not trustworthy :("),
Some(_) => println!("MyTrait of MyType is trustworthy :D"),
}
} |
This is an old thread, and by now Rust already has (Just for the sake of completeness: your workaround would work for 'simulated' specialization using constant-returning functions plus the optimizer, but it wouldn't work particularly well for the upcoming true (type-system based) trait specialization. This is because the existence of an impl is not enough; since |
@pythonesque I just tried it on 0.15.0 and it seems to be mandatory. I can't find any documentation to confirm whether this is intended behavior or not, though. If it is, then this proposal can be implemented without any new language features. |
Closing as implemented. |
Is there an issue/RFC for unsafe versions of |
We could sometimes write faster routines (e.g. sort, etc) if they can rely on orderings being sane, iterators having correct lengths. But this makes implementing that ordering/iterator code unsafe. Currently the traits do not require unsafe code.
This issue is a request to extend the set of traits with unsafe variants that let users opt in to these faster implementations. These traits might be generated by derive etc.
RFCs and issues along these lines:
The text was updated successfully, but these errors were encountered: