Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions zerocopy/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,23 @@ pub struct SizeError<Src, Dst: ?Sized> {
}

impl<Src, Dst: ?Sized> SizeError<Src, Dst> {
pub(crate) fn new(src: Src) -> Self {
/// Constructs a new `SizeError`.
///
/// This can be used to propagate a size error out of code which knows,
/// out-of-band, that a conversion would fail due to an incorrect source
/// size, without needing to actually perform a failing conversion just to
/// obtain the error.
///
/// # Examples
///
/// ```
/// use zerocopy::error::{ConvertError, SizeError};
///
/// let err: SizeError<(), u32> = SizeError::new(());
/// let err: ConvertError<(), SizeError<(), u32>, ()> = err.into();
/// ```
#[inline]
pub fn new(src: Src) -> Self {
Self { src, _dst: SendSyncPhantomData::default() }
}

Expand Down Expand Up @@ -595,7 +611,24 @@ pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
}

impl<Src, Dst: ?Sized + TryFromBytes> ValidityError<Src, Dst> {
pub(crate) fn new(src: Src) -> Self {
/// Constructs a new `ValidityError`.
///
/// This can be used to propagate a validity error out of code which
/// knows, out-of-band, that a conversion would fail due to invalid
/// source data, without needing to actually perform a failing conversion
/// just to obtain the error.
///
/// # Examples
///
/// ```
/// use zerocopy::error::{ConvertError, ValidityError};
/// use zerocopy::FromBytes;
///
/// let err: ValidityError<(), bool> = ValidityError::new(());
/// let err: ConvertError<(), (), ValidityError<(), bool>> = err.into();
/// ```
#[inline]
pub fn new(src: Src) -> Self {
Self { src, _dst: SendSyncPhantomData::default() }
}

Expand Down
8 changes: 4 additions & 4 deletions zerocopy/tests/ui/try_transmute.msrv.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ error[E0277]: the trait bound `NotZerocopy: TryFromBytes` is not satisfied
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `TryFromBytes` is not implemented for `NotZerocopy`
|
note: required by a bound in `ValidityError`
--> $WORKSPACE/src/error.rs:590:45
--> $WORKSPACE/src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`

error[E0277]: the trait bound `NotZerocopy: TryFromBytes` is not satisfied
Expand All @@ -30,9 +30,9 @@ error[E0277]: the trait bound `NotZerocopy: TryFromBytes` is not satisfied
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `TryFromBytes` is not implemented for `NotZerocopy`
|
note: required by a bound in `ValidityError`
--> $WORKSPACE/src/error.rs:590:45
--> $WORKSPACE/src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: this error originates in the macro `try_transmute` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
8 changes: 4 additions & 4 deletions zerocopy/tests/ui/try_transmute.nightly.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ help: the trait `TryFromBytes` is not implemented for `NotZerocopy`
(A, B, C, D, E, F, G, H)
and $OTHER_TYPES others
note: required by a bound in `ValidityError`
--> src/error.rs:590:45
--> src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: the full name for the type has been written to '/long-type-HASH.txt'
= note: consider using `--verbose` to print the full type name to the console
Expand Down Expand Up @@ -85,9 +85,9 @@ help: the trait `TryFromBytes` is not implemented for `NotZerocopy`
(A, B, C, D, E, F, G, H)
and $OTHER_TYPES others
note: required by a bound in `ValidityError`
--> src/error.rs:590:45
--> src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: the full name for the type has been written to '/long-type-HASH.txt'
= note: consider using `--verbose` to print the full type name to the console
Expand Down
8 changes: 4 additions & 4 deletions zerocopy/tests/ui/try_transmute.stable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ help: the trait `TryFromBytes` is not implemented for `NotZerocopy`
(A, B, C, D, E, F, G, H)
and $OTHER_TYPES others
note: required by a bound in `ValidityError`
--> $WORKSPACE/src/error.rs:590:45
--> $WORKSPACE/src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: the full name for the type has been written to '/long-type-HASH.txt'
= note: consider using `--verbose` to print the full type name to the console
Expand Down Expand Up @@ -85,9 +85,9 @@ help: the trait `TryFromBytes` is not implemented for `NotZerocopy`
(A, B, C, D, E, F, G, H)
and $OTHER_TYPES others
note: required by a bound in `ValidityError`
--> $WORKSPACE/src/error.rs:590:45
--> $WORKSPACE/src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: the full name for the type has been written to '/long-type-HASH.txt'
= note: consider using `--verbose` to print the full type name to the console
Expand Down
8 changes: 4 additions & 4 deletions zerocopy/tests/ui/try_transmute_mut.msrv.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ error[E0277]: the trait bound `NotZerocopy: TryFromBytes` is not satisfied
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `TryFromBytes` is not implemented for `NotZerocopy`
|
note: required by a bound in `ValidityError`
--> $WORKSPACE/src/error.rs:590:45
--> $WORKSPACE/src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`

error[E0277]: the trait bound `NotZerocopy: TryFromBytes` is not satisfied
Expand All @@ -33,9 +33,9 @@ error[E0277]: the trait bound `NotZerocopy: TryFromBytes` is not satisfied
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `TryFromBytes` is not implemented for `NotZerocopy`
|
note: required by a bound in `ValidityError`
--> $WORKSPACE/src/error.rs:590:45
--> $WORKSPACE/src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: this error originates in the macro `try_transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
8 changes: 4 additions & 4 deletions zerocopy/tests/ui/try_transmute_mut.nightly.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ help: the trait `TryFromBytes` is not implemented for `NotZerocopy`
(A, B, C, D, E, F, G, H)
and $OTHER_TYPES others
note: required by a bound in `ValidityError`
--> src/error.rs:590:45
--> src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: the full name for the type has been written to '/long-type-HASH.txt'
= note: consider using `--verbose` to print the full type name to the console
Expand Down Expand Up @@ -117,9 +117,9 @@ help: the trait `TryFromBytes` is not implemented for `NotZerocopy`
(A, B, C, D, E, F, G, H)
and $OTHER_TYPES others
note: required by a bound in `ValidityError`
--> src/error.rs:590:45
--> src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: the full name for the type has been written to '/long-type-HASH.txt'
= note: consider using `--verbose` to print the full type name to the console
Expand Down
8 changes: 4 additions & 4 deletions zerocopy/tests/ui/try_transmute_mut.stable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ help: the trait `TryFromBytes` is not implemented for `NotZerocopy`
(A, B, C, D, E, F, G, H)
and $OTHER_TYPES others
note: required by a bound in `ValidityError`
--> $WORKSPACE/src/error.rs:590:45
--> $WORKSPACE/src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: the full name for the type has been written to '/long-type-HASH.txt'
= note: consider using `--verbose` to print the full type name to the console
Expand Down Expand Up @@ -117,9 +117,9 @@ help: the trait `TryFromBytes` is not implemented for `NotZerocopy`
(A, B, C, D, E, F, G, H)
and $OTHER_TYPES others
note: required by a bound in `ValidityError`
--> $WORKSPACE/src/error.rs:590:45
--> $WORKSPACE/src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: the full name for the type has been written to '/long-type-HASH.txt'
= note: consider using `--verbose` to print the full type name to the console
Expand Down
8 changes: 4 additions & 4 deletions zerocopy/tests/ui/try_transmute_ref.msrv.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ error[E0277]: the trait bound `NotZerocopy: TryFromBytes` is not satisfied
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `TryFromBytes` is not implemented for `NotZerocopy`
|
note: required by a bound in `ValidityError`
--> $WORKSPACE/src/error.rs:590:45
--> $WORKSPACE/src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`

error[E0277]: the trait bound `NotZerocopy: TryFromBytes` is not satisfied
Expand All @@ -56,9 +56,9 @@ error[E0277]: the trait bound `NotZerocopy: TryFromBytes` is not satisfied
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `TryFromBytes` is not implemented for `NotZerocopy`
|
note: required by a bound in `ValidityError`
--> $WORKSPACE/src/error.rs:590:45
--> $WORKSPACE/src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: this error originates in the macro `try_transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
8 changes: 4 additions & 4 deletions zerocopy/tests/ui/try_transmute_ref.nightly.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ help: the trait `TryFromBytes` is not implemented for `NotZerocopy`
(A, B, C, D, E, F, G, H)
and $OTHER_TYPES others
note: required by a bound in `ValidityError`
--> src/error.rs:590:45
--> src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: the full name for the type has been written to '/long-type-HASH.txt'
= note: consider using `--verbose` to print the full type name to the console
Expand Down Expand Up @@ -136,9 +136,9 @@ help: the trait `TryFromBytes` is not implemented for `NotZerocopy`
(A, B, C, D, E, F, G, H)
and $OTHER_TYPES others
note: required by a bound in `ValidityError`
--> src/error.rs:590:45
--> src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: the full name for the type has been written to '/long-type-HASH.txt'
= note: consider using `--verbose` to print the full type name to the console
Expand Down
8 changes: 4 additions & 4 deletions zerocopy/tests/ui/try_transmute_ref.stable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ help: the trait `TryFromBytes` is not implemented for `NotZerocopy`
(A, B, C, D, E, F, G, H)
and $OTHER_TYPES others
note: required by a bound in `ValidityError`
--> $WORKSPACE/src/error.rs:590:45
--> $WORKSPACE/src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: the full name for the type has been written to '/long-type-HASH.txt'
= note: consider using `--verbose` to print the full type name to the console
Expand Down Expand Up @@ -136,9 +136,9 @@ help: the trait `TryFromBytes` is not implemented for `NotZerocopy`
(A, B, C, D, E, F, G, H)
and $OTHER_TYPES others
note: required by a bound in `ValidityError`
--> $WORKSPACE/src/error.rs:590:45
--> $WORKSPACE/src/error.rs:606:45
|
590 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
606 | pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
| ^^^^^^^^^^^^ required by this bound in `ValidityError`
= note: the full name for the type has been written to '/long-type-HASH.txt'
= note: consider using `--verbose` to print the full type name to the console
Expand Down
Loading