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

[Feature Request] Merge init_pointee_move and move_pointee_into. #3038

Closed
1 task done
soraros opened this issue Jun 13, 2024 · 5 comments
Closed
1 task done

[Feature Request] Merge init_pointee_move and move_pointee_into. #3038

soraros opened this issue Jun 13, 2024 · 5 comments
Labels
enhancement New feature or request mojo-repo Tag all issues with this label

Comments

@soraros
Copy link
Contributor

soraros commented Jun 13, 2024

Review Mojo's priorities

What is your request?

As title.

What is your motivation for this change?

If I understand correctly, we don't need both.

Any other details?

N/A

@soraros soraros added enhancement New feature or request mojo-repo Tag all issues with this label labels Jun 13, 2024
@bethebunny
Copy link
Contributor

init_pointee_move treats the input as "uninit", which means it won't call the destructor on whatever data is in the pointer already, whereas move_pointee_into will call the destructor of the old data before the move.

@soraros
Copy link
Contributor Author

soraros commented Jun 21, 2024

@bethebunny So s.move_pointee_into(t) is t.init_pointee_move(s.take_pointee())?

@bethebunny
Copy link
Contributor

s.move_pointee_into(t) should be equivalent to

_ = t.take_pointee()
t.init_pointee_move(s.take_pointee())

@soraros
Copy link
Contributor Author

soraros commented Jun 21, 2024

But the doctoring of move_pointee_into says that dst is assumed to be uninitialised, so it doesn't run the destructor.

If it calls the destructor of the old data, why can't we write

t[] = s[]^

@soraros
Copy link
Contributor Author

soraros commented Jun 22, 2024

The really difference seems to be that t.init_pointee_move(s.take_pointee()) calls moveinit twice.

struct S:
  var data: Float64

  fn __init__(inout self, data: Float64):
    self.data = data

  fn __copyinit__(inout self, other: S):
    print("  copyinit", other.data)
    self.data = other.data

  fn __moveinit__(inout self, owned other: S):
    print("  moveinit", other.data)
    self.data = other.data

  fn __del__(owned self):
    print("  del", self.data)

fn move_pointee_into():
  print("move_pointee_into:")
  var v = S(10)
  var t = UnsafePointer[S].alloc(1)
  var s = UnsafePointer.address_of(v)
  s.move_pointee_into(t)
  print()
  _ = v

fn init_pointee_move():
  print("init_pointee_move:")
  var v = S(20)
  var t = UnsafePointer[S].alloc(1)
  var s = UnsafePointer.address_of(v)
  t.init_pointee_move(s.take_pointee())
  print()
  _ = v

fn main():
  move_pointee_into()
  print()
  init_pointee_move()

Result:

move_pointee_into:
  moveinit 10.0

  del 10.0

init_pointee_move:
  moveinit 20.0
  moveinit 20.0

  del 20.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

2 participants