Introduce an interim 'Utf8String' ref struct type for UTF-8 string literals for type (pin) safety #102949
Replies: 2 comments 5 replies
-
You don't get pin safety if there is a string-like Utf8String. Strings are allocated on heap and are unsafe to be operated like that. You code is already creating a GC hole if it accepts a (non-literal) string. Indeed, the pattern you are using that accepting a memory-safe object and returning a pointer is never GC-safe. If all you need is to pass the string to an unmanaged function, you can just use Or, you can write an analyzer at C# level to force only literals can be passed into your function. This also helps |
Beta Was this translation helpful? Give feedback.
-
I think the right way is to introduce an explicit extension for |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I love the new "u8" string literals and they are very efficient to work with when comes to interop with old unmanaged code that takes ASCII strings. I believe these utf8 string are allocated in the .data section, so GC won't touch them and they are pretty much already "pinned", so I came up with code like below to get the pointer address of these literals:
As you can see, however, since all utf-8 string literals are
ReadOnlySpan<byte>
, this method is very dangerous, because we cannot tell if the parameter passed in is a real utf-8 string literal that's already pinned, or just some managed, unpinnedReadOnlySpan<byte>
memory blocks.I know this is a very corner case and rare usage, but it would be nice to have an intermediate type like
Utf8String
so that I could at least constraint the passed in parameter a bit for type (pin) safety.Also hopefully I got it right about the non-alloc and no-GC characteristics of the utf-8 string literals :)
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions