google / rust-async-coap

A flexible, asynchronous library for using and serving CoAP resources in Rust.
Apache License 2.0
51 stars 17 forks source link

async-coap-uri: Make `AnyUriRef::write_to` (and friends) unsafe. #17

Closed darconeous closed 5 years ago

darconeous commented 5 years ago

From the rustdocs for AnyUriRef::write_to:

Safety

Calling this method is not unsafe, but implementing it is! The underlying guarantee is that the written URI reference SHALL be well-formed. If this method writes out something that is not a valid URI reference, the resulting behavior is undefined.

That this method is safe to use but "unsafe" to implement seems problematic in its current form.

This change creates a new automatically-assigned trait, AnyUriRefExt, and moves all of the methods from AnyUriRef that should not be overridden into it. The previous AnyUriRef::write_to method is made unsafe and renamed to AnyUriRef::write_to_unsafe. A new method, AnyUriRefExt::write_to is added that simply calls unsafe { self.write_to_unsafe(write) }.

This arrangement continues to allow the behavior of write_to to be changed, but instead of overriding it directly you must now override AnyUriRef::write_to_unsafe.

This change also deals with a similar problem with write_resolved, except in that case the entire method was simply moved to AnyUriRefExt to prevent it from being overridden at all.

Fixes #8

darconeous commented 5 years ago

Unfortunately this diff is a tad longer than I would have wanted because I ended up doing a slight reorganization of some of the methods in AnyUriRef. Sorry about that.