Skip to content

Commit

Permalink
Merge pull request #358 from nervosnetwork/clippy-fix
Browse files Browse the repository at this point in the history
chore: clippy fix
  • Loading branch information
driftluo authored Jun 23, 2022
2 parents 6eda9ac + dd20a16 commit a5b945f
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 31 deletions.
3 changes: 1 addition & 2 deletions multiaddr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ impl Multiaddr {
/// ```
///
pub fn push(&mut self, p: Protocol<'_>) {
let mut w = Vec::with_capacity(self.bytes.len());
unsafe { w.set_len(self.bytes.len()) }
let mut w = vec![0; self.bytes.len()];
w.copy_from_slice(&self.bytes[..]);
p.write_to_bytes(&mut w);
self.bytes = Bytes::from(w);
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.56.1
1.61.0
2 changes: 1 addition & 1 deletion secio/src/codec/secure_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ where
pub(crate) async fn verify_nonce(&mut self) -> Result<(), SecioError> {
if !self.nonce.is_empty() {
let mut nonce = self.nonce.clone();
self.read(&mut nonce).await?;
self.read_exact(&mut nonce).await?;

trace!(
"received nonce={}, my_nonce={}",
Expand Down
12 changes: 2 additions & 10 deletions secio/src/crypto/openssl_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ impl OpenSsLCrypt {

// aead use self-increase iv
let nonce_size = cipher_type.iv_size();
let mut nonce = BytesMut::with_capacity(nonce_size);
// Safety: capacity == len
unsafe {
nonce.set_len(nonce_size);
::std::ptr::write_bytes(nonce.as_mut_ptr(), 0, nonce_size);
}
let nonce = BytesMut::from(vec![0u8; nonce_size].as_slice());

OpenSsLCrypt {
cipher,
Expand All @@ -53,10 +48,7 @@ impl OpenSsLCrypt {
pub fn encrypt(&mut self, input: &[u8]) -> Result<Vec<u8>, SecioError> {
nonce_advance(self.iv.as_mut());
let tag_size = self.cipher_type.tag_size();
let mut tag = Vec::with_capacity(tag_size);
unsafe {
tag.set_len(tag_size);
}
let mut tag = vec![0; tag_size];
let mut output =
symm::encrypt_aead(self.cipher, &self.key, Some(&self.iv), &[], input, &mut tag)?;
output.append(&mut tag);
Expand Down
10 changes: 2 additions & 8 deletions secio/src/crypto/ring_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ impl RingAeadCipher {
/// +----------------------------------------+-----------------------+
/// ```
pub fn encrypt(&mut self, input: &[u8]) -> Result<Vec<u8>, SecioError> {
let mut output = Vec::with_capacity(input.len() + self.cipher_type.tag_size());
unsafe {
output.set_len(input.len());
}
let mut output = vec![0; input.len()];
output.copy_from_slice(input);
if let RingAeadCryptoVariant::Seal(ref mut key) = self.cipher {
key.seal_in_place_append_tag(Aad::empty(), &mut output)
Expand All @@ -96,11 +93,8 @@ impl RingAeadCipher {
.len()
.checked_sub(self.cipher_type.tag_size())
.ok_or(SecioError::FrameTooShort)?;
let mut buf = Vec::with_capacity(input.len());
let mut buf = vec![0; input.len()];

unsafe {
buf.set_len(input.len());
}
buf.copy_from_slice(input);

if let RingAeadCryptoVariant::Open(ref mut key) = self.cipher {
Expand Down
4 changes: 3 additions & 1 deletion secio/src/dh_compat/openssl_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ use openssl::{

use crate::{dh_compat::KeyAgreement, error::SecioError};

type PairsGenarate = fn() -> Result<(PKey<Private>, Vec<u8>), SecioError>;

struct Algorithm {
_private_len: usize,
pubkey_len: usize,
pairs_generate: fn() -> Result<(PKey<Private>, Vec<u8>), SecioError>,
pairs_generate: PairsGenarate,
from_pubkey: fn(&[u8]) -> Result<PKey<Public>, SecioError>,
}

Expand Down
2 changes: 1 addition & 1 deletion secio/src/handshake/handshake_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Propose {
.set(self.rand.into_iter().map(Into::into).collect())
.build();
let pubkey = handshake_mol::Bytes::new_builder()
.set(self.pubkey.to_vec().into_iter().map(Into::into).collect())
.set(self.pubkey.iter().copied().map(Into::into).collect())
.build();
let exchange = handshake_mol::String::new_builder()
.set(
Expand Down
4 changes: 2 additions & 2 deletions tentacle/src/protocol_handle_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ where
self.current_task.idle();
break;
}
poll_fn(|cx| crate::runtime::poll_proceed(cx)).await;
poll_fn(crate::runtime::poll_proceed).await;
tokio::select! {
event = self.receiver.next() => {
match event {
Expand Down Expand Up @@ -447,7 +447,7 @@ where

pub async fn run(&mut self, mut recv: oneshot::Receiver<()>) {
loop {
poll_fn(|cx| crate::runtime::poll_proceed(cx)).await;
poll_fn(crate::runtime::poll_proceed).await;
tokio::select! {
event = self.receiver.next() => {
match event {
Expand Down
6 changes: 3 additions & 3 deletions tentacle/src/runtime/budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ mod test {
#[test]
fn test_budget() {
assert_eq!(get(), 128);
block_on(poll_fn(|cx| poll_proceed(cx)));
block_on(poll_fn(poll_proceed));
assert_eq!(get(), 127);

thread::spawn(|| {
assert_eq!(get(), 128);
block_on(async {
for _ in 0..2 {
poll_fn(|cx| poll_proceed(cx)).await
poll_fn(poll_proceed).await
}
});
assert_eq!(get(), 126);
Expand All @@ -59,7 +59,7 @@ mod test {
assert_eq!(get(), 127);
block_on(async {
for _ in 0..127 {
poll_fn(|cx| poll_proceed(cx)).await
poll_fn(poll_proceed).await
}
});
assert_eq!(get(), 127);
Expand Down
2 changes: 1 addition & 1 deletion tentacle/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ where
break;
}

poll_fn(|cx| crate::runtime::poll_proceed(cx)).await;
poll_fn(crate::runtime::poll_proceed).await;
#[cfg(not(target_arch = "wasm32"))]
self.try_update_listens().await;
tokio::select! {
Expand Down
2 changes: 1 addition & 1 deletion yamux/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ mod test {
let data = [0; 8];

stream.send_window_update().unwrap();
stream.write(&data).await.unwrap();
stream.write_all(&data).await.unwrap();

let event = unbound_receiver.next().await.unwrap();
match event {
Expand Down

0 comments on commit a5b945f

Please sign in to comment.