named-data / python-ndn

An NDN client library with AsyncIO support in Python 3
https://python-ndn.readthedocs.io/en/latest
Apache License 2.0
24 stars 17 forks source link

Component no to_bytes() option #23

Closed justincpresley closed 3 years ago

justincpresley commented 3 years ago

When transferring bytes into a Component via from_bytes(), there is not a good way to transfer it back since there is no to_bytes() method.

Since the from_bytes() method adds a type and length, it is not a straightforward method to get it back into bytes and does require lots of testing / hardcoding to get a solution that does get it back. I would greatly appreciate adding this method as I am doing work that requires the from_bytes() method to be used.

Thank you.

zjkmxy commented 3 years ago

Why don't just use get_value()? The following program is supposed to be always true:

bytes(Name.Component.get_value(Name.Component.from_bytes(X))) == X

The outmost bytes is used because get_value returns a memoryview to avoid unnecessary copy.