Skip to content

Commit

Permalink
Fix as_bytes() documentation
Browse files Browse the repository at this point in the history
Signed-off-by: martinvuyk <[email protected]>
  • Loading branch information
martinvuyk committed Dec 11, 2024
1 parent 2ace785 commit ec08bae
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
14 changes: 7 additions & 7 deletions stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -526,28 +526,28 @@ struct StringLiteral(

@always_inline
fn as_bytes(self) -> Span[Byte, StaticConstantOrigin]:
"""
Returns a contiguous Span of the bytes owned by this string.
"""Returns a contiguous slice of bytes.
Returns:
A contiguous slice pointing to the bytes owned by this string.
"""
A contiguous slice pointing to bytes.
Notes:
This does not include the trailing null terminator.
"""
return Span[Byte, StaticConstantOrigin](
ptr=self.unsafe_ptr(), length=self.byte_length()
)

@always_inline
fn as_bytes(ref self) -> Span[Byte, __origin_of(self)]:
"""Returns a contiguous slice of the bytes owned by this string.
"""Returns a contiguous slice of bytes.
Returns:
A contiguous slice pointing to the bytes owned by this string.
A contiguous slice pointing to bytes.
Notes:
This does not include the trailing null terminator.
"""
# Does NOT include the NUL terminator.
return Span[Byte, __origin_of(self)](
ptr=self.unsafe_ptr(), length=self.byte_length()
)
Expand Down
8 changes: 3 additions & 5 deletions stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1614,18 +1614,16 @@ struct String(

@always_inline
fn as_bytes(ref self) -> Span[Byte, __origin_of(self)]:
"""Returns a contiguous slice of the bytes owned by this string.
"""Returns a contiguous slice of bytes.
Returns:
A contiguous slice pointing to the bytes owned by this string.
A contiguous slice pointing to bytes.
Notes:
This does not include the trailing null terminator.
"""

# Does NOT include the NUL terminator.
return Span[Byte, __origin_of(self)](
ptr=self._buffer.unsafe_ptr(), length=self.byte_length()
ptr=self.unsafe_ptr(), length=self.byte_length()
)

@always_inline
Expand Down
8 changes: 3 additions & 5 deletions stdlib/src/memory/span.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ from memory import Pointer, UnsafePointer


trait AsBytes:
"""
The `AsBytes` trait denotes a type that can be returned as a immutable byte
span.
"""The `AsBytes` trait denotes a type that can be returned as a byte span.
"""

fn as_bytes(ref self) -> Span[Byte, __origin_of(self)]:
"""Returns a contiguous slice of the bytes owned by this string.
"""Returns a contiguous slice of bytes.
Returns:
A contiguous slice pointing to the bytes owned by this string.
A contiguous slice pointing to bytes.
Notes:
This does not include the trailing null terminator.
Expand Down
7 changes: 5 additions & 2 deletions stdlib/src/utils/string_slice.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -750,10 +750,13 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]](

@always_inline
fn as_bytes(self) -> Span[Byte, origin]:
"""Get the sequence of encoded bytes of the underlying string.
"""Returns a contiguous slice of bytes.
Returns:
A slice containing the underlying sequence of encoded bytes.
A contiguous slice pointing to bytes.
Notes:
This does not include the trailing null terminator.
"""
return self._slice

Expand Down

0 comments on commit ec08bae

Please sign in to comment.