-
Notifications
You must be signed in to change notification settings - Fork 13
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
Knowing the amount of bits that will be written #11
Comments
Unlike As a result, the minimal-allocation method is to reuse a Feel free to give other/more specific reasons to implement this functionality, e.g. a code example, taking into account the above limitations. |
I'm not sure I fully understood your comment; what I meant was a trait like this: https://github.com/naia-lib/naia/blob/main/shared/serde/src/serde.rs#L4 Where there could be an additional function that simply returns the amount of bytes that the struct/enum will be serialized into, but without doing the actual serialization. For example via these kinds of implementations: https://github.com/naia-lib/naia/blob/main/shared/serde/src/impls/string.rs#L28 |
Thanks for providing a code example! It looks like you are using the bit length to decide whether to serialize the message at all, which could legitimately benefit from the functionality. (Edit: FWIW, I tried implementing the desired functionality on the |
I avoided adding something similar to
I would advise serializing each structure to a While copying the bytes isn't ideal, it should be much faster than something like |
@caibear brings up some good points against implementing this and a possible alternative for your code. Here is one more possible alternative for you, in the form of code that you can drop in to your project: use std::cell::RefCell;
use serde::Serialize;
use bitcode::{Encode, Buffer, Error};
// for serde::Serialize
fn serialize_len<T: Serialize + ?Sized>(t: &T) -> Result<usize, Error> {
thread_local! {
static BUFFER: RefCell<Option<Buffer>> = RefCell::new(None);
}
BUFFER.with(|buffer| {
let mut buffer = buffer.borrow_mut();
if buffer.is_none() {
*buffer = Some(Default::default());
}
buffer.as_mut().unwrap().serialize(t).map(|bytes| bytes.len())
})
}
// for bitcode::Encode
fn encode_len<T: Encode + ?Sized>(t: &T) -> Result<usize, Error> {
thread_local! {
static BUFFER: RefCell<Option<Buffer>> = RefCell::new(None);
}
BUFFER.with(|buffer| {
let mut buffer = buffer.borrow_mut();
if buffer.is_none() {
*buffer = Some(Default::default());
}
buffer.as_mut().unwrap().encode(t).map(|bytes| bytes.len())
})
} Use these as a last resort if you can't refactor your code as suggested by @caibear. By reusing the |
Thank you! |
Hi,
I'd like to use bitcode for games networking; and it would be useful to have a function to know how many bits/bytes a structure would take if it were encoded, but without doing the actual encoding (so that i know in which packet i can put the encoded data).
Something similar to https://docs.rs/bincode/latest/bincode/fn.serialized_size.html
The text was updated successfully, but these errors were encountered: