Skip to content

Commit

Permalink
[External] [stdlib] Rename List.size to List._len and refactor us…
Browse files Browse the repository at this point in the history
…age of the field to use the public API (#56692)

[External] [stdlib] Rename `List.size` to `List._len`

Make the data member `size` of `List` private by prefixing with an `_`.
Rename it to `len` while we're here.  Adjust call sites to use public APIs
where possible.

Details:
- Part of #3511
- To prepare for any future implementation details than can change under
the hood for `List`.

Co-authored-by: martinvuyk <110240700+martinvuyk@users.noreply.github.com>
Closes #3814
MODULAR_ORIG_COMMIT_REV_ID: c2ac41d4c543b01619814ce0cd58fd8ba15ac9e9
  • Loading branch information
martinvuyk authored and modularbot committed Feb 27, 2025
1 parent e543f24 commit 5f2570f
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 157 deletions.
36 changes: 36 additions & 0 deletions mojo/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,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 mojo/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 mojo/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

0 comments on commit 5f2570f

Please sign in to comment.