Skip to content

Commit

Permalink
Use Borrow<T> instead
Browse files Browse the repository at this point in the history
  • Loading branch information
0b01 committed Nov 1, 2022
1 parent d4a399f commit 1247ffc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
8 changes: 4 additions & 4 deletions aya/src/maps/hash_map/hash_map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
convert::{AsMut, AsRef},
marker::PhantomData,
marker::PhantomData, borrow::Borrow,
};

use crate::{
Expand Down Expand Up @@ -78,8 +78,8 @@ impl<T: AsRef<MapData>, K: Pod, V: Pod> HashMap<T, K, V> {

impl<T: AsMut<MapData>, K: Pod, V: Pod> HashMap<T, K, V> {
/// Inserts a key-value pair into the map.
pub fn insert(&mut self, key: K, value: V, flags: u64) -> Result<(), MapError> {
hash_map::insert(self.inner.as_mut(), key, value, flags)
pub fn insert(&mut self, key: impl Borrow<K>, value: impl Borrow<V>, flags: u64) -> Result<(), MapError> {
hash_map::insert(self.inner.as_mut(), key.borrow(), value.borrow(), flags)
}

/// Removes a key from the map.
Expand Down Expand Up @@ -285,7 +285,7 @@ mod tests {
let mut hm = HashMap::<_, u32, u32>::new(&mut map).unwrap();

assert!(matches!(
hm.insert(1, 42, 0),
hm.insert(1u32, 42u32, 0),
Err(MapError::SyscallError { call, io_error }) if call == "bpf_map_update_elem" && io_error.raw_os_error() == Some(EFAULT)
));
}
Expand Down
7 changes: 4 additions & 3 deletions aya/src/maps/hash_map/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Hash map types.

use crate::{
maps::MapError,
sys::{bpf_map_delete_elem, bpf_map_update_elem},
Expand All @@ -15,12 +16,12 @@ use super::MapData;

pub(crate) fn insert<K, V>(
map: &mut MapData,
key: K,
value: V,
key: &K,
value: &V,
flags: u64,
) -> Result<(), MapError> {
let fd = map.fd_or_err()?;
bpf_map_update_elem(fd, Some(&key), &value, flags).map_err(|(_, io_error)| {
bpf_map_update_elem(fd, Some(key), value, flags).map_err(|(_, io_error)| {
MapError::SyscallError {
call: "bpf_map_update_elem".to_owned(),
io_error,
Expand Down
2 changes: 1 addition & 1 deletion aya/src/maps/sock/sock_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<T: AsRef<MapData>, K: Pod> SockHash<T, K> {
impl<T: AsMut<MapData>, K: Pod> SockHash<T, K> {
/// Inserts a socket under the given key.
pub fn insert<I: AsRawFd>(&mut self, key: K, value: I, flags: u64) -> Result<(), MapError> {
hash_map::insert(self.inner.as_mut(), key, value.as_raw_fd(), flags)
hash_map::insert(self.inner.as_mut(), &key, &value.as_raw_fd(), flags)
}

/// Removes a socket from the map.
Expand Down

0 comments on commit 1247ffc

Please sign in to comment.