-
Notifications
You must be signed in to change notification settings - Fork 76
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
The difference between Bytes and Array[Byte] #466
Comments
The |
If you have a free conversion then you might write this erronous code: fn main {
let bytes : Array[Bytes] = [b'\x00', b'\x01', b'\x02']
let content : Bytes = bytes.to_bytes()
bytes[1] = b'\x02'
// now content is [0, 2, 2]
} |
Apart from that,
So |
Got it, thanks. I opened this issue because I think it should also be included in the docs (in the website and the code) |
BTW, could there be an API like |
Do you have a concrete use case? I think it's possible for |
I think it would be great if there were a preferred way to handle Bytes. If the conversion is free, a function can always return a |
Why not use a BytesView then? |
When serializing an object, we may not know the size of the serialized data in advance. So we need to initialize it as |
If you are trying to serializing data that you don't know the size in advance, you will definitely resize the buffer and introduce copying, and the final result can hardly be the exact size, which will introduce another copy. So I don't see how the 'zero cost' can be benefitial |
It's a good point. But we usually don't need the capacity and length to be the same, right? My thought is if there is an ideal way to deal with bytes, there can be a convention so that people can share code more easily in the future. BTW, will Bytes and Array[Byte] be compiled to |
I would suggest sharing with As of the ABI, I don't think it's stable yet (i.e. we won't make a commitment yet). I think the primitives are compiled to ArrayBuffer's @hackwaly is that the case? |
We compiles
|
Ah. I see. Thanks |
Sorry. There's |
Why the conversion between them is not free?
The text was updated successfully, but these errors were encountered: