Skip to content

Commit b6ebd3c

Browse files
pytorchbotswolchok
andauthored
Fix missing move and bare new in pytree from_str_internal (#6803)
* Use std::variant to implement pytree Key Pull Request resolved: #6701 Key was a struct that should've been a union; std::variant makes using a union much easier. ghstack-source-id: 253128071 @exported-using-ghexport Differential Revision: [D65575184](https://our.internmc.facebook.com/intern/diff/D65575184/) * Fix missing move and bare new in pytree from_str_internal Just a couple minor fixes. Differential Revision: [D65576543](https://our.internmc.facebook.com/intern/diff/D65576543/) ghstack-source-id: 253128072 Pull Request resolved: #6783 --------- Co-authored-by: Scott Wolchok <[email protected]>
1 parent 15b1f39 commit b6ebd3c

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

extension/pytree/pytree.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ struct ContainerHandle {
139139

140140
/*implicit*/ ContainerHandle(container_type* c) : handle(c) {}
141141

142+
/*implicit*/ ContainerHandle(std::unique_ptr<container_type> c)
143+
: handle(std::move(c)) {}
144+
142145
void set_leaf(leaf_type* leaf) {
143146
pytree_assert(handle->kind == Kind::Leaf);
144147
handle->leaf = leaf;
@@ -486,10 +489,10 @@ TreeSpec<Aux> from_str_internal(
486489
read_idx++;
487490
auto layout = read_node_layout(spec, read_idx);
488491
const auto size = layout.size();
489-
auto c = new TreeSpecContainer<Aux>(kind, size);
492+
auto c = std::make_unique<TreeSpecContainer<Aux>>(kind, size);
490493

491494
if (Kind::Custom == kind) {
492-
c->custom_type = custom_type;
495+
c->custom_type = std::move(custom_type);
493496
}
494497

495498
size_t child_idx = 0;
@@ -509,14 +512,14 @@ TreeSpec<Aux> from_str_internal(
509512
read_idx++;
510513
}
511514
c->leaves_num = leaves_offset;
512-
return c;
515+
return TreeSpec<Aux>(std::move(c));
513516
}
514517

515518
case Config::kDict: {
516519
read_idx++;
517520
auto layout = read_node_layout(spec, read_idx);
518521
const auto size = layout.size();
519-
auto c = new TreeSpecContainer<Aux>(Kind::Dict, size);
522+
auto c = std::make_unique<TreeSpecContainer<Aux>>(Kind::Dict, size);
520523

521524
size_t child_idx = 0;
522525
size_t leaves_offset = 0;
@@ -549,7 +552,7 @@ TreeSpec<Aux> from_str_internal(
549552
read_idx++;
550553
}
551554
c->leaves_num = leaves_offset;
552-
return c;
555+
return TreeSpec<Aux>(std::move(c));
553556
}
554557

555558
case Config::kLeaf:

0 commit comments

Comments
 (0)