casey / casey/x-serialization-format

Consider using `MaybeUninit` to mark potentially invalid instances

Open
#29 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
1
Forks
1
PR merge metrics
No merged PRs in 30d

Description

Currently, `View::check` takes a `&self` reference, but that contents of that reference may be invalid. However, it was initialized from a `&[u8]` of the correct size, and a correct impl ensures that a value can have any valid bit pattern, so it is not uninitialized according to rust's rules.

However, it might still be useful to use MaybeUninit as the type passed to self, to signal that the type may not be correct, according to the requirements of the type.

If it is inappropriate to use `MaybeUninit`, then another option is to use something like:

```
struct Unchecked {
unchecked: T
}

impl Unchecked {
unsafe fn get(self) -> T {
self.unchecked
)
}
```

This would ensure that only unsafe code could access unchecked values. Currently, I'm using an unsafe trait impl, but it might also be good to additionally use a wrapper with an unsafe getter, so this requirement is understood.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.