ABI-compatible Option and Result types#41
Conversation
License Check Results🚀 The license check job ran with the Bazel command: bazel run //:license-checkStatus: Click to expand output |
|
The created documentation from the pull request is available at: docu-html |
src/containers/inline/option.rs
Outdated
| /// Returns an optional reference to the contained value. | ||
| pub const fn as_ref(&self) -> Option<&T> { | ||
| if self.is_some { | ||
| Some(unsafe { &*self.value.as_ptr() }) |
There was a problem hiding this comment.
safet why this is safe to derefence maybeuninit ptr (just that we checked above is_some)
There was a problem hiding this comment.
other unsafe places too
There was a problem hiding this comment.
You're right. There's a (clippy?) lint that enforces "SAFETY" comments, maybe we should enable that.
| #[repr(C)] | ||
| pub struct InlineOption<T: Copy> { | ||
| value: MaybeUninit<T>, | ||
| is_some: bool, |
There was a problem hiding this comment.
Shall we specify this as reprC enum enforced to u8 with states instead realing on bool? Or use u8 and do checking on value before using it ?
There was a problem hiding this comment.
This would be possible, and the ABI tool actually translates enums from the description language into repr(u8) enums in Rust. Unfortunately, the ABI types feature request dictates this memory layout; we could change the feature request, but I don't think we'd gain any benefits from this.
src/containers/inline/result.rs
Outdated
| /// Returns `true` if and only if this instance represents failure. | ||
| pub fn is_err(&self) -> bool { | ||
| !self.is_ok | ||
| } |
There was a problem hiding this comment.
wonder if we shall expose this or rather fallback this all into buildin types via as_ref and say this container have no direct Result API ?
There was a problem hiding this comment.
I guess there's no big advantage to having these methods, except that they generate less intermediate code which has to be optimized away by the compiler. I can remove them if you want.
There was a problem hiding this comment.
lets remove then for consistent usage ? and why we dont use deref?
There was a problem hiding this comment.
Done, as discussed
ab6489b to
772b4e2
Compare
772b4e2 to
3275d90
Compare
3275d90 to
46494a6
Compare
8095d3e to
acba861
Compare
Notes for Reviewer
The types are called
InlineOptionandInlineResultto be consistent with the other ABI-compatible data structures in the library. Like them, these types are intended to be imported in themw_comlibrary, where the zero-copy traits areimpld for the wrappers.Pre-Review Checklist for the PR Author
Checklist for the PR Reviewer
Post-review Checklist for the PR Author
References
Closes #40