I have a situation where I have a 10mb String that I need to prepend 50-500bytes to the beginning 100 times, with the first 50-500 bytes different each time. This would be massively wasteful to reallocate 100 times. I was hoping there could be a way to make multiple &str appear combined as a String to a function I need to pass it to for generating a hash of the text.
One way I could do this is allocate the data once, and then just exchange the beginning data each time but this seems tricky to get everything aligned correctly. It would also force my code to be synchronous since a mutable reference cannot be shared between threads.
Can anyone offer a better solution?
You can't make something that's not a
String look like a
String is a type, not a trait.
Is the hash function under your control? If so, could you modify it to take a
Read rather than a
String? That would be a better API regardless (IMO), and then you could quite easily implement
Read for a chain of
I need to take a SHA256 sum of the data with a package like: sha256 - Rust
I have not gotten to the point where I am doing the sum yet so I haven't actually researched the packages, I'm just trying to think ahead. What you proposed is interesting, I hadn't thought of that. I'm not sure if that sha256 package supports that but it surely might, perhaps you could tell me.
sha256 looks like a wrapper around the
sha2 implements SHA-256;
sha256 just provides a handy
So you could use
sha2, and then it should be simple. With
sha2, you can create a
Sha256 type, and then feed your data into it, in chunks of whatever size you like.
Ok, thank you for explaining that. Learned a new thing today. I will try this and report back if I run into trouble.
Hashing methods have
update() that lets you add data bit by bit, so call
update() for each
&str that you have, and the result will be the same as hashing one long
If you need to do this sort of expensive string manipulation often, it might be worth using a more efficient data sructure than
I know the
ropey crate has a nice rope implementation, and it has methods which will yield the individual strings that make up the rope so you can hash them incrementally.