Commit 2b89d5f
committed
encode: implement Encodable and Decodable for Vec<T>
Right now we have explicit impls of Vec<T> for a ton of specific types T.
This is annoying for users, who are unable to implement Vec<T> themselves
for their own types. In particular is super annoying for me trying to
do a semver trick with elements 0.25 and 0.26, since I can't reexport
the 0.26 Encodable trait since I would need to implement it for Vec<TxOut>
for the 0.25 TxOut type. (But conversely, if I retain the full trait
definition in 0.25, I can't blanket-impl this trait for all T: Encodable
because of this Vec<T> crap; so I wind up needing to explicitly implement
Encodable and Decodeable for every single type I re-export, and if I
forget any then that's a semver violation. Brutal.)
We do this is because we need a specialized impl for Vec<u8> and Rust has
no specialization. However, Kixunil pointed out on rust-bitcoin that we
can just do "runtime specialization" by checking TypeId::of::<T> and doing
the efficient thing when T is u8.
In practice, because the compiler knows T at codegen time, it will know
how the check goes and simply delete the check and the untaken branch,
so we have exactly the same result as we would if we had specialization.
This works only for T: 'static, but it turns out that all of our types
that we currently impl Encodable for Vec<T> on are 'static (and it's
hard to imagine one that wouldn't be, since all bitcoin/elements primitive
types are simple PODs).
It also seems plausible that a user could construct a Vec<u8> which the
compiler somehow doesn't realize is a Vec<u8> (e.g. if they had a
Vec<dyn Encodable> or something, in some future Rust version that
supports that). Such code will be sound and correct, but be slow because
it won't use the fast path. But I don't think this is a practical
concern because I can't even figure out how to do it..1 parent 45a6938 commit 2b89d5f
1 file changed
+73
-69
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
27 | 26 | | |
28 | 27 | | |
29 | 28 | | |
| |||
314 | 313 | | |
315 | 314 | | |
316 | 315 | | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
328 | 330 | | |
| 331 | + | |
329 | 332 | | |
| 333 | + | |
| 334 | + | |
330 | 335 | | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
349 | 377 | | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
350 | 383 | | |
351 | | - | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
352 | 393 | | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | 394 | | |
359 | 395 | | |
360 | 396 | | |
| |||
383 | 419 | | |
384 | 420 | | |
385 | 421 | | |
386 | | - | |
387 | | - | |
388 | | - | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | | - | |
399 | | - | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | 422 | | |
419 | 423 | | |
420 | 424 | | |
| |||
0 commit comments