Skip to content

Commit

Permalink
[Bug] Fix copy_from() of StructField (taichi-dev#7294)
Browse files Browse the repository at this point in the history
Issue: fix taichi-dev#7290
  • Loading branch information
strongoier authored and quadpixels committed May 13, 2023
1 parent 58efc6f commit ede3b62
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/taichi/lang/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def copy_from(self, other):
assert isinstance(other, Field)
assert set(self.keys) == set(other.keys)
for k in self.keys:
self.field_dict[k].copy_from(other[k])
self.field_dict[k].copy_from(other.get_member_field(k))

@python_scope
def fill(self, val):
Expand Down
29 changes: 29 additions & 0 deletions tests/python/test_copy_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,32 @@ def test_scalar():
assert y[0] == 1
assert y[1] == 0
assert y[2] == 3


@test_utils.test()
def test_struct():
@ti.dataclass
class C:
i: int
f: float

n = 16

x = C.field(shape=n)
y = C.field(shape=n)

x[1].i = 2
x[2].i = 4

y[0].f = 1.0
y[2].i = 3

x.copy_from(y)

assert x[0].f == 1.0
assert x[1].i == 0
assert x[2].i == 3

assert y[0].f == 1.0
assert y[1].i == 0
assert y[2].i == 3

0 comments on commit ede3b62

Please sign in to comment.