This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:will construct an incorrect slice.
Using
strlen
would avoid such errors andstrlen
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:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 onlychar *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 versionddprof_ffi_CharSlice_from_char
(?), although I do somewhat dislike that you may be doing strlen without realizing.There was a problem hiding this comment.
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*: