generated from eclipse-score/module_template
-
Notifications
You must be signed in to change notification settings - Fork 4
ABI-compatible Option and Result types #41
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
Merged
pawelrutkaq
merged 2 commits into
eclipse-score:main
from
qorix-group:adwi_option-result
Dec 18, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| # ******************************************************************************* | ||
| # Copyright (c) 2025 Contributors to the Eclipse Foundation | ||
| # | ||
| # See the NOTICE file(s) distributed with this work for additional | ||
| # information regarding copyright ownership. | ||
| # | ||
| # This program and the accompanying materials are made available under the | ||
| # terms of the Apache License Version 2.0 which is available at | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # ******************************************************************************* | ||
|
|
||
| import http.cookiejar | ||
| import json | ||
| import netrc | ||
| import os | ||
| import sys | ||
| import urllib.parse | ||
| import urllib.request | ||
|
|
||
|
|
||
| def eprint(*args, **kwargs): | ||
| print(*args, file=sys.stderr, **kwargs) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| data = json.load(sys.stdin) | ||
|
|
||
| if "qnx.com" not in data["uri"]: | ||
| eprint("Unsupported domain") | ||
| sys.exit(1) | ||
|
|
||
| if "SCORE_QNX_USER" in os.environ and "SCORE_QNX_PASSWORD" in os.environ: | ||
| login = os.environ["SCORE_QNX_USER"] | ||
| password = os.environ["SCORE_QNX_PASSWORD"] | ||
| else: | ||
| try: | ||
| nrc = netrc.netrc() | ||
| auth = nrc.authenticators("qnx.com") | ||
| if auth: | ||
| login, _, password = auth | ||
| else: | ||
| raise Exception("No credential found for QNX") | ||
| except Exception as excp: | ||
| eprint(excp) | ||
| eprint("Failed getting credentials from .netrc") | ||
| sys.exit(1) | ||
|
|
||
| data = urllib.parse.urlencode( | ||
| {"userlogin": login, "password": password, "UseCookie": "1"} | ||
| ) | ||
| data = data.encode("ascii") | ||
|
|
||
| cookie_jar = http.cookiejar.CookieJar() | ||
| cookie_processor = urllib.request.HTTPCookieProcessor(cookie_jar) | ||
| opener = urllib.request.build_opener(cookie_processor) | ||
| urllib.request.install_opener(opener) | ||
|
|
||
| r = urllib.request.urlopen("https://www.qnx.com/account/login.html", data) | ||
| if r.status != 200: | ||
| eprint("Failed to login to QNX") | ||
| sys.exit(1) | ||
|
|
||
| cookies = {c.name: c.value for c in list(cookie_jar)} | ||
| if not "myQNX" in cookies: | ||
| eprint("Failed to get myQNX cookie from login page") | ||
| sys.exit(1) | ||
|
|
||
| myQNX = cookies["myQNX"] | ||
| print( | ||
| json.dumps( | ||
| { | ||
| "headers": { | ||
| "Cookie": [f"myQNX={myQNX}"], | ||
| } | ||
| } | ||
| ) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,280 @@ | ||
| // ******************************************************************************* | ||
| // Copyright (c) 2025 Contributors to the Eclipse Foundation | ||
| // | ||
| // See the NOTICE file(s) distributed with this work for additional | ||
| // information regarding copyright ownership. | ||
| // | ||
| // This program and the accompanying materials are made available under the | ||
| // terms of the Apache License Version 2.0 which is available at | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // ******************************************************************************* | ||
|
|
||
| use core::cmp; | ||
| use core::fmt; | ||
| use core::mem::MaybeUninit; | ||
|
|
||
| /// An optional value, similar to [`Option`] in the Rust standard library. | ||
| /// | ||
| /// This data structure has a stable, well-defined memory layout and satisfies the requirements for | ||
| /// [ABI-compatible types](https://eclipse-score.github.io/score/main/features/communication/abi_compatible_data_types/index.html). | ||
| /// Unlike the built-in [`Option`] or other enum-based types, this data structure will **not** be affected by | ||
| /// [*null-pointer optimization*](https://doc.rust-lang.org/core/option/index.html#representation), | ||
| /// where invalid byte representations of `T` are used to encode the `None` case. | ||
| /// | ||
| /// **Note:** When encoding an instance of this type in other programming languages, | ||
| /// the `is_some` field must only be assigned the binary values `0x00` and `0x01`; | ||
| /// everything else results in *Undefined Behavior*. | ||
| #[derive(Clone, Copy)] | ||
| #[repr(C)] | ||
| pub struct InlineOption<T: Copy> { | ||
| /// This is valid/initialized if and only if `is_some` is true. | ||
| value: MaybeUninit<T>, | ||
| is_some: bool, | ||
| } | ||
|
|
||
| impl<T: Copy> InlineOption<T> { | ||
| /// Creates a new instance populated by `value`. | ||
| pub const fn some(value: T) -> Self { | ||
| Self { | ||
| value: MaybeUninit::new(value), | ||
| is_some: true, | ||
| } | ||
| } | ||
|
|
||
| /// Creates an empty instance. | ||
| pub const fn none() -> Self { | ||
| Self { | ||
| value: MaybeUninit::uninit(), | ||
| is_some: false, | ||
| } | ||
| } | ||
|
|
||
| /// Creates a new instance from a standard [`Option`]. | ||
| pub fn from_option(option: Option<T>) -> Self { | ||
| if let Some(value) = option { | ||
| Self::some(value) | ||
| } else { | ||
| Self::none() | ||
| } | ||
| } | ||
|
|
||
| /// Converts this instance into a standard [`Option`]. | ||
| pub const fn into_option(self) -> Option<T> { | ||
| if self.is_some { | ||
| // SAFETY: `is_some` is true, so `value` must be valid as per its invariant. | ||
| Some(unsafe { self.value.assume_init() }) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| /// Returns an optional reference to the contained value. | ||
| pub const fn as_ref(&self) -> Option<&T> { | ||
| if self.is_some { | ||
| // SAFETY: `is_some` is true, so `value` must be valid as per its invariant. | ||
| Some(unsafe { self.value.assume_init_ref() }) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| /// Returns an optional mutable reference to the contained value. | ||
| pub const fn as_mut(&mut self) -> Option<&mut T> { | ||
| if self.is_some { | ||
| // SAFETY: `is_some` is true, so `value` must be valid as per its invariant. | ||
| Some(unsafe { self.value.assume_init_mut() }) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T: Copy> Default for InlineOption<T> { | ||
| fn default() -> Self { | ||
| Self { | ||
| value: MaybeUninit::uninit(), | ||
| is_some: false, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T: Copy> From<T> for InlineOption<T> { | ||
| fn from(value: T) -> Self { | ||
| Self { | ||
| value: MaybeUninit::new(value), | ||
| is_some: true, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T: Copy> From<Option<T>> for InlineOption<T> { | ||
| fn from(option: Option<T>) -> Self { | ||
| Self::from_option(option) | ||
| } | ||
| } | ||
|
|
||
| impl<T: Copy> From<InlineOption<T>> for Option<T> { | ||
| fn from(option: InlineOption<T>) -> Self { | ||
| option.into_option() | ||
| } | ||
| } | ||
|
|
||
| impl<T: PartialEq + Copy> PartialEq for InlineOption<T> { | ||
| fn eq(&self, other: &Self) -> bool { | ||
| match (self.as_ref(), other.as_ref()) { | ||
| (Some(a), Some(b)) => a.eq(b), | ||
| (Some(_), None) => false, | ||
| (None, Some(_)) => false, | ||
| (None, None) => true, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T: Eq + Copy> Eq for InlineOption<T> {} | ||
|
|
||
| impl<T: PartialOrd + Copy> PartialOrd for InlineOption<T> { | ||
| fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { | ||
| match (self.as_ref(), other.as_ref()) { | ||
| (Some(a), Some(b)) => a.partial_cmp(b), | ||
| (Some(_), None) => Some(cmp::Ordering::Greater), | ||
| (None, Some(_)) => Some(cmp::Ordering::Less), | ||
| (None, None) => Some(cmp::Ordering::Equal), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T: Ord + Copy> Ord for InlineOption<T> { | ||
| fn cmp(&self, other: &Self) -> cmp::Ordering { | ||
| match (self.as_ref(), other.as_ref()) { | ||
| (Some(a), Some(b)) => a.cmp(b), | ||
| (Some(_), None) => cmp::Ordering::Greater, | ||
| (None, Some(_)) => cmp::Ordering::Less, | ||
| (None, None) => cmp::Ordering::Equal, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T: Copy> fmt::Display for InlineOption<T> | ||
| where | ||
| for<'a> Option<&'a T>: fmt::Display, | ||
| { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt::Display::fmt(&self.as_ref(), f) | ||
| } | ||
| } | ||
|
|
||
| impl<T: Copy> fmt::Debug for InlineOption<T> | ||
| where | ||
| for<'a> Option<&'a T>: fmt::Debug, | ||
| { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt::Debug::fmt(&self.as_ref(), f) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn from_and_into() { | ||
| let original_some = Some(0x1234567890abcdef_u64); | ||
| let inline_some = InlineOption::from_option(original_some); | ||
| assert_eq!(inline_some.into_option(), original_some); | ||
|
|
||
| let original_none: Option<u64> = None; | ||
| let inline_none = InlineOption::from_option(original_none); | ||
| assert_eq!(inline_none.into_option(), original_none); | ||
| } | ||
|
|
||
| #[test] | ||
| fn as_ref() { | ||
| let original_some = Some(0x1234567890abcdef_u64); | ||
| let inline_some = InlineOption::from_option(original_some); | ||
| assert_eq!(inline_some.as_ref(), original_some.as_ref()); | ||
|
|
||
| let original_none: Option<u64> = None; | ||
| let inline_none = InlineOption::from_option(original_none); | ||
| assert_eq!(inline_none.as_ref(), original_none.as_ref()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn as_mut() { | ||
| let mut original_some = Some(0x1234567890abcdef_u64); | ||
| let mut inline_some = InlineOption::from_option(original_some); | ||
| assert_eq!(inline_some.as_mut(), original_some.as_mut()); | ||
| *original_some.as_mut().unwrap() = 5; | ||
| *inline_some.as_mut().unwrap() = 5; | ||
| assert_eq!(inline_some.as_mut(), original_some.as_mut()); | ||
|
|
||
| let mut original_none: Option<u64> = None; | ||
| let mut inline_none = InlineOption::from_option(original_none); | ||
| assert_eq!(inline_none.as_mut(), original_none.as_mut()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn eq() { | ||
| #[track_caller] | ||
| fn check(lhs: &InlineOption<i32>, rhs: &InlineOption<i32>) { | ||
| // Check that InlineOption behaves exactly like Option, since Option's implementation is assumed to be correct. | ||
| assert_eq!(lhs.eq(rhs), lhs.as_ref().eq(&rhs.as_ref())); | ||
| assert_eq!(lhs.ne(rhs), lhs.as_ref().ne(&rhs.as_ref())); | ||
| } | ||
|
|
||
| let some_5 = InlineOption::some(5); | ||
| let some_6 = InlineOption::some(6); | ||
| let none = InlineOption::none(); | ||
| // Check all combinations | ||
| for lhs in &[some_5, some_6, none] { | ||
| for rhs in &[some_5, some_6, none] { | ||
| check(lhs, rhs); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn partial_cmp() { | ||
| #[track_caller] | ||
| fn check(lhs: &InlineOption<i32>, rhs: &InlineOption<i32>) { | ||
| // Check that InlineOption behaves exactly like Option, since Option's implementation is assumed to be correct. | ||
| assert_eq!(lhs.partial_cmp(rhs), lhs.as_ref().partial_cmp(&rhs.as_ref())); | ||
| assert_eq!(lhs < rhs, lhs.as_ref() < rhs.as_ref()); | ||
| assert_eq!(lhs <= rhs, lhs.as_ref() <= rhs.as_ref()); | ||
| assert_eq!(lhs > rhs, lhs.as_ref() > rhs.as_ref()); | ||
| assert_eq!(lhs >= rhs, lhs.as_ref() >= rhs.as_ref()); | ||
| } | ||
|
|
||
| let some_5 = InlineOption::some(5); | ||
| let some_6 = InlineOption::some(6); | ||
| let none = InlineOption::none(); | ||
| // Check all combinations | ||
| for lhs in &[some_5, some_6, none] { | ||
| for rhs in &[some_5, some_6, none] { | ||
| check(lhs, rhs); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn cmp() { | ||
| #[track_caller] | ||
| fn check(lhs: &InlineOption<i32>, rhs: &InlineOption<i32>) { | ||
| // Check that InlineOption behaves exactly like Option, since Option's implementation is assumed to be correct. | ||
| assert_eq!(lhs.cmp(rhs), lhs.as_ref().cmp(&rhs.as_ref())); | ||
| assert_eq!(lhs.max(rhs).as_ref(), lhs.as_ref().max(rhs.as_ref())); | ||
| assert_eq!(lhs.min(rhs).as_ref(), lhs.as_ref().min(rhs.as_ref())); | ||
| } | ||
|
|
||
| let some_5 = InlineOption::some(5); | ||
| let some_6 = InlineOption::some(6); | ||
| let none = InlineOption::none(); | ||
| // Check all combinations | ||
| for lhs in &[some_5, some_6, none] { | ||
| for rhs in &[some_5, some_6, none] { | ||
| check(lhs, rhs); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.