Make OctetString constructor public#402
Conversation
Summary of ChangesHello @jadamcrain, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an issue by making the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request aims to make the OctetString struct constructible from the FFI layer by adding an initializer. While the intent is correct, the current implementation uses FunctionReturnStruct and an iterator field, which makes it non-constructible for FFI clients. My review provides a detailed comment on how to correctly implement this by using UniversalStruct and a collection type, which will require a slightly larger refactoring.
| .add("value", byte_it.clone(), "Point value")? | ||
| .doc("Octet String point")? | ||
| .end_fields()? | ||
| .add_full_initializer("init")? |
There was a problem hiding this comment.
The goal of making OctetString constructible is correct, but this implementation will not work as intended for FFI clients due to a couple of fundamental issues.
-
FunctionReturnStructis not user-constructible: Theoctet_string_structis defined as aFunctionReturnStruct. This struct type is designed for values returned from Rust and is opaque to FFI clients, meaning they cannot construct it. To create a user-constructible struct, you should useUniversalStruct. -
Iterator as a constructor argument: The
valuefield has the typebyte_iterator. FFI clients (e.g., in C# or Java) cannot create and pass an iterator. They work with collections, like a byte array. Thevaluefield should be changed to use aCollectionHandle, such as thebyte_collectionalready defined in this file.
To properly expose a constructible OctetString, a refactoring of build_octet_string is needed. Here is a suggested implementation:
fn build_octet_string(
lib: &mut LibraryBuilder,
byte_collection: &CollectionHandle,
) -> Result<
(
UniversalStructHandle,
AbstractIteratorHandle,
AbstractIteratorHandle,
),
BindingError,
> {
let byte_it = lib.define_iterator_with_lifetime("byte_iterator", Primitive::U8)?;
let octet_string_struct_decl = lib.declare_universal_struct("octet_string")?;
let octet_string_struct = lib
.define_universal_struct(octet_string_struct_decl)?
.add("index", Primitive::U16, "Point index")?
.add("value", byte_collection.clone(), "Point value")?
.doc("Octet String point")?
.end_fields()?
.add_full_initializer("init")?
.build()?;
let octet_string_iterator =
lib.define_iterator_with_lifetime("octet_string_iterator", octet_string_struct.clone())?;
Ok((octet_string_struct, octet_string_iterator, byte_it))
}Please note that this change will require further adjustments:
- The
define()function will need to be modified to passbyte_collection. - The
SharedDefinitionsstruct will need to be updated to useUniversalStructHandlefor_octet_string.
I'm providing this broader context here as I'm limited to commenting on the changed lines.
fixes #370