Skip to content

Treat Option<T> as Vec<T>#101

Merged
ihciah merged 3 commits intoihciah:masterfrom
Danil-Grigorev:add-option-type-handling
Feb 3, 2026
Merged

Treat Option<T> as Vec<T>#101
ihciah merged 3 commits intoihciah:masterfrom
Danil-Grigorev:add-option-type-handling

Conversation

@Danil-Grigorev
Copy link
Copy Markdown
Contributor

Naive implementation of the Option handling, as a regular slice of length either 0 or 1. This implementation allows to pass more complex structures with optional values, where golang treats such as a []T type objects.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements support for Option<T> types by treating them as slices of length 0 or 1, similar to how Vec<T> is handled. This allows passing optional values to Go code where they're represented as []T types.

  • Adds Option<T> pattern matching alongside Vec<T> in type processing
  • Implements ToRef and FromRef traits for Option<T> using slice-based conversion
  • Updates macro and common type handling to recognize Option as a list type

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
rust2go-macro/src/lib.rs Extends pattern matching to include "Option" alongside "Vec" for ListRef generation
rust2go-convert/src/convert.rs Implements ToRef and FromRef traits for Option using slice-based conversion logic
rust2go-common/src/common.rs Updates type processing to treat Option as a list type in two locations

Comment thread rust2go-convert/src/convert.rs
Comment thread rust2go-convert/src/convert.rs
Comment thread rust2go-convert/src/convert.rs Outdated
Copy link
Copy Markdown
Collaborator

@lirenjie95 lirenjie95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Danil-Grigorev , thank you for your kind contribution. Can you please resolve the lint issue and add related tests as well?

Signed-off-by: Danil-Grigorev <danil.grigorev@suse.com>
@Danil-Grigorev Danil-Grigorev force-pushed the add-option-type-handling branch 2 times, most recently from 878b59c to 762aa0c Compare August 1, 2025 08:08
@Danil-Grigorev Danil-Grigorev force-pushed the add-option-type-handling branch from 762aa0c to 7d267b4 Compare August 1, 2025 08:09
Copy link
Copy Markdown
Collaborator

@lirenjie95 lirenjie95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Owner

@ihciah ihciah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small regression: DataView::new intentionally sets ptr = null when len == 0, but Vec::to_ref and Option::to_ref now overwrite data.0.ptr unconditionally for MemType::Complex, which re‑introduces a non‑null (dangling) pointer for empty lists and changes nil/empty semantics on the Go side. Suggest a tiny follow‑up patch to only overwrite when len > 0. I’m OK to merge as‑is and fix right after.

@ihciah ihciah merged commit 7b0d51c into ihciah:master Feb 3, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants