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

[docs] Update UnsafePointer APIs in pointers.ipynb #3080

Closed
wants to merge 2 commits into from
Closed
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
34 changes: 17 additions & 17 deletions docs/manual/pointers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
"metadata": {},
"outputs": [],
"source": [
"from memory.unsafe_pointer import UnsafePointer, initialize_pointee_copy, initialize_pointee_move\n",
"from memory.unsafe_pointer import UnsafePointer\n",
"\n",
"# Allocate memory to hold a value\n",
"var ptr = UnsafePointer[Int].alloc(1)\n",
"# Initialize the allocated memory\n",
"initialize_pointee_copy(ptr, 100)"
"ptr.init_pointee_copy(100)"
]
},
{
Expand Down Expand Up @@ -143,9 +143,9 @@
" the `address_of()` static method to get a pointer to an existing value. \n",
"\n",
" ```mojo\n",
" initialize_pointee_copy(ptr, value)\n",
" ptr.init_pointee_copy(value)\n",
" # or\n",
" initalize_pointee_move(ptr, value^)\n",
" ptr.init_pointee_move(value^)\n",
" # or \n",
" ptr = UnsafePointer[Int].address_of(value)\n",
" ```\n",
Expand Down Expand Up @@ -201,19 +201,19 @@
"\n",
"The `unsafe_pointer` module includes a number of free functions for working with\n",
"the `UnsafePointer` type. To initialize allocated memory, you can use the \n",
"[`initialize_pointee_copy()`](/mojo/stdlib/memory/unsafe_pointer/initialize_pointee_copy)\n",
"or [`initialize_pointee_move()`](/mojo/stdlib/memory/unsafe_pointer/initialize_pointee_move)\n",
"[`init_pointee_copy()`](/mojo/stdlib/memory/unsafe_pointer/UnsafePointer#init_pointee_copy)\n",
"or [`init_pointee_move()`](/mojo/stdlib/memory/unsafe_pointer/UnsafePointer#init_pointee_move)\n",
"functions:\n",
"\n",
"```mojo\n",
"initialize_pointee_copy(ptr, 5)\n",
"ptr.init_pointee_copy(5)\n",
"```\n",
"\n",
"To move a value into the pointer's memory location, use\n",
"`initialize_pointee_move()`:\n",
"`init_pointee_move`:\n",
"\n",
"```mojo\n",
"initialize_pointee_move(str_ptr, my_string^)\n",
"str_ptr.init_pointee_move(my_string^)\n",
"```\n",
"\n",
"Note that to move the value, you usually need to add the transfer operator\n",
Expand All @@ -222,7 +222,7 @@
"`Int`) or a newly-constructed, \"owned\" value:\n",
"\n",
"```mojo\n",
"initialize_pointee_move(str_ptr, str(\"Owned string\"))\n",
"str_ptr.init_pointee_move(str(\"Owned string\"))\n",
"```"
]
},
Expand Down Expand Up @@ -368,7 +368,7 @@
"```mojo\n",
"str_ptr = UnsafePointer[String].alloc(1)\n",
"# str_ptr[] = \"Testing\" # Undefined behavior!\n",
"initialize_pointee_move(str_ptr, \"Testing\")\n",
"str_ptr.init_pointee_move(\"Testing\")\n",
"str_ptr[] += \" pointers\" # Works now\n",
"```"
]
Expand All @@ -380,20 +380,20 @@
"### Destroying or removing values\n",
"\n",
"The \n",
"[`move_from_pointee(ptr)`](/mojo/stdlib/memory/unsafe_pointer/move_from_pointee)\n",
"[`take_pointee()`](/mojo/stdlib/memory/unsafe_pointer/UnsafePointer#take_pointee)\n",
"function moves the pointee from the memory location pointed to by `ptr`. This is\n",
"a consuming move—it invokes `__moveinit__()` on the destination value. It leaves\n",
"the memory location uninitialized.\n",
"\n",
"The [`destroy_pointee(ptr)`](/mojo/stdlib/memory/unsafe_pointer/destroy_pointee)\n",
"The [`destroy_pointee()`](/mojo/stdlib/memory/unsafe_pointer/UnsafePointer#destroy_pointee)\n",
"function calls the destructor on the pointee, and leaves the memory location\n",
"pointed to by `ptr` uninitialized. \n",
"\n",
"Both `move_from_pointee()` and `destroy_pointee()` require that the pointer is \n",
"Both `take_pointee()` and `destroy_pointee()` require that the pointer is \n",
"non-null, and the memory location contains a valid, initialized value of the \n",
"pointee's type; otherwise the function results in undefined behavior.\n",
"\n",
"The [`move_pointee(src, dst)`](/mojo/stdlib/memory/unsafe_pointer/move_pointee)\n",
"The [`move_pointee_into(self, dst)`](/mojo/stdlib/memory/unsafe_pointer/UnsafePointer#move_pointee_into)\n",
"function moves the pointee from one pointer location to another. Both pointers\n",
"must be non-null. The source location must contain a valid, initialized value of \n",
"the pointee's type, and is left uninitialized after the call. The destination \n",
Expand All @@ -413,7 +413,7 @@
"pointer frees the memory allocated by the pointer. It doesn't call the \n",
"destructors on any values stored in the memory—you need to do that explicitly\n",
"(for example, using\n",
"[`destroy_pointee()`](/mojo/stdlib/memory/unsafe_pointer/destroy_pointee) or\n",
"[`destroy_pointee()`](/mojo/stdlib/memory/unsafe_pointer/UnsafePointer#destroy_pointee) or\n",
"one of the other functions described in \n",
"[Destroying or removing values](#destroying-or-removing-values)).\n",
"\n",
Expand Down Expand Up @@ -473,7 +473,7 @@
"source": [
"float_ptr = UnsafePointer[Float64].alloc(6)\n",
"for offset in range(6):\n",
" initialize_pointee_copy(float_ptr+offset, 0.0)"
" (float_ptr+offset).init_pointee_copy(0.0)"
]
},
{
Expand Down
Loading