8000 feat(bytes): expose unsafe cast by peter-jerry-ye · Pull Request #2301 · moonbitlang/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(bytes): expose unsafe cast #2301

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
merged 1 commit into from
Jun 22, 2025
Merged

feat(bytes): expose unsafe cast #2301

merged 1 commit into from
Jun 22, 2025

Conversation

peter-jerry-ye
Copy link
Collaborator

Closes #1893

Copy link
peter-jerry-ye-code-review bot commented Jun 20, 2025
The new function name `unsafe_reinterpret_as_bytes` is more descriptive about its behavior than `unsafe_to_bytes`

Category
Maintainability
Code Snippet
pub fn FixedArray::unsafe_reinterpret_as_bytes(self : FixedArray[Byte]) -> Bytes = "%identity"
Recommendation
The rename is good. Consider also adding a safe alternative that creates a copy instead of a view
Reasoning
The new name better indicates that this is a reinterpretation without copying, which is important for safety. Having both unsafe view and safe copy versions would give users more flexibility.

Added #internal attribute with unsafe warning about mutable Bytes creation

Category
Correctness
Code Snippet
#internal(unsafe, "Creating mutable Bytes")
Recommendation
The warning is appropriate. Consider adding examples in the documentation showing safe vs unsafe usage patterns
Reasoning
Since the function creates a mutable view that can be modified from multiple places, clear documentation about the risks is important for preventing bugs

Long lines in StringBuilder::to_string implementation could be formatted better

Category
Maintainability
Code Snippet
self.data.unsafe_reinterpret_as_bytes().to_unchecked_string(offset=0, length=self.len)
Recommendation
Format with line breaks before method calls:

self.data
  .unsafe_reinterpret_as_bytes()
  .to_unchecked_string(offset=0, length=self.len)```
**Reasoning**
Consistent formatting with line breaks improves readability, especially for chained method calls

</details>

@coveralls
Copy link
Collaborator
coveralls commented Jun 20, 2025

Pull Request Test Coverage Report for Build 7413

Details

  • 7 of 7 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.003%) to 89.889%

Totals Coverage Status
Change from base Build 7409: 0.003%
Covered Lines: 8508
Relevant Lines: 9465

💛 - Coveralls

@peter-jerry-ye peter-jerry-ye force-pushed the zihang/unsafe-to-bytes branch from bd2c8f9 to 82aeed1 Compare June 20, 2025 10:23
@bobzhang bobzhang merged commit ef88eca into main Jun 22, 2025
12 checks passed
@bobzhang bobzhang deleted the zihang/unsafe-to-bytes branch June 22, 2025 08:10
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.

Bytes Method
3 participants
0