P3KI / bendy

A rust library for encoding and decoding bencode with enforced cannonicalization rules.
BSD 3-Clause "New" or "Revised" License
74 stars 14 forks source link

Unable to encode Value type in a container #37

Closed 17dec closed 4 years ago

17dec commented 4 years ago

Easiest to illustrate with an example, attempting to encode an empty Vec<Value>

use bendy::{value::Value,encoding::ToBencode};

fn main() {
    let _ = Vec::<Value>::new().to_bencode();
}

Results in a compile error:

error[E0080]: could not evaluate constant
   --> $path/bendy-0.3.0/src/encoding/to_bencode.rs:114:38
    |
114 |             const MAX_DEPTH: usize = ContentT::MAX_DEPTH + 1;
    |                                      ^^^^^^^^^^^^^^^^^^^^^^^ attempt to add with overflow

Which I suspect is due to Value::MAX_DEPTH being the max_usize. A saturating add would be ideal in this case, but using a slightly lower MAX_DEPTH with room for a bunch parent containers may work, too.

thequux commented 4 years ago

Yeah, I think that saturating adds are the right solution here. I'll fix this quickly and push out 0.3.1, but long term, it was a mistake to make Value have a MAX_DEPTH of usize::max_value() (it should actually have been 0, following the doc comment in encoding.rs.

That's a breaking change, though, so it will need to be fixed in 0.4

thequux commented 4 years ago

Could you test using the latest master? If it works for you, I'll release it as 0.3.1.

Thanks!

17dec commented 4 years ago

Tested & working.

Thanks for the quick fix!