Add NullStr type#496
Conversation
17cf992 to
d05ab86
Compare
8c134f7 to
468144f
Compare
|
There's a world where it might be better to make the wrappers even thinner, to reduce some duplicated code. e.g. something like: pub struct SpComponent(nullstr::NullStr);and impl deref/derefmut, but that's also Not Exactly The Best Approach. OR, we could make the inner field public, and just use methods directly, but this exposes OR, we could write a little decl macro that impls all the pass-throughs per-type. All options are imperfect, open to thoughts. |
|
Some part of me would still like to see this replaced with the I'm fine with merging the two types later though, and I'm happy to merge forwards with this change for now since I know other stuff you're working on depends on it. |
I think keeping Let me know if you have any opinions on the APIs, or how I've done the plumbing with the shim layers. Arguably, making |
hawkw
left a comment
There was a problem hiding this comment.
Thanks for doing this! As I mentioned, it would be nice if, eventually, this could be merged with FixedStr, but it seems good for now.
I left some relatively minor notes; overall, the implementation looks solid.
| /// fail (i.e., we're always storing contents as human-readable | ||
| /// strings), but because we reconstitute components from network messages | ||
| /// we still need to check. | ||
| pub fn as_str(&self) -> Option<&str> { |
There was a problem hiding this comment.
i feel like this could return the str::FromUtf8Error rather than an Option?
There was a problem hiding this comment.
It could! But this code was already there. I could change it if you think that would be a good idea though!
| /// Create an [`SpComponent`] from the given slice. | ||
| /// | ||
| /// This function has some Interesting Details: | ||
| /// | ||
| /// 1. If `src` exceeds the length of `N`, only the first `N` bytes will be copied in | ||
| /// 2. If `src` contains null bytes, these will be copied in | ||
| /// | ||
| /// You might ask yourself, why isn't this unsafe? Why doesn't it violated any invariants? | ||
| /// Well, we implement `Deserialize`, which means we might obtain garbage off the wire | ||
| /// anyway! So, morally, this is not particularly worse to support. | ||
| /// | ||
| /// This is *primarily* intended to be done when re-magicking from a code-generated string. | ||
| #[inline] | ||
| pub fn from_bstr_unchecked(src: &[u8]) -> Self { | ||
| Self { id: nullstr::NullStr::from_bstr_unchecked(src) } | ||
| } | ||
|
|
||
| #[inline] | ||
| pub const fn from_const(val: &str) -> Self { | ||
| Self { id: nullstr::NullStr::from_const(val) } | ||
| } |
There was a problem hiding this comment.
huh, this all ends up a bit boilerplate-y; I thought about saying "can't we have some kinda From<T> where T: Into<NullStr>, but I guess we can't, because the whole point of the different constructors is to have some be const-callable so it doesn't really work out. This still seem sworth it, just a bit of a bother.
Yeah, it's not --- I think it may be worth a longer term project to unify the two. We could easily make The reason I would want this in the long term is that we use device refdes produced by codegen for both the |
468144f to
604614c
Compare
This is a private type that is intended to be shared across wire types that act as a null-padded fixed length container format. This is intended to be used in a follow up PR for supporting oxidecomputer/hubris#2463, but this is separated to make reviewing this helper easier.
604614c to
d06ea64
Compare
hawkw
left a comment
There was a problem hiding this comment.
okay, this seems good to me! it might be worth testing that a faux-mgs built from this branch can happily call into the inventory and component-details APIs on a SP running the current release Hubris image, to ensure we've not accidentally broken wire compatibility someplace? i feel like that's unlikely based on the change but it is probably still worth checking.
|
Happy to do so! Will probably be tomorrow AM my time. Appreciate the review! |
|
@hawkw tested successfully (full info in chat) with
|
Codegen impl changes necessary to support oxidecomputer/management-gateway-service#496.
This is a private type that is intended to be shared across wire types that act as a null-padded fixed length container format.
This is intended to be used in a follow up PR for supporting oxidecomputer/hubris#2463, but this is separated to make reviewing this helper easier. Split out of https://github.com/oxidecomputer/management-gateway-service/tree/james/pmbus-please.