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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ fn FixedArray::set_utf16le_char(Self[Byte], Int, Char) -> Int
fn FixedArray::set_utf8_char(Self[Byte], Int, Char) -> Int
fn[A] FixedArray::unsafe_blit(Self[A], Int, Self[A], Int, Int) -> Unit
fn[T] FixedArray::unsafe_get(Self[T], Int) -> T
fn FixedArray::unsafe_reinterpret_as_bytes(Self[Byte]) -> Bytes
fn[T] FixedArray::unsafe_set(Self[T], Int, T) -> Unit

fn Bytes::copy(Bytes) -> Bytes
Expand Down
13 changes: 10 additions & 3 deletions builtin/bytes.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@

///|
/// Reinterpret the byte sequence as Bytes.
fn FixedArray::unsafe_to_bytes(self : FixedArray[Byte]) -> Bytes = "%identity"
///
/// Notice that this will make the `Bytes` object to be a view of the original
/// byte sequence, so any modification to the original byte sequence will be
/// reflected in the `Bytes` object.
#internal(unsafe, "Creating mutable Bytes")
pub fn FixedArray::unsafe_reinterpret_as_bytes(
self : FixedArray[Byte]
) -> Bytes = "%identity"

///|
/// Creates a new byte sequence of the specified length, where each byte is
Expand Down Expand Up @@ -44,7 +51,7 @@ pub fn Bytes::makei(length : Int, value : (Int) -> Byte) -> Bytes {
for i in 1..<length {
arr[i] = value(i)
}
FixedArray::unsafe_to_bytes(arr)
FixedArray::unsafe_reinterpret_as_bytes(arr)
}

///|
Expand All @@ -68,7 +75,7 @@ pub fn Bytes::makei(length : Int, value : (Int) -> Byte) -> Bytes {
pub fn Bytes::of_string(str : String) -> Bytes {
FixedArray::make(str.length() * 2, Byte::default())
..blit_from_string(0, str, 0, str.length())
.unsafe_to_bytes()
.unsafe_reinterpret_as_bytes()
}

///|
Expand Down
2 changes: 1 addition & 1 deletion builtin/string.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fn unsafe_substring(str : String, start : Int, end : Int) -> String {
let len = end - start
let bytes = FixedArray::make(len * 2, Byte::default())
bytes.blit_from_string(0, str, start, len)
bytes.unsafe_to_bytes().to_unchecked_string()
bytes.unsafe_reinterpret_as_bytes().to_unchecked_string()
}

///|
Expand Down
8 changes: 6 additions & 2 deletions builtin/stringbuilder_buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,18 @@ pub impl Logger for StringBuilder with write_substring(
///|
/// Returns the current content of the StringBuilder as a string.
pub fn StringBuilder::to_string(self : StringBuilder) -> String {
self.data.unsafe_to_bytes().to_unchecked_string(offset=0, length=self.len)
self.data
.unsafe_reinterpret_as_bytes()
.to_unchecked_string(offset=0, length=self.len)
}

///|
/// TODO: improve perf
pub impl Show for StringBuilder with output(self, logger) {
logger.write_string(
self.data.unsafe_to_bytes().to_unchecked_string(offset=0, length=self.len),
self.data
.unsafe_reinterpret_as_bytes()
.to_unchecked_string(offset=0, length=self.len),
)
}

Expand Down
0