Skip to content
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

[stdlib] Rename List.size to List._len and refactor usage of the field to use the public API #3814

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6d5e0fd
Rename List.size to List._len adding getattr and setattr for size
martinvuyk Nov 27, 2024
4419121
add length attribute overload
martinvuyk Nov 27, 2024
b6e9e36
fix detail
martinvuyk Nov 27, 2024
720e043
fix detail
martinvuyk Nov 27, 2024
0f43949
try removing all uses of list.size
martinvuyk Dec 4, 2024
2c7857d
fix uses of buf.size
martinvuyk Dec 4, 2024
1cc618e
fix pixi.toml diff
martinvuyk Dec 4, 2024
4c4e1fe
fix pixi.toml diff
martinvuyk Dec 4, 2024
6c178fb
fix details
martinvuyk Dec 4, 2024
8e88d34
fix details
martinvuyk Dec 4, 2024
5bc5722
fix details
martinvuyk Dec 4, 2024
e236c6f
Merge remote-tracking branch 'upstream/nightly' into make-list-size-p…
martinvuyk Dec 10, 2024
abc6e26
fix details
martinvuyk Dec 10, 2024
554a0b6
fix details
martinvuyk Dec 10, 2024
a3bfe40
fix details
martinvuyk Dec 10, 2024
d7bf8c3
Merge remote-tracking branch 'upstream/nightly' into make-list-size-p…
martinvuyk Dec 31, 2024
64cd704
fix after merge
martinvuyk Dec 31, 2024
f97b7e9
fix allocation
martinvuyk Dec 31, 2024
a8f2cd9
fix detail
martinvuyk Dec 31, 2024
6113bc2
fix detail
martinvuyk Dec 31, 2024
b25008d
fix bug in pathlib
martinvuyk Jan 2, 2025
1abb34b
Merge remote-tracking branch 'upstream/main' into make-list-size-private
martinvuyk Feb 2, 2025
2f32763
add changelog examples
martinvuyk Feb 2, 2025
7276562
fix change implementations to use the public API
martinvuyk Feb 2, 2025
d361412
mojo format
martinvuyk Feb 2, 2025
a9a2609
fix assert message
martinvuyk Feb 2, 2025
498f4d3
fix details
martinvuyk Feb 2, 2025
58b81a5
fix more uses of string buffer constructor
martinvuyk Feb 2, 2025
87a1a53
fix more uses of string buffer constructor
martinvuyk Feb 2, 2025
44625b9
Merge remote-tracking branch 'upstream/main' into make-list-size-private
martinvuyk Feb 25, 2025
fc70e1e
fix detail
martinvuyk Feb 25, 2025
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
36 changes: 36 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,40 @@ ctx.enqueue_function(compiled_func, grid_dim=1, block_dim=1)

### ❌ Removed

- Direct access to `List.size` has been removed. Use the public API instead.

Examples:

Extending a List:

```mojo
base_data = List[Byte](1, 2, 3)

data_list = List[Byte](4, 5, 6)
ext_data_list = base_data.copy()
ext_data_list.extend(data_list) # [1, 2, 3, 4, 5, 6]

data_span = Span(List[Byte](4, 5, 6))
ext_data_span = base_data.copy()
ext_data_span.extend(data_span) # [1, 2, 3, 4, 5, 6]

data_vec = SIMD[DType.uint8, 4](4, 5, 6, 7)
ext_data_vec_full = base_data.copy()
ext_data_vec_full.extend(data_vec) # [1, 2, 3, 4, 5, 6, 7]

ext_data_vec_partial = base_data.copy()
ext_data_vec_partial.extend(data_vec, count=3) # [1, 2, 3, 4, 5, 6]
```

Slicing and extending a list efficiently:

```mojo
base_data = List[Byte](1, 2, 3, 4, 5, 6)
n4_n5 = Span(base_data)[3:5]
extra_data = Span(List[Byte](8, 10))
end_result = List[Byte](capacity=len(n4_n5) + len(extra_data))
end_result.extend(n4_n5)
end_result.extend(extra_data) # [4, 5, 8, 10]
```

### 🛠️ Fixed
4 changes: 2 additions & 2 deletions stdlib/src/builtin/file.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ struct FileHandle:
"""Reads data from a file and sets the file handle seek position. If
size is left as the default of -1, it will read to the end of the file.
Setting size to a number larger than what's in the file will set
String.size to the total number of bytes, and read all the data.
the String length to the total number of bytes, and read all the data.

Args:
size: Requested number of bytes to read (Default: -1 = EOF).
Expand Down Expand Up @@ -273,7 +273,7 @@ struct FileHandle:
"""Reads data from a file and sets the file handle seek position. If
size is left as default of -1, it will read to the end of the file.
Setting size to a number larger than what's in the file will be handled
and set the List.size to the total number of bytes in the file.
and set the List length to the total number of bytes in the file.

Args:
size: Requested number of bytes to read (Default: -1 = EOF).
Expand Down
15 changes: 4 additions & 11 deletions stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -411,17 +411,10 @@ struct StringLiteral(
# inline the string slice constructor to work around an elaborator
# memory leak.
# return self.as_string_slice()
var string = String()
var length = self.byte_length()
var buffer = String._buffer_type()
var new_capacity = length + 1
buffer._realloc(new_capacity)
buffer.size = new_capacity
var data: UnsafePointer[UInt8] = self.unsafe_ptr()
memcpy(buffer.data, data, length)
(buffer.data + length).init_pointee_move(0)
string._buffer = buffer^
return string
var buffer = String._buffer_type(capacity=self.byte_length() + 1)
buffer.extend(self.as_bytes())
buffer.append(0)
return String(buffer=buffer^)

@no_inline
fn __repr__(self) -> String:
Expand Down
Loading