diff --git a/aya/src/maps/array/array.rs b/aya/src/maps/array/array.rs index 9344f192..71ac1223 100644 --- a/aya/src/maps/array/array.rs +++ b/aya/src/maps/array/array.rs @@ -1,6 +1,5 @@ use std::{ - borrow::Borrow, - convert::{AsMut, AsRef}, + borrow::{Borrow, BorrowMut}, marker::PhantomData, }; @@ -35,9 +34,9 @@ pub struct Array { _v: PhantomData, } -impl, V: Pod> Array { +impl, V: Pod> Array { pub(crate) fn new(map: T) -> Result, MapError> { - let data = map.as_ref(); + let data = map.borrow(); check_kv_size::(data)?; let _fd = data.fd_or_err()?; @@ -52,7 +51,7 @@ impl, V: Pod> Array { /// /// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side. pub fn len(&self) -> u32 { - self.inner.as_ref().obj.max_entries() + self.inner.borrow().obj.max_entries() } /// Returns the value stored at the given index. @@ -62,7 +61,7 @@ impl, V: Pod> Array { /// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`] /// if `bpf_map_lookup_elem` fails. pub fn get(&self, index: &u32, flags: u64) -> Result { - let data = self.inner.as_ref(); + let data = self.inner.borrow(); check_bounds(data, *index)?; let fd = data.fd_or_err()?; @@ -82,7 +81,7 @@ impl, V: Pod> Array { } } -impl, V: Pod> Array { +impl, V: Pod> Array { /// Sets the value of the element at the given index. /// /// # Errors @@ -90,7 +89,7 @@ impl, V: Pod> Array { /// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`] /// if `bpf_map_update_elem` fails. pub fn set(&mut self, index: u32, value: impl Borrow, flags: u64) -> Result<(), MapError> { - let data = self.inner.as_mut(); + let data = self.inner.borrow_mut(); check_bounds(data, index)?; let fd = data.fd_or_err()?; bpf_map_update_elem(fd, Some(&index), value.borrow(), flags).map_err(|(_, io_error)| { @@ -103,9 +102,9 @@ impl, V: Pod> Array { } } -impl, V: Pod> IterableMap for Array { +impl, V: Pod> IterableMap for Array { fn map(&self) -> &MapData { - self.inner.as_ref() + self.inner.borrow() } fn get(&self, index: &u32) -> Result { diff --git a/aya/src/maps/array/per_cpu_array.rs b/aya/src/maps/array/per_cpu_array.rs index a3b5eab9..e83f59e5 100644 --- a/aya/src/maps/array/per_cpu_array.rs +++ b/aya/src/maps/array/per_cpu_array.rs @@ -1,5 +1,5 @@ use std::{ - convert::{AsMut, AsRef}, + borrow::{Borrow, BorrowMut}, marker::PhantomData, }; @@ -53,9 +53,9 @@ pub struct PerCpuArray { _v: PhantomData, } -impl, V: Pod> PerCpuArray { +impl, V: Pod> PerCpuArray { pub(crate) fn new(map: T) -> Result, MapError> { - let data = map.as_ref(); + let data = map.borrow(); check_kv_size::(data)?; let _fd = data.fd_or_err()?; @@ -70,7 +70,7 @@ impl, V: Pod> PerCpuArray { /// /// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side. pub fn len(&self) -> u32 { - self.inner.as_ref().obj.max_entries() + self.inner.borrow().obj.max_entries() } /// Returns a slice of values - one for each CPU - stored at the given index. @@ -80,7 +80,7 @@ impl, V: Pod> PerCpuArray { /// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`] /// if `bpf_map_lookup_elem` fails. pub fn get(&self, index: &u32, flags: u64) -> Result, MapError> { - let data = self.inner.as_ref(); + let data = self.inner.borrow(); check_bounds(data, *index)?; let fd = data.fd_or_err()?; @@ -100,7 +100,7 @@ impl, V: Pod> PerCpuArray { } } -impl, V: Pod> PerCpuArray { +impl, V: Pod> PerCpuArray { /// Sets the values - one for each CPU - at the given index. /// /// # Errors @@ -108,7 +108,7 @@ impl, V: Pod> PerCpuArray { /// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`] /// if `bpf_map_update_elem` fails. pub fn set(&mut self, index: u32, values: PerCpuValues, flags: u64) -> Result<(), MapError> { - let data = self.inner.as_mut(); + let data = self.inner.borrow_mut(); check_bounds(data, index)?; let fd = data.fd_or_err()?; @@ -122,9 +122,9 @@ impl, V: Pod> PerCpuArray { } } -impl, V: Pod> IterableMap> for PerCpuArray { +impl, V: Pod> IterableMap> for PerCpuArray { fn map(&self) -> &MapData { - self.inner.as_ref() + self.inner.borrow() } fn get(&self, index: &u32) -> Result, MapError> { diff --git a/aya/src/maps/array/program_array.rs b/aya/src/maps/array/program_array.rs index 27566e28..b8a57cf5 100644 --- a/aya/src/maps/array/program_array.rs +++ b/aya/src/maps/array/program_array.rs @@ -1,7 +1,7 @@ //! An array of eBPF program file descriptors used as a jump table. use std::{ - convert::{AsMut, AsRef}, + borrow::{Borrow, BorrowMut}, os::unix::prelude::{AsRawFd, RawFd}, }; @@ -51,9 +51,9 @@ pub struct ProgramArray { inner: T, } -impl> ProgramArray { +impl> ProgramArray { pub(crate) fn new(map: T) -> Result, MapError> { - let data = map.as_ref(); + let data = map.borrow(); check_kv_size::(data)?; let _fd = data.fd_or_err()?; @@ -64,17 +64,17 @@ impl> ProgramArray { /// An iterator over the indices of the array that point to a program. The iterator item type /// is `Result`. pub fn indices(&self) -> MapKeys<'_, u32> { - MapKeys::new(self.inner.as_ref()) + MapKeys::new(self.inner.borrow()) } } -impl> ProgramArray { +impl> ProgramArray { /// Sets the target program file descriptor for the given index in the jump table. /// /// When an eBPF program calls `bpf_tail_call(ctx, prog_array, index)`, control /// flow will jump to `program`. pub fn set(&mut self, index: u32, program: ProgramFd, flags: u64) -> Result<(), MapError> { - let data = self.inner.as_mut(); + let data = self.inner.borrow_mut(); check_bounds(data, index)?; let fd = data.fd_or_err()?; let prog_fd = program.as_raw_fd(); @@ -93,9 +93,9 @@ impl> ProgramArray { /// Calling `bpf_tail_call(ctx, prog_array, index)` on an index that has been cleared returns an /// error. pub fn clear_index(&mut self, index: &u32) -> Result<(), MapError> { - let data = self.inner.as_mut(); + let data = self.inner.borrow_mut(); check_bounds(data, *index)?; - let fd = self.inner.as_mut().fd_or_err()?; + let fd = self.inner.borrow_mut().fd_or_err()?; bpf_map_delete_elem(fd, index) .map(|_| ()) diff --git a/aya/src/maps/bloom_filter.rs b/aya/src/maps/bloom_filter.rs index 16eb1aa3..7a5ad59a 100644 --- a/aya/src/maps/bloom_filter.rs +++ b/aya/src/maps/bloom_filter.rs @@ -1,5 +1,5 @@ //! A Bloom Filter. -use std::{borrow::Borrow, convert::AsRef, marker::PhantomData}; +use std::{borrow::Borrow, marker::PhantomData}; use crate::{ maps::{check_v_size, MapData, MapError}, @@ -35,9 +35,9 @@ pub struct BloomFilter { _v: PhantomData, } -impl, V: Pod> BloomFilter { +impl, V: Pod> BloomFilter { pub(crate) fn new(map: T) -> Result, MapError> { - let data = map.as_ref(); + let data = map.borrow(); check_v_size::(data)?; let _ = data.fd_or_err()?; @@ -50,7 +50,7 @@ impl, V: Pod> BloomFilter { /// Query the existence of the element. pub fn contains(&self, mut value: &V, flags: u64) -> Result<(), MapError> { - let fd = self.inner.as_ref().fd_or_err()?; + let fd = self.inner.borrow().fd_or_err()?; bpf_map_lookup_elem_ptr::(fd, None, &mut value, flags) .map_err(|(_, io_error)| MapError::SyscallError { @@ -63,7 +63,7 @@ impl, V: Pod> BloomFilter { /// Inserts a value into the map. pub fn insert(&self, value: impl Borrow, flags: u64) -> Result<(), MapError> { - let fd = self.inner.as_ref().fd_or_err()?; + let fd = self.inner.borrow().fd_or_err()?; bpf_map_push_elem(fd, value.borrow(), flags).map_err(|(_, io_error)| { MapError::SyscallError { call: "bpf_map_push_elem".to_owned(), diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index a67cdead..97ec6c39 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -1,6 +1,5 @@ use std::{ - borrow::Borrow, - convert::{AsMut, AsRef}, + borrow::{Borrow, BorrowMut}, marker::PhantomData, }; @@ -39,9 +38,9 @@ pub struct HashMap { _v: PhantomData, } -impl, K: Pod, V: Pod> HashMap { +impl, K: Pod, V: Pod> HashMap { pub(crate) fn new(map: T) -> Result, MapError> { - let data = map.as_ref(); + let data = map.borrow(); check_kv_size::(data)?; let _ = data.fd_or_err()?; @@ -54,7 +53,7 @@ impl, K: Pod, V: Pod> HashMap { /// Returns a copy of the value associated with the key. pub fn get(&self, key: &K, flags: u64) -> Result { - let fd = self.inner.as_ref().fd_or_err()?; + let fd = self.inner.borrow().fd_or_err()?; let value = bpf_map_lookup_elem(fd, key, flags).map_err(|(_, io_error)| { MapError::SyscallError { call: "bpf_map_lookup_elem".to_owned(), @@ -73,11 +72,11 @@ impl, K: Pod, V: Pod> HashMap { /// An iterator visiting all keys in arbitrary order. The iterator element /// type is `Result`. pub fn keys(&self) -> MapKeys<'_, K> { - MapKeys::new(self.inner.as_ref()) + MapKeys::new(self.inner.borrow()) } } -impl, K: Pod, V: Pod> HashMap { +impl, K: Pod, V: Pod> HashMap { /// Inserts a key-value pair into the map. pub fn insert( &mut self, @@ -85,18 +84,18 @@ impl, K: Pod, V: Pod> HashMap { value: impl Borrow, flags: u64, ) -> Result<(), MapError> { - hash_map::insert(self.inner.as_mut(), key.borrow(), value.borrow(), flags) + hash_map::insert(self.inner.borrow_mut(), key.borrow(), value.borrow(), flags) } /// Removes a key from the map. pub fn remove(&mut self, key: &K) -> Result<(), MapError> { - hash_map::remove(self.inner.as_mut(), key) + hash_map::remove(self.inner.borrow_mut(), key) } } -impl, K: Pod, V: Pod> IterableMap for HashMap { +impl, K: Pod, V: Pod> IterableMap for HashMap { fn map(&self) -> &MapData { - self.inner.as_ref() + self.inner.borrow() } fn get(&self, key: &K) -> Result { diff --git a/aya/src/maps/hash_map/per_cpu_hash_map.rs b/aya/src/maps/hash_map/per_cpu_hash_map.rs index eb7fe6ae..eea8ed01 100644 --- a/aya/src/maps/hash_map/per_cpu_hash_map.rs +++ b/aya/src/maps/hash_map/per_cpu_hash_map.rs @@ -1,7 +1,6 @@ //! Per-CPU hash map. use std::{ - borrow::Borrow, - convert::{AsMut, AsRef}, + borrow::{Borrow, BorrowMut}, marker::PhantomData, }; @@ -48,9 +47,9 @@ pub struct PerCpuHashMap { _v: PhantomData, } -impl, K: Pod, V: Pod> PerCpuHashMap { +impl, K: Pod, V: Pod> PerCpuHashMap { pub(crate) fn new(map: T) -> Result, MapError> { - let data = map.as_ref(); + let data = map.borrow(); check_kv_size::(data)?; let _ = data.fd_or_err()?; @@ -64,7 +63,7 @@ impl, K: Pod, V: Pod> PerCpuHashMap { /// Returns a slice of values - one for each CPU - associated with the key. pub fn get(&self, key: &K, flags: u64) -> Result, MapError> { - let fd = self.inner.as_ref().fd_or_err()?; + let fd = self.inner.borrow().fd_or_err()?; let values = bpf_map_lookup_elem_per_cpu(fd, key, flags).map_err(|(_, io_error)| { MapError::SyscallError { call: "bpf_map_lookup_elem".to_owned(), @@ -83,11 +82,11 @@ impl, K: Pod, V: Pod> PerCpuHashMap { /// An iterator visiting all keys in arbitrary order. The iterator element /// type is `Result`. pub fn keys(&self) -> MapKeys<'_, K> { - MapKeys::new(self.inner.as_ref()) + MapKeys::new(self.inner.borrow()) } } -impl, K: Pod, V: Pod> PerCpuHashMap { +impl, K: Pod, V: Pod> PerCpuHashMap { /// Inserts a slice of values - one for each CPU - for the given key. /// /// # Examples @@ -122,7 +121,7 @@ impl, K: Pod, V: Pod> PerCpuHashMap { values: PerCpuValues, flags: u64, ) -> Result<(), MapError> { - let fd = self.inner.as_mut().fd_or_err()?; + let fd = self.inner.borrow_mut().fd_or_err()?; bpf_map_update_elem_per_cpu(fd, key.borrow(), &values, flags).map_err( |(_, io_error)| MapError::SyscallError { call: "bpf_map_update_elem".to_owned(), @@ -135,13 +134,15 @@ impl, K: Pod, V: Pod> PerCpuHashMap { /// Removes a key from the map. pub fn remove(&mut self, key: &K) -> Result<(), MapError> { - hash_map::remove(self.inner.as_mut(), key) + hash_map::remove(self.inner.borrow_mut(), key) } } -impl, K: Pod, V: Pod> IterableMap> for PerCpuHashMap { +impl, K: Pod, V: Pod> IterableMap> + for PerCpuHashMap +{ fn map(&self) -> &MapData { - self.inner.as_ref() + self.inner.borrow() } fn get(&self, key: &K) -> Result, MapError> { diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index 4a6cc94f..ad38d2bd 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -1,7 +1,6 @@ //! A LPM Trie. use std::{ - borrow::Borrow, - convert::{AsMut, AsRef}, + borrow::{Borrow, BorrowMut}, marker::PhantomData, }; @@ -99,9 +98,9 @@ impl Clone for Key { // A Pod impl is required as Key struct is a key for a map. unsafe impl Pod for Key {} -impl, K: Pod, V: Pod> LpmTrie { +impl, K: Pod, V: Pod> LpmTrie { pub(crate) fn new(map: T) -> Result, MapError> { - let data = map.as_ref(); + let data = map.borrow(); check_kv_size::, V>(data)?; let _ = data.fd_or_err()?; @@ -115,7 +114,7 @@ impl, K: Pod, V: Pod> LpmTrie { /// Returns a copy of the value associated with the longest prefix matching key in the LpmTrie. pub fn get(&self, key: &Key, flags: u64) -> Result { - let fd = self.inner.as_ref().fd_or_err()?; + let fd = self.inner.borrow().fd_or_err()?; let value = bpf_map_lookup_elem(fd, key, flags).map_err(|(_, io_error)| { MapError::SyscallError { call: "bpf_map_lookup_elem".to_owned(), @@ -134,17 +133,17 @@ impl, K: Pod, V: Pod> LpmTrie { /// An iterator visiting all keys in arbitrary order. The iterator element /// type is `Result, MapError>`. pub fn keys(&self) -> MapKeys<'_, Key> { - MapKeys::new(self.inner.as_ref()) + MapKeys::new(self.inner.borrow()) } /// An iterator visiting all keys matching key. The /// iterator item type is `Result, MapError>`. pub fn iter_key(&self, key: Key) -> LpmTrieKeys<'_, K> { - LpmTrieKeys::new(self.inner.as_ref(), key) + LpmTrieKeys::new(self.inner.borrow(), key) } } -impl, K: Pod, V: Pod> LpmTrie { +impl, K: Pod, V: Pod> LpmTrie { /// Inserts a key value pair into the map. pub fn insert( &mut self, @@ -152,7 +151,7 @@ impl, K: Pod, V: Pod> LpmTrie { value: impl Borrow, flags: u64, ) -> Result<(), MapError> { - let fd = self.inner.as_mut().fd_or_err()?; + let fd = self.inner.borrow().fd_or_err()?; bpf_map_update_elem(fd, Some(key), value.borrow(), flags).map_err(|(_, io_error)| { MapError::SyscallError { call: "bpf_map_update_elem".to_owned(), @@ -167,7 +166,7 @@ impl, K: Pod, V: Pod> LpmTrie { /// /// Both the prefix and data must match exactly - this method does not do a longest prefix match. pub fn remove(&mut self, key: &Key) -> Result<(), MapError> { - let fd = self.inner.as_mut().fd_or_err()?; + let fd = self.inner.borrow().fd_or_err()?; bpf_map_delete_elem(fd, key) .map(|_| ()) .map_err(|(_, io_error)| MapError::SyscallError { @@ -177,9 +176,9 @@ impl, K: Pod, V: Pod> LpmTrie { } } -impl, K: Pod, V: Pod> IterableMap, V> for LpmTrie { +impl, K: Pod, V: Pod> IterableMap, V> for LpmTrie { fn map(&self) -> &MapData { - self.inner.as_ref() + self.inner.borrow() } fn get(&self, key: &Key) -> Result { diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index b7eafb6d..77f551ca 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -37,7 +37,6 @@ //! versa. Because of that, all map values must be plain old data and therefore //! implement the [Pod] trait. use std::{ - convert::{AsMut, AsRef}, ffi::CString, fmt, io, marker::PhantomData, @@ -481,18 +480,6 @@ pub struct MapData { pub pinned: bool, } -impl AsRef for MapData { - fn as_ref(&self) -> &MapData { - self - } -} - -impl AsMut for MapData { - fn as_mut(&mut self) -> &mut MapData { - self - } -} - impl MapData { /// Creates a new map with the provided `name` pub fn create(&mut self, name: &str) -> Result { diff --git a/aya/src/maps/perf/async_perf_event_array.rs b/aya/src/maps/perf/async_perf_event_array.rs index 39ffcfa1..7e55889a 100644 --- a/aya/src/maps/perf/async_perf_event_array.rs +++ b/aya/src/maps/perf/async_perf_event_array.rs @@ -1,6 +1,6 @@ use bytes::BytesMut; use std::{ - convert::AsMut, + borrow::{Borrow, BorrowMut}, os::unix::prelude::{AsRawFd, RawFd}, }; @@ -89,7 +89,7 @@ pub struct AsyncPerfEventArray { perf_map: PerfEventArray, } -impl + AsRef> AsyncPerfEventArray { +impl + Borrow> AsyncPerfEventArray { /// Opens the perf buffer at the given index. /// /// The returned buffer will receive all the events eBPF programs send at the given index. @@ -112,7 +112,7 @@ impl + AsRef> AsyncPerfEventArray { } } -impl> AsyncPerfEventArray { +impl> AsyncPerfEventArray { pub(crate) fn new(map: T) -> Result, MapError> { Ok(AsyncPerfEventArray { perf_map: PerfEventArray::new(map)?, @@ -138,7 +138,7 @@ pub struct AsyncPerfEventArrayBuffer { } #[cfg(any(feature = "async_tokio"))] -impl + AsRef> AsyncPerfEventArrayBuffer { +impl + Borrow> AsyncPerfEventArrayBuffer { /// Reads events from the buffer. /// /// This method reads events into the provided slice of buffers, filling @@ -168,7 +168,7 @@ impl + AsRef> AsyncPerfEventArrayBuffer { } #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))] -impl + AsRef> AsyncPerfEventArrayBuffer { +impl + Borrow> AsyncPerfEventArrayBuffer { /// Reads events from the buffer. /// /// This method reads events into the provided slice of buffers, filling diff --git a/aya/src/maps/perf/perf_event_array.rs b/aya/src/maps/perf/perf_event_array.rs index 8dafd7f1..c241a37b 100644 --- a/aya/src/maps/perf/perf_event_array.rs +++ b/aya/src/maps/perf/perf_event_array.rs @@ -2,7 +2,7 @@ //! //! [`perf`]: https://perf.wiki.kernel.org/index.php/Main_Page. use std::{ - convert::AsMut, + borrow::{Borrow, BorrowMut}, ops::Deref, os::unix::io::{AsRawFd, RawFd}, sync::Arc, @@ -31,7 +31,7 @@ pub struct PerfEventArrayBuffer { buf: PerfBuffer, } -impl + AsRef> PerfEventArrayBuffer { +impl + Borrow> PerfEventArrayBuffer { /// Returns true if the buffer contains events that haven't been read. pub fn readable(&self) -> bool { self.buf.readable() @@ -55,7 +55,7 @@ impl + AsRef> PerfEventArrayBuffer { } } -impl + AsRef> AsRawFd for PerfEventArrayBuffer { +impl + Borrow> AsRawFd for PerfEventArrayBuffer { fn as_raw_fd(&self) -> RawFd { self.buf.as_raw_fd() } @@ -84,14 +84,14 @@ impl + AsRef> AsRawFd for PerfEventArrayBuffer { /// ```no_run /// # use aya::maps::perf::PerfEventArrayBuffer; /// # use aya::maps::MapData; -/// # use std::convert::AsMut; +/// # use std::borrow::BorrowMut; /// # struct Poll { _t: std::marker::PhantomData }; -/// # impl> Poll { +/// # impl> Poll { /// # fn poll_readable(&self) -> &mut [PerfEventArrayBuffer] { /// # &mut [] /// # } /// # } -/// # fn poll_buffers>(bufs: Vec>) -> Poll { +/// # fn poll_buffers>(bufs: Vec>) -> Poll { /// # Poll { _t: std::marker::PhantomData } /// # } /// # #[derive(thiserror::Error, Debug)] @@ -160,9 +160,9 @@ pub struct PerfEventArray { page_size: usize, } -impl> PerfEventArray { +impl> PerfEventArray { pub(crate) fn new(map: T) -> Result, MapError> { - let _fd = map.as_ref().fd_or_err()?; + let _fd = map.borrow().fd_or_err()?; Ok(PerfEventArray { map: Arc::new(map), @@ -171,7 +171,7 @@ impl> PerfEventArray { } } -impl + AsRef> PerfEventArray { +impl + Borrow> PerfEventArray { /// Opens the perf buffer at the given index. /// /// The returned buffer will receive all the events eBPF programs send at the given index. @@ -183,7 +183,7 @@ impl + AsRef> PerfEventArray { // FIXME: keep track of open buffers // this cannot fail as new() checks that the fd is open - let map_data: &MapData = self.map.deref().as_ref(); + let map_data: &MapData = self.map.deref().borrow(); let map_fd = map_data.fd_or_err().unwrap(); let buf = PerfBuffer::open(index, self.page_size, page_count.unwrap_or(2))?; bpf_map_update_elem(map_fd, Some(&index), &buf.as_raw_fd(), 0) diff --git a/aya/src/maps/queue.rs b/aya/src/maps/queue.rs index d10ecd04..c6810546 100644 --- a/aya/src/maps/queue.rs +++ b/aya/src/maps/queue.rs @@ -1,7 +1,6 @@ //! A FIFO queue. use std::{ - borrow::Borrow, - convert::{AsMut, AsRef}, + borrow::{Borrow, BorrowMut}, marker::PhantomData, }; @@ -34,9 +33,9 @@ pub struct Queue { _v: PhantomData, } -impl, V: Pod> Queue { +impl, V: Pod> Queue { pub(crate) fn new(map: T) -> Result, MapError> { - let data = map.as_ref(); + let data = map.borrow(); check_kv_size::<(), V>(data)?; let _fd = data.fd_or_err()?; @@ -51,11 +50,11 @@ impl, V: Pod> Queue { /// /// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side. pub fn capacity(&self) -> u32 { - self.inner.as_ref().obj.max_entries() + self.inner.borrow().obj.max_entries() } } -impl, V: Pod> Queue { +impl, V: Pod> Queue { /// Removes the first element and returns it. /// /// # Errors @@ -63,7 +62,7 @@ impl, V: Pod> Queue { /// Returns [`MapError::ElementNotFound`] if the queue is empty, [`MapError::SyscallError`] /// if `bpf_map_lookup_and_delete_elem` fails. pub fn pop(&mut self, flags: u64) -> Result { - let fd = self.inner.as_mut().fd_or_err()?; + let fd = self.inner.borrow().fd_or_err()?; let value = bpf_map_lookup_and_delete_elem::(fd, None, flags).map_err( |(_, io_error)| MapError::SyscallError { @@ -80,7 +79,7 @@ impl, V: Pod> Queue { /// /// [`MapError::SyscallError`] if `bpf_map_update_elem` fails. pub fn push(&mut self, value: impl Borrow, flags: u64) -> Result<(), MapError> { - let fd = self.inner.as_mut().fd_or_err()?; + let fd = self.inner.borrow().fd_or_err()?; bpf_map_push_elem(fd, value.borrow(), flags).map_err(|(_, io_error)| { MapError::SyscallError { call: "bpf_map_push_elem".to_owned(), diff --git a/aya/src/maps/sock/sock_hash.rs b/aya/src/maps/sock/sock_hash.rs index 7da2d097..244dcd1c 100644 --- a/aya/src/maps/sock/sock_hash.rs +++ b/aya/src/maps/sock/sock_hash.rs @@ -1,6 +1,5 @@ use std::{ - borrow::Borrow, - convert::{AsMut, AsRef}, + borrow::{Borrow, BorrowMut}, marker::PhantomData, os::unix::io::{AsRawFd, RawFd}, }; @@ -69,9 +68,9 @@ pub struct SockHash { _k: PhantomData, } -impl, K: Pod> SockHash { +impl, K: Pod> SockHash { pub(crate) fn new(map: T) -> Result, MapError> { - let data = map.as_ref(); + let data = map.borrow(); check_kv_size::(data)?; let _ = data.fd_or_err()?; @@ -83,7 +82,7 @@ impl, K: Pod> SockHash { /// Returns the fd of the socket stored at the given key. pub fn get(&self, key: &K, flags: u64) -> Result { - let fd = self.inner.as_ref().fd_or_err()?; + let fd = self.inner.borrow().fd_or_err()?; let value = bpf_map_lookup_elem(fd, key, flags).map_err(|(_, io_error)| { MapError::SyscallError { call: "bpf_map_lookup_elem".to_owned(), @@ -102,7 +101,7 @@ impl, K: Pod> SockHash { /// An iterator visiting all keys in arbitrary order. The iterator element /// type is `Result`. pub fn keys(&self) -> MapKeys<'_, K> { - MapKeys::new(self.inner.as_ref()) + MapKeys::new(self.inner.borrow()) } /// Returns the map's file descriptor. @@ -110,11 +109,11 @@ impl, K: Pod> SockHash { /// The returned file descriptor can be used to attach programs that work with /// socket maps, like [`SkMsg`](crate::programs::SkMsg) and [`SkSkb`](crate::programs::SkSkb). pub fn fd(&self) -> Result { - Ok(SockMapFd(self.inner.as_ref().fd_or_err()?)) + Ok(SockMapFd(self.inner.borrow().fd_or_err()?)) } } -impl, K: Pod> SockHash { +impl, K: Pod> SockHash { /// Inserts a socket under the given key. pub fn insert( &mut self, @@ -122,18 +121,23 @@ impl, K: Pod> SockHash { value: I, flags: u64, ) -> Result<(), MapError> { - hash_map::insert(self.inner.as_mut(), key.borrow(), &value.as_raw_fd(), flags) + hash_map::insert( + self.inner.borrow_mut(), + key.borrow(), + &value.as_raw_fd(), + flags, + ) } /// Removes a socket from the map. pub fn remove(&mut self, key: &K) -> Result<(), MapError> { - hash_map::remove(self.inner.as_mut(), key) + hash_map::remove(self.inner.borrow_mut(), key) } } -impl, K: Pod> IterableMap for SockHash { +impl, K: Pod> IterableMap for SockHash { fn map(&self) -> &MapData { - self.inner.as_ref() + self.inner.borrow() } fn get(&self, key: &K) -> Result { diff --git a/aya/src/maps/sock/sock_map.rs b/aya/src/maps/sock/sock_map.rs index 28c5b404..32625b41 100644 --- a/aya/src/maps/sock/sock_map.rs +++ b/aya/src/maps/sock/sock_map.rs @@ -1,7 +1,7 @@ //! An array of eBPF program file descriptors used as a jump table. use std::{ - convert::{AsMut, AsRef}, + borrow::{Borrow, BorrowMut}, os::unix::{io::AsRawFd, prelude::RawFd}, }; @@ -44,9 +44,9 @@ pub struct SockMap { pub(crate) inner: T, } -impl> SockMap { +impl> SockMap { pub(crate) fn new(map: T) -> Result, MapError> { - let data = map.as_ref(); + let data = map.borrow(); check_kv_size::(data)?; let _fd = data.fd_or_err()?; @@ -57,7 +57,7 @@ impl> SockMap { /// An iterator over the indices of the array that point to a program. The iterator item type /// is `Result`. pub fn indices(&self) -> MapKeys<'_, u32> { - MapKeys::new(self.inner.as_ref()) + MapKeys::new(self.inner.borrow()) } /// Returns the map's file descriptor. @@ -65,14 +65,14 @@ impl> SockMap { /// The returned file descriptor can be used to attach programs that work with /// socket maps, like [`SkMsg`](crate::programs::SkMsg) and [`SkSkb`](crate::programs::SkSkb). pub fn fd(&self) -> Result { - Ok(SockMapFd(self.inner.as_ref().fd_or_err()?)) + Ok(SockMapFd(self.inner.borrow().fd_or_err()?)) } } -impl> SockMap { +impl> SockMap { /// Stores a socket into the map. pub fn set(&mut self, index: u32, socket: &I, flags: u64) -> Result<(), MapError> { - let data = self.inner.as_mut(); + let data = self.inner.borrow_mut(); let fd = data.fd_or_err()?; check_bounds(data, index)?; bpf_map_update_elem(fd, Some(&index), &socket.as_raw_fd(), flags).map_err( @@ -86,7 +86,7 @@ impl> SockMap { /// Removes the socket stored at `index` from the map. pub fn clear_index(&mut self, index: &u32) -> Result<(), MapError> { - let data = self.inner.as_mut(); + let data = self.inner.borrow_mut(); let fd = data.fd_or_err()?; check_bounds(data, *index)?; bpf_map_delete_elem(fd, index) diff --git a/aya/src/maps/stack.rs b/aya/src/maps/stack.rs index c0742cfa..db428ddf 100644 --- a/aya/src/maps/stack.rs +++ b/aya/src/maps/stack.rs @@ -1,7 +1,6 @@ //! A LIFO stack. use std::{ - borrow::Borrow, - convert::{AsMut, AsRef}, + borrow::{Borrow, BorrowMut}, marker::PhantomData, }; @@ -34,9 +33,9 @@ pub struct Stack { _v: PhantomData, } -impl, V: Pod> Stack { +impl, V: Pod> Stack { pub(crate) fn new(map: T) -> Result, MapError> { - let data = map.as_ref(); + let data = map.borrow(); check_kv_size::<(), V>(data)?; let _fd = data.fd_or_err()?; @@ -51,11 +50,11 @@ impl, V: Pod> Stack { /// /// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side. pub fn capacity(&self) -> u32 { - self.inner.as_ref().obj.max_entries() + self.inner.borrow().obj.max_entries() } } -impl, V: Pod> Stack { +impl, V: Pod> Stack { /// Removes the last element and returns it. /// /// # Errors @@ -63,7 +62,7 @@ impl, V: Pod> Stack { /// Returns [`MapError::ElementNotFound`] if the stack is empty, [`MapError::SyscallError`] /// if `bpf_map_lookup_and_delete_elem` fails. pub fn pop(&mut self, flags: u64) -> Result { - let fd = self.inner.as_mut().fd_or_err()?; + let fd = self.inner.borrow().fd_or_err()?; let value = bpf_map_lookup_and_delete_elem::(fd, None, flags).map_err( |(_, io_error)| MapError::SyscallError { @@ -80,7 +79,7 @@ impl, V: Pod> Stack { /// /// [`MapError::SyscallError`] if `bpf_map_update_elem` fails. pub fn push(&mut self, value: impl Borrow, flags: u64) -> Result<(), MapError> { - let fd = self.inner.as_mut().fd_or_err()?; + let fd = self.inner.borrow().fd_or_err()?; bpf_map_update_elem(fd, None::<&u32>, value.borrow(), flags).map_err(|(_, io_error)| { MapError::SyscallError { call: "bpf_map_update_elem".to_owned(), diff --git a/aya/src/maps/stack_trace.rs b/aya/src/maps/stack_trace.rs index 135387e4..a8cce414 100644 --- a/aya/src/maps/stack_trace.rs +++ b/aya/src/maps/stack_trace.rs @@ -1,7 +1,7 @@ //! A hash map of kernel or user space stack traces. //! //! See [`StackTraceMap`] for documentation and examples. -use std::{collections::BTreeMap, convert::AsRef, fs, io, mem, path::Path, str::FromStr}; +use std::{borrow::Borrow, collections::BTreeMap, fs, io, mem, path::Path, str::FromStr}; use crate::{ maps::{IterableMap, MapData, MapError, MapIter, MapKeys}, @@ -67,9 +67,9 @@ pub struct StackTraceMap { max_stack_depth: usize, } -impl> StackTraceMap { +impl> StackTraceMap { pub(crate) fn new(map: T) -> Result, MapError> { - let data = map.as_ref(); + let data = map.borrow(); let expected = mem::size_of::(); let size = data.obj.key_size() as usize; if size != expected { @@ -102,7 +102,7 @@ impl> StackTraceMap { /// Returns [`MapError::KeyNotFound`] if there is no stack trace with the /// given `stack_id`, or [`MapError::SyscallError`] if `bpf_map_lookup_elem` fails. pub fn get(&self, stack_id: &u32, flags: u64) -> Result { - let fd = self.inner.as_ref().fd_or_err()?; + let fd = self.inner.borrow().fd_or_err()?; let mut frames = vec![0; self.max_stack_depth]; bpf_map_lookup_elem_ptr(fd, Some(stack_id), frames.as_mut_ptr(), flags) @@ -136,13 +136,13 @@ impl> StackTraceMap { /// An iterator visiting all the stack_ids in arbitrary order. The iterator element /// type is `Result`. pub fn stack_ids(&self) -> MapKeys<'_, u32> { - MapKeys::new(self.inner.as_ref()) + MapKeys::new(self.inner.borrow()) } } -impl> IterableMap for StackTraceMap { +impl> IterableMap for StackTraceMap { fn map(&self) -> &MapData { - self.inner.as_ref() + self.inner.borrow() } fn get(&self, index: &u32) -> Result { @@ -150,7 +150,7 @@ impl> IterableMap for StackTraceMap { } } -impl<'a, T: AsRef> IntoIterator for &'a StackTraceMap { +impl<'a, T: Borrow> IntoIterator for &'a StackTraceMap { type Item = Result<(u32, StackTrace), MapError>; type IntoIter = MapIter<'a, u32, StackTrace, StackTraceMap>;