Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Add helper for creating CharSlice from a string literal #36

Merged
merged 4 commits into from
Mar 21, 2022
Merged
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions ddprof-ffi/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ style = "both"
no_includes = true
sys_includes = ["stdbool.h", "stddef.h", "stdint.h"]

after_includes = """

#define ddprof_ffi_CharSlice_from_literal(string) ((ddprof_ffi_CharSlice) {.ptr = "" string, .len = sizeof("" string) - 1})"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion !
But I find sizeof dangerous:

const char* s = "foo";
...
ddprof_ffi_CharSlice_from_literal(s);

will construct an incorrect slice.
Using strlen would avoid such errors and strlen call would be optimized away by the compiler for string literals.
I am also not a big fan of macros, you could use static inline function:

static inline ddprof_ffi_CharSlice ddprof_ffi_CharSlice_from_literal(const char*s) {
    return (ddprof_ffi_CharSlice){.ptr=s, .len=strlen(s); };
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion !
But I find sizeof dangerous:

const char* s = "foo";
...
ddprof_ffi_CharSlice_from_literal(s);

will construct an incorrect slice.

Actually, by design, it won't. It will give you a compile error :D :D

That's because of the .ptr = "" string instead of .ptr = string -- you can only char *foo = "do " "this " "if " "every " "part " "is " "a " "literal";.

To be fair the error you get is not a great one, but at least it does barf at the usage.

We could have both ddprof_ffi_CharSlice_from_literal as well as a strlen version ddprof_ffi_CharSlice_from_char (?), although I do somewhat dislike that you may be doing strlen without realizing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, you got me thinking that the error message could be improved. I've pushed d20aff4 that adds a nice comment which incidentally gets shown by the compiler when you try to pass in a char*:

foo.c: In function 'endpoint_from':
foo.c:133:27: error: expected '}' before 's'
   byte_slice_from_literal(s);
                           ^
foo.c:15:113: note: in definition of macro 'byte_slice_from_literal'
   /* NOTE: Compilation fails if you pass in a char* instead of a literal */ ((ddprof_ffi_CharSlice) {.ptr = "" string, .len = sizeof(string) - 1})
                                                                                                                ^~~~~~
foo.c:15:91: note: to match this '{'
   /* NOTE: Compilation fails if you pass in a char* instead of a literal */ ((ddprof_ffi_CharSlice) {.ptr = "" string, .len = sizeof(string) - 1})


[export]
prefix = "ddprof_ffi_"

Expand Down