From 3d33f5e9639ceec3106fd08596c69029ccece6ce Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Tue, 10 Dec 2024 19:58:38 -0300 Subject: [PATCH 1/5] Simplify String List[Byte] constructor Signed-off-by: martinvuyk --- stdlib/src/collections/string.mojo | 59 ++++-------------------------- stdlib/test/python/my_module.py | 3 +- 2 files changed, 10 insertions(+), 52 deletions(-) diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index fe9746584d..cca32a97d2 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -784,68 +784,25 @@ struct String( @always_inline @implicit - fn __init__(out self, owned impl: List[UInt8, *_]): - """Construct a string from a buffer of bytes without copying the - allocated data. - - The buffer must be terminated with a null byte: - - ```mojo - var buf = List[UInt8]() - buf.append(ord('H')) - buf.append(ord('i')) - buf.append(0) - var hi = String(buf) - ``` + fn __init__(out self, impl: List[Byte, *_]): + """Construct a string from a buffer of null-terminated bytes, copying + the allocated data. Use the transfer operator `^` to avoid the copy. Args: - impl: The buffer. - """ - debug_assert( - len(impl) > 0 and impl[-1] == 0, - "expected last element of String buffer to be null terminator", - ) - # We make a backup because steal_data() will clear size and capacity. - var size = impl.size - debug_assert( - impl[size - 1] == 0, - "expected last element of String buffer to be null terminator", - ) - var capacity = impl.capacity - self._buffer = Self._buffer_type( - ptr=impl.steal_data(), length=size, capacity=capacity - ) - - @always_inline - @implicit - fn __init__(out self, impl: Self._buffer_type): - """Construct a string from a buffer of bytes, copying the allocated - data. Use the transfer operator ^ to avoid the copy. + impl: The null-terminated buffer. - The buffer must be terminated with a null byte: + Examples: ```mojo - var buf = List[UInt8]() - buf.append(ord('H')) - buf.append(ord('i')) - buf.append(0) - var hi = String(buf) + print(String(List[Byte](ord('h'), ord('i'), 0))) # hi ``` - - Args: - impl: The buffer. + . """ debug_assert( len(impl) > 0 and impl[-1] == 0, "expected last element of String buffer to be null terminator", ) - # We make a backup because steal_data() will clear size and capacity. - var size = impl.size - debug_assert( - impl[size - 1] == 0, - "expected last element of String buffer to be null terminator", - ) - self._buffer = impl + self._buffer = rebind[Self._buffer_type](impl) @always_inline fn __init__(out self): diff --git a/stdlib/test/python/my_module.py b/stdlib/test/python/my_module.py index 8147b0a382..c78c39556e 100644 --- a/stdlib/test/python/my_module.py +++ b/stdlib/test/python/my_module.py @@ -25,7 +25,8 @@ def __init__(self, bar): class AbstractPerson(ABC): @abstractmethod - def method(self): ... + def method(self): + ... def my_function(name): From 6083cde11c73cd56a777d1c1d6a59565d73de0cd Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 14:22:51 -0300 Subject: [PATCH 2/5] unformat my_module.py Signed-off-by: martinvuyk --- stdlib/test/python/my_module.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/test/python/my_module.py b/stdlib/test/python/my_module.py index c78c39556e..8147b0a382 100644 --- a/stdlib/test/python/my_module.py +++ b/stdlib/test/python/my_module.py @@ -25,8 +25,7 @@ def __init__(self, bar): class AbstractPerson(ABC): @abstractmethod - def method(self): - ... + def method(self): ... def my_function(name): From e432ae3410d697f69ca751e1afa3d62a70402677 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Thu, 12 Dec 2024 21:21:27 -0300 Subject: [PATCH 3/5] add owned as suggested by @lsh Signed-off-by: martinvuyk --- stdlib/src/collections/string.mojo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index 3cc01c1ef5..46d078db9f 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -784,7 +784,7 @@ struct String( @always_inline @implicit - fn __init__(out self, impl: List[Byte, *_]): + fn __init__(out self, owned impl: List[Byte, *_]): """Construct a string from a buffer of null-terminated bytes, copying the allocated data. Use the transfer operator `^` to avoid the copy. From 55eab187febdad8c705c815e5a4281a4630f6ac4 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Thu, 9 Jan 2025 17:17:33 -0300 Subject: [PATCH 4/5] remove _cast_hint_trivial_type Signed-off-by: martinvuyk --- stdlib/src/collections/list.mojo | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/stdlib/src/collections/list.mojo b/stdlib/src/collections/list.mojo index 1f0d42bde8..abe62df9ee 100644 --- a/stdlib/src/collections/list.mojo +++ b/stdlib/src/collections/list.mojo @@ -1000,19 +1000,6 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False]( """ return self.data - fn _cast_hint_trivial_type[ - hint_trivial_type: Bool - ](owned self) -> List[T, hint_trivial_type]: - var size = self.size - var capacity = self.capacity - - # TODO: Why doesn't `__disable_del self` work here? - var data = self.steal_data() - - return List[T, hint_trivial_type]( - ptr=data, length=size, capacity=capacity - ) - fn _clip(value: Int, start: Int, end: Int) -> Int: return max(start, min(value, end)) From cc718251f296ef899f879203fa60f7fc24b8e14a Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Thu, 9 Jan 2025 17:19:22 -0300 Subject: [PATCH 5/5] remove implicit construction Signed-off-by: martinvuyk --- stdlib/src/collections/string/string.mojo | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/src/collections/string/string.mojo b/stdlib/src/collections/string/string.mojo index ff0ec9b7c2..aaddb38e33 100644 --- a/stdlib/src/collections/string/string.mojo +++ b/stdlib/src/collections/string/string.mojo @@ -750,7 +750,6 @@ struct String( # ===------------------------------------------------------------------=== # @always_inline - @implicit fn __init__(out self, owned buffer: List[Byte, *_]): """Construct a string from a buffer of null-terminated bytes, copying the allocated data. Use the transfer operator `^` to avoid the copy.