-
Notifications
You must be signed in to change notification settings - Fork 60
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
Introduce ByteString based on an existing ByteArray #428
base: master
Are you sure you want to change the base?
Conversation
Maybe OT, but while we're talking about an easy way to initialize a |
You can already wrap an existing
You can ask a |
@lppedd in addition to what Jake wrote, you can also use UnsafeBufferOperations.moveToTail to gave ownership over an array to the buffer, which, to some extent, should be the same as initializing a buffer with an array. |
@Laxystem one of the most crucial parts of the ByteString's contract is its immutability. The approach proposed in this PR let users to accidentaly break the contract much easily. There is existing API that raises the same concern, however, it is ringing all the bells to ensure that nobody will break the contract unintentionally (and, hopefully, won't break it at all):
If some "unsafe" API is required, (most likely) the only place for it would be I would love to read more details about a problem you're trying to solve. Perhaps, we'll figure out how to solve your problem without making the builder less "safe". |
Not really. My use case for this is simple. interface VertexKind<in T> {
val sizeInBytes: UInt
fun ByteStringBuilder.append(vertex: T)
}
interface Mesh<out T> {
val vertices: Sequence<T>
val indices: Sequence<T>
} I want to be able to use the same code (and therefore, API) for both constructing a new vertex buffer out of the The length of a Perhaps it'd be preferred to make the new constructor introduced |
I implemented this using an existing class and an
extendable
boolean as I didn't want to break binary compatibility with existing usage of theByteStringBuilder
constructor, but it is possible to rewrite using an abstract class (should be ever-so-slightly-faster).My use case: I have a game engine, and I'd like to avoid copying data around more than necessary. Usually, this would be a micro-optimization; But re-meshing everything every frame adds up quickly. This way, I can pass my existing vertex buffer, and an offset (that I can record using the buffer's own
size
), and simply override the changed vertex's data.I did not add a
buildByteString
function as this is a lower-level use-case and should be treated as such; I expect most to not need the resultingByteString
at all, and simply useapply
instead.I would add a
DelicateIoApi
annotation, but I didn't seem to find one.