nervosnetwork / tentacle

A multiplexed p2p network framework that supports custom protocols
https://docs.rs/tentacle
MIT License
54 stars 24 forks source link

chore: clippy fix #358

Closed driftluo closed 2 years ago

driftluo commented 2 years ago

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