You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Our errors currently store the source value of any cast or conversion. We also permit the user to discard this value via .map_src methods, which is sometimes necessary in order to play nicely with APIs that require errors to be 'static, Send, or Sync.
However, we've also discussed supporting #280 by doing the opposite: by preventing the user from discarding the source, we can use an error object as a witness that an error was encountered with a particular source type, and thus as a witness that certain failure modes are impossible (in particular, when casting from Src to Dst where Src is more aligned than Dst, alignment errors are impossible).
Design
These two goals are at odds, but I think we can reconcile them. We do this by storing both the original source type and a source value.
pubstructAlignmentError<Src,Dst: ?Sized,SrcVal = Src>{/// The source value involved in the conversion.src_val:SrcVal,/// The inner destination type inolved in the conversion.////// INVARIANT: An `AlignmentError` may only be constructed if `Dst`'s/// alignment requirement is greater `Src`'s alignment requirement.src_dst:SendSyncPhantomData<(Src,Dst)>,}impl<Src,Dst: ?Sized,SrcVal>AlignmentError<Src,Dst,SrcVal>{pubfnmap_src<NewSrcVal>(self,f:implFn(SrcVal) -> NewSrcVal) -> AlignmentError<Src,Dst,NewSrcVal>{AlignmentError{src_val:f(self.src_val),src_dst:SendSyncPhantomData::default()}}}
With this change, we can require that both Src and Dst are the actual types involved in a cast, and thus we can make the internal invariant more powerful. As it stands today, the invariant is that:
Design
Problem statement
Our errors currently store the source value of any cast or conversion. We also permit the user to discard this value via
.map_src
methods, which is sometimes necessary in order to play nicely with APIs that require errors to be'static
,Send
, orSync
.However, we've also discussed supporting #280 by doing the opposite: by preventing the user from discarding the source, we can use an error object as a witness that an error was encountered with a particular source type, and thus as a witness that certain failure modes are impossible (in particular, when casting from
Src
toDst
whereSrc
is more aligned thanDst
, alignment errors are impossible).Design
These two goals are at odds, but I think we can reconcile them. We do this by storing both the original source type and a source value.
With this change, we can require that both
Src
andDst
are the actual types involved in a cast, and thus we can make the internal invariant more powerful. As it stands today, the invariant is that:zerocopy/src/error.rs
Lines 237 to 238 in 0b8ad3e
With this change, we generalize the invariant to require that
Dst
's alignment is greater thanSrc
's alignment.Use
In theory, this should let us infallibly discard alignment errors in more circumstances, generalizing our current support:
zerocopy/src/error.rs
Lines 720 to 769 in 0b8ad3e
However, this requires being able to express alignment inequality as a type-level bound (e.g. via #1316), which we don't yet support.
The text was updated successfully, but these errors were encountered: