Use Borrow<T> instead

pull/431/head
Ricky Han 2 years ago
parent d4a399fb0c
commit 1247ffc19b

@ -1,6 +1,6 @@
use std::{
convert::{AsMut, AsRef},
marker::PhantomData,
marker::PhantomData, borrow::Borrow,
};
use crate::{
@ -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.
@ -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)
));
}

@ -1,4 +1,5 @@
//! Hash map types.
use crate::{
maps::MapError,
sys::{bpf_map_delete_elem, bpf_map_update_elem},
@ -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,

@ -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.

Loading…
Cancel
Save