Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: clippy fix #358

Merged
merged 1 commit into from
Jun 23, 2022
Merged

chore: clippy fix #358

merged 1 commit into from
Jun 23, 2022

Conversation

driftluo
Copy link
Collaborator

Uninitialized memory that does not have to be read or referenced, otherwise it is UB, but it can be converted as pointer to use.

But this behavior is difficult to delimit, especially when the type itself does not provide a corresponding method.

The correct behavior is for example Vec::as_mut_ptr():

    pub fn as_mut_ptr(&mut self) -> *mut T {
        // We shadow the slice method of the same name to avoid going through
        // `deref_mut`, which creates an intermediate reference.
        let ptr = self.buf.ptr();
        unsafe {
            assume(!ptr.is_null());
        }
        ptr
    }

The conversion process is entirely pointer and does not involve any notion of reference.

ByteMut::uninit_slice

  fn uninit_slice(&mut self) -> &mut UninitSlice {
      unsafe {
          let ptr = self.ptr.as_ptr().offset(self.len as isize);
          let len = self.cap - self.len;

          UninitSlice::from_raw_parts_mut(ptr, len)
      }
  }

ByteMut::as_slice_mut

    fn as_slice_mut(&mut self) -> &mut [u8] {
        unsafe { slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len) }
    }

These methods rely too much on the internal implementation of the library, which is not a big problem if it is a standard library, but it is dangerous to use third-party libraries in this way, so this PR removes all use cases

@driftluo driftluo requested a review from TheWaWaR as a code owner June 22, 2022 08:58
@driftluo driftluo merged commit a5b945f into master Jun 23, 2022
@driftluo driftluo deleted the clippy-fix branch June 23, 2022 03:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants