Hi there,
I created the dart_sys
crate about four months ago. This exposes raw bindings to the dart native extensions api.
I've since spent about two months exploring dart, and playing around with it and I now feel comfortable using it.
My current plan is to create safe and easy to use bindings to the API for Rust, but I am having some issues deciding on a design for a part. Specifically, the Dart_Handle
.
The Dart_Handle
is used as a handle to a Dart object on the heap, and is updated automatically to refer to the correct object. About half of the API interacts with Dart_Handle
s, so this is not something I want to leave as is.
The thing is, is that the Dart documentation claims that I will not get an invalid handle to something, and it makes it seem that if I give it an invalid handle, it will try to deal with it itself, and return a handle to an exception.
But I am not sure of the validity of their functions, and in fact, I'm not sure that I could create the api in a way that it's impossible to put it into an invalid state.
The issue is that I am now unsure of whether I should make my constructor unsafe
, or the individual functions unsafe
. Since a rust user is supposed to never be able to trigger UB through a non-unsafe function, one could say that making the constructor unsafe and the rest of the functions safe would break that rule, but that would be unwieldy, since my safe api has suddenly become a struct with a safe constructor and 100 unsafe functions.
IE, it's this:
// Unverified since it may be an exception handle.
struct UnverifiedDartHandle {
handle: ffi::Dart_Handle // The raw handle
}
impl UnverifiedDartHandle {
pub fn new(ffi::Dart_Handle) -> Self { /**/ }
pub unsafe fn is_error(&self) -> bool { /**/ }
}
versus this:
// Unverified since it may be an exception handle.
struct UnverifiedDartHandle {
handle: ffi::Dart_Handle // The raw handle
}
impl UnverifiedDartHandle {
pub unsafe fn new(ffi::Dart_Handle) -> Self { /**/ }
pub fn is_error(&self) -> bool { /**/ }
}
I am wondering what the recommended course of action would be.
Thank you, and have a nice day!