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

clone doesn't work with i_aux custom container #119

Open
fonghou opened this issue Feb 2, 2025 · 1 comment
Open

clone doesn't work with i_aux custom container #119

fonghou opened this issue Feb 2, 2025 · 1 comment

Comments

@fonghou
Copy link

fonghou commented Feb 2, 2025

I'm trying to implement custom i_aux following sample code in documentation e.g.

#define i_aux { MemoryContext memctx; } // NB: enclose in curly braces!
#define i_allocator pgs
#define i_no_clone

I noticed it uses i_no_clone.

I tried to implement a custom allocator (example code below). The code compiles but failed at runtime. In the screenshot, it's clear that Self tmp = {0} didn't copy vec.aux field. Is this a bug or by design (if later, it'd be helpful to issue a warn/error at compile time).

Image
#define i_type     Users
#define i_keyclass User
#define i_use_cmp
#define i_aux { Arena *arena; }
#define i_allocator arena
#include "stc/vec.h"

void test_vec2(Arena a) {
  Users users = {.aux = {&a}};
  Users_push(&users, (User){cstr_lit("mary"), 0});
  Users_push(&users, (User){cstr_lit("joe"), 1});
  Users_push(&users, (User){cstr_lit("admin"), 2});

  Users users2 = Users_clone(users); 
  Users_sort(&users2);

  for (c_each(i, Users, users2))
    printf("%s: %d\n", cstr_str(&i.ref->name), i.ref->id);

  c_drop(Users, &users, &users2);  // cleanup
}

@fonghou
Copy link
Author

fonghou commented Feb 2, 2025

btw, here is a simple fix.

diff --git a/include/stc/vec.h b/include/stc/vec.h
index cecffb4e..30274ccf 100644
--- a/include/stc/vec.h
+++ b/include/stc/vec.h
@@ -333,6 +333,9 @@ _c_MEMB(_erase_n)(Self* self, const isize idx, const isize len) {
 STC_DEF Self
 _c_MEMB(_clone)(Self vec) {
     Self tmp = {0};
+#ifdef i_aux
+    tmp.aux = vec.aux;
+#endif
     _c_MEMB(_copy_n)(&tmp, 0, vec.data, vec.size);
     vec.data = tmp.data;
     vec.capacity = tmp.capacity;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant