From 939d16cce5dcfeaebc5d571d21127105fc886186 Mon Sep 17 00:00:00 2001 From: Andrew Stoycos Date: Mon, 17 Oct 2022 17:19:20 -0400 Subject: [PATCH] Fixups 2 Respond to more review comments: Revert to try_from in doctests so we don't need to explicitly specify type parameters. Fixup some documentation Remove explit types in `try_from` methods Signed-off-by: Andrew Stoycos --- aya/src/maps/array/array.rs | 2 +- aya/src/maps/array/per_cpu_array.rs | 2 +- aya/src/maps/array/program_array.rs | 2 +- aya/src/maps/bloom_filter.rs | 2 +- aya/src/maps/hash_map/hash_map.rs | 2 +- aya/src/maps/hash_map/per_cpu_hash_map.rs | 6 +++--- aya/src/maps/lpm_trie.rs | 2 +- aya/src/maps/mod.rs | 18 +++++++++--------- aya/src/maps/perf/async_perf_event_array.rs | 2 +- aya/src/maps/perf/perf_event_array.rs | 2 +- aya/src/maps/queue.rs | 2 +- aya/src/maps/sock/sock_hash.rs | 10 ++++++---- aya/src/maps/sock/sock_map.rs | 8 +++++--- aya/src/maps/stack.rs | 2 +- 14 files changed, 33 insertions(+), 29 deletions(-) diff --git a/aya/src/maps/array/array.rs b/aya/src/maps/array/array.rs index d661be18..0074e2c4 100644 --- a/aya/src/maps/array/array.rs +++ b/aya/src/maps/array/array.rs @@ -23,7 +23,7 @@ use crate::{ /// # let mut bpf = aya::Bpf::load(&[])?; /// use aya::maps::Array; /// -/// let mut array: Array<_, u32> = bpf.map_mut("ARRAY")?.try_into()?; +/// let mut array = Array::try_from(bpf.map_mut("ARRAY")?)?; /// array.set(1, 42, 0)?; /// assert_eq!(array.get(&1, 0)?, 42); /// # Ok::<(), aya::BpfError>(()) diff --git a/aya/src/maps/array/per_cpu_array.rs b/aya/src/maps/array/per_cpu_array.rs index d9a4f73c..2692eac2 100644 --- a/aya/src/maps/array/per_cpu_array.rs +++ b/aya/src/maps/array/per_cpu_array.rs @@ -33,7 +33,7 @@ use crate::{ /// use aya::maps::{PerCpuArray, PerCpuValues}; /// use aya::util::nr_cpus; /// -/// let mut array: PerCpuArray<_,u32> = bpf.map_mut("ARRAY")?.try_into()?; +/// let mut array = PerCpuArray::try_from(bpf.map_mut("ARRAY")?)?; /// /// // set array[1] = 42 for all cpus /// let nr_cpus = nr_cpus()?; diff --git a/aya/src/maps/array/program_array.rs b/aya/src/maps/array/program_array.rs index 5245d001..b4cbbbf7 100644 --- a/aya/src/maps/array/program_array.rs +++ b/aya/src/maps/array/program_array.rs @@ -27,7 +27,7 @@ use crate::{ /// use aya::maps::ProgramArray; /// use aya::programs::CgroupSkb; /// -/// let mut prog_array: ProgramArray<_> = bpf.take_map("JUMP_TABLE")?.try_into()?; +/// let mut prog_array = ProgramArray::try_from(bpf.take_map("JUMP_TABLE")?)?; /// let prog_0: &CgroupSkb = bpf.program("example_prog_0").unwrap().try_into()?; /// let prog_0_fd = prog_0.fd().unwrap(); /// let prog_1: &CgroupSkb = bpf.program("example_prog_1").unwrap().try_into()?; diff --git a/aya/src/maps/bloom_filter.rs b/aya/src/maps/bloom_filter.rs index 7a540367..b97f8720 100644 --- a/aya/src/maps/bloom_filter.rs +++ b/aya/src/maps/bloom_filter.rs @@ -19,7 +19,7 @@ use crate::{ /// # let mut bpf = aya::Bpf::load(&[])?; /// use aya::maps::bloom_filter::BloomFilter; /// -/// let mut bloom_filter: BloomFilter<_,u32> = bpf.map_mut("BLOOM_FILTER")?.try_into()?; +/// let mut bloom_filter = BloomFilter::try_from(bpf.map_mut("BLOOM_FILTER")?)?; /// /// bloom_filter.insert(1, 0)?; /// diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index 474337bd..42e5f596 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -21,7 +21,7 @@ use crate::{ /// # let mut bpf = aya::Bpf::load(&[])?; /// use aya::maps::HashMap; /// -/// let mut redirect_ports: HashMap<_, u32, u32> = bpf.map_mut("REDIRECT_PORTS")?.try_into()?; +/// let mut redirect_ports = HashMap::try_from(bpf.map_mut("REDIRECT_PORTS")?)?; /// /// // redirect port 80 to 8080 /// redirect_ports.insert(80, 8080, 0); 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 946ab63a..b94bf8ba 100644 --- a/aya/src/maps/hash_map/per_cpu_hash_map.rs +++ b/aya/src/maps/hash_map/per_cpu_hash_map.rs @@ -25,13 +25,13 @@ use crate::{ /// # Examples /// /// ```no_run -/// # let bpf = aya::Bpf::load(&[])?; +/// # let mut bpf = aya::Bpf::load(&[])?; /// use aya::maps::PerCpuHashMap; /// /// const CPU_IDS: u8 = 1; /// const WAKEUPS: u8 = 2; /// -/// let mut hm = PerCpuHashMap::<_, u8, u32>::try_from(bpf.map("COUNTERS")?)?; +/// let mut hm = PerCpuHashMap::<_, u8, u32>::try_from(bpf.map_mut("PER_CPU_STORAGE")?)?; /// let cpu_ids = unsafe { hm.get(&CPU_IDS, 0)? }; /// let wakeups = unsafe { hm.get(&WAKEUPS, 0)? }; /// for (cpu_id, wakeups) in cpu_ids.iter().zip(wakeups.iter()) { @@ -107,7 +107,7 @@ impl, K: Pod, V: Pod> PerCpuHashMap { /// /// const RETRIES: u8 = 1; /// - /// let mut hm: PerCpuHashMap::<_, u8, u32> = bpf.map_mut("PER_CPU_STORAGE")?.try_into()?; + /// let mut hm = PerCpuHashMap::<_, u8, u32>::try_from(bpf.map_mut("PER_CPU_STORAGE")?)?; /// hm.insert( /// RETRIES, /// PerCpuValues::try_from(vec![3u32; nr_cpus()?])?, diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index ae479529..047b36b1 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -24,7 +24,7 @@ use crate::{ /// use aya::maps::lpm_trie::{LpmTrie, Key}; /// use std::net::Ipv4Addr; /// -/// let mut trie: LpmTrie<_,u32,u32> = bpf.map_mut("LPM_TRIE")?.try_into()?; +/// let mut trie = LpmTrie::try_from(bpf.map_mut("LPM_TRIE")?)?; /// let ipaddr = Ipv4Addr::new(8, 8, 8, 8); /// // The following represents a key for the "8.8.8.8/16" subnet. /// // The first argument - the prefix length - represents how many bytes should be matched against. The second argument is the actual data to be matched. diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 29bf0658..94484cc9 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -22,7 +22,7 @@ //! use aya::maps::SockMap; //! use aya::programs::SkMsg; //! -//! let mut intercept_egress: SockMap<_> = bpf.map_mut("INTERCEPT_EGRESS")?.try_into()?; +//! let intercept_egress = SockMap::try_from(bpf.map_mut("INTERCEPT_EGRESS")?)?; //! let map_fd = intercept_egress.fd()?; //! let prog: &mut SkMsg = bpf.program_mut("intercept_egress_packet").unwrap().try_into()?; //! prog.load()?; @@ -281,7 +281,7 @@ macro_rules! impl_try_from_map { fn try_from(map: &'a Map) -> Result<$tx<&'a MapData>, MapError> { match map { Map::$ty(m) => { - $tx::<&'a MapData>::new(m) + $tx::new(m) }, _ => Err(MapError::UnexpectedMapType), } @@ -294,7 +294,7 @@ macro_rules! impl_try_from_map { fn try_from(map: &'a mut Map) -> Result<$tx<&'a mut MapData>, MapError> { match map { Map::$ty(m) => { - $tx::<&'a mut MapData>::new(m) + $tx::new(m) }, _ => Err(MapError::UnexpectedMapType), } @@ -307,7 +307,7 @@ macro_rules! impl_try_from_map { fn try_from(map: Map) -> Result<$tx, MapError> { match map { Map::$ty(m) => { - $tx::::new(m) + $tx::new(m) }, _ => Err(MapError::UnexpectedMapType), } @@ -339,7 +339,7 @@ macro_rules! impl_try_from_map_generic_key_or_value { fn try_from(map: &'a Map) -> Result<$ty<&'a MapData , V>, MapError> { match map { Map::$ty(m) => { - $ty::<&'a MapData,V>::new(m) + $ty::new(m) }, _ => Err(MapError::UnexpectedMapType), } @@ -352,7 +352,7 @@ macro_rules! impl_try_from_map_generic_key_or_value { fn try_from(map: &'a mut Map) -> Result<$ty<&'a mut MapData, V>, MapError> { match map { Map::$ty(m) => { - $ty::<&'a mut MapData,V>::new(m) + $ty::new(m) }, _ => Err(MapError::UnexpectedMapType), } @@ -365,7 +365,7 @@ macro_rules! impl_try_from_map_generic_key_or_value { fn try_from(map: Map) -> Result<$ty, MapError> { match map { Map::$ty(m) => { - $ty::::new(m) + $ty::new(m) }, _ => Err(MapError::UnexpectedMapType), } @@ -386,7 +386,7 @@ macro_rules! impl_try_from_map_generic_key_and_value { fn try_from(map: &'a Map) -> Result<$ty<&'a MapData,V,K>, MapError> { match map { Map::$ty(m) => { - $ty::<&'a MapData,V,K>::new(m) + $ty::new(m) }, _ => Err(MapError::UnexpectedMapType), } @@ -399,7 +399,7 @@ macro_rules! impl_try_from_map_generic_key_and_value { fn try_from(map: &'a mut Map) -> Result<$ty<&'a mut MapData, V, K>, MapError> { match map { Map::$ty(m) => { - $ty::<&'a mut MapData,V,K>::new(m) + $ty::new(m) }, _ => Err(MapError::UnexpectedMapType), } diff --git a/aya/src/maps/perf/async_perf_event_array.rs b/aya/src/maps/perf/async_perf_event_array.rs index 651d503a..1ca14be5 100644 --- a/aya/src/maps/perf/async_perf_event_array.rs +++ b/aya/src/maps/perf/async_perf_event_array.rs @@ -53,7 +53,7 @@ use crate::maps::{ /// use tokio::task; // or async_std::task /// /// // try to convert the PERF_ARRAY map to an AsyncPerfEventArray -/// let mut perf_array: AsyncPerfEventArray<_> =bpf.take_map("PERF_ARRAY")?.try_into()?; +/// let mut perf_array = AsyncPerfEventArray::try_from(bpf.take_map("PERF_ARRAY")?)?; /// /// for cpu_id in online_cpus()? { /// // open a separate perf buffer for each cpu diff --git a/aya/src/maps/perf/perf_event_array.rs b/aya/src/maps/perf/perf_event_array.rs index 0517cb7d..7b25addd 100644 --- a/aya/src/maps/perf/perf_event_array.rs +++ b/aya/src/maps/perf/perf_event_array.rs @@ -110,7 +110,7 @@ impl + AsRef> AsRawFd for PerfEventArrayBuffer { /// use aya::util::online_cpus; /// use bytes::BytesMut; /// -/// let mut perf_array: PerfEventArray<_> = bpf.map_mut("EVENTS")?.try_into()?; +/// let mut perf_array = PerfEventArray::try_from(bpf.map_mut("EVENTS")?)?; /// /// // eBPF programs are going to write to the EVENTS perf array, using the id of the CPU they're /// // running on as the array index. diff --git a/aya/src/maps/queue.rs b/aya/src/maps/queue.rs index 8484f2df..fe882430 100644 --- a/aya/src/maps/queue.rs +++ b/aya/src/maps/queue.rs @@ -21,7 +21,7 @@ use crate::{ /// # let mut bpf = aya::Bpf::load(&[])?; /// use aya::maps::Queue; /// -/// let mut queue: Queue<_, u32> = bpf.map_mut("ARRAY")?.try_into()?; +/// let mut queue = Queue::try_from(bpf.map_mut("ARRAY")?)?; /// queue.push(42, 0)?; /// queue.push(43, 0)?; /// assert_eq!(queue.pop(0)?, 42); diff --git a/aya/src/maps/sock/sock_hash.rs b/aya/src/maps/sock/sock_hash.rs index 58ba8944..302d9df4 100644 --- a/aya/src/maps/sock/sock_hash.rs +++ b/aya/src/maps/sock/sock_hash.rs @@ -46,7 +46,7 @@ use crate::{ /// use aya::maps::SockHash; /// use aya::programs::SkMsg; /// -/// let intercept_egress: SockHash<_, u32> = bpf.map("INTERCEPT_EGRESS")?.try_into()?; +/// let mut intercept_egress = SockHash::<_, u32>::try_from(bpf.map("INTERCEPT_EGRESS")?)?; /// let map_fd = intercept_egress.fd()?; /// /// let prog: &mut SkMsg = bpf.program_mut("intercept_egress_packet").unwrap().try_into()?; @@ -54,7 +54,7 @@ use crate::{ /// prog.attach(map_fd)?; /// /// let mut client = TcpStream::connect("127.0.0.1:1234")?; -/// let mut intercept_egress: SockHash<_, u32> = bpf.map_mut("INTERCEPT_EGRESS")?.try_into()?; +/// let mut intercept_egress = SockHash::try_from(bpf.map_mut("INTERCEPT_EGRESS")?)?; /// /// intercept_egress.insert(1234, client.as_raw_fd(), 0)?; /// @@ -104,8 +104,10 @@ impl, K: Pod> SockHash { MapKeys::new(self.inner.as_ref()) } - /// Returns the map's file descriptor, used for instances where programs - /// are attached to maps. + /// Returns the map's file descriptor. + /// + /// The returned file descriptor can be used to attach programs that work with + /// socket maps, like [`SkMsg`] and [`SkSkb`]. pub fn fd(&self) -> Result { Ok(SockMapFd(self.inner.as_ref().fd_or_err()?)) } diff --git a/aya/src/maps/sock/sock_map.rs b/aya/src/maps/sock/sock_map.rs index e4b7131e..2c7efa7a 100644 --- a/aya/src/maps/sock/sock_map.rs +++ b/aya/src/maps/sock/sock_map.rs @@ -30,7 +30,7 @@ use crate::{ /// use aya::maps::SockMap; /// use aya::programs::SkSkb; /// -/// let intercept_ingress: SockMap<_> = bpf.map("INTERCEPT_INGRESS")?.try_into()?; +/// let intercept_ingress = SockMap::try_from(bpf.map("INTERCEPT_INGRESS")?)?; /// let map_fd = intercept_ingress.fd()?; /// /// let prog: &mut SkSkb = bpf.program_mut("intercept_ingress_packet").unwrap().try_into()?; @@ -60,8 +60,10 @@ impl> SockMap { MapKeys::new(self.inner.as_ref()) } - /// Returns the map's file descriptor, used for instances where programs - /// are attached to maps. + /// Returns the map's file descriptor. + /// + /// The returned file descriptor can be used to attach programs that work with + /// socket maps, like [`SkMsg`] and [`SkSkb`]. pub fn fd(&self) -> Result { Ok(SockMapFd(self.inner.as_ref().fd_or_err()?)) } diff --git a/aya/src/maps/stack.rs b/aya/src/maps/stack.rs index 135c18d3..7089d9e7 100644 --- a/aya/src/maps/stack.rs +++ b/aya/src/maps/stack.rs @@ -21,7 +21,7 @@ use crate::{ /// # let mut bpf = aya::Bpf::load(&[])?; /// use aya::maps::Stack; /// -/// let mut stack: Stack<_, u32> = bpf.map_mut("STACK")?.try_into()?; +/// let mut stack = Stack::try_from(bpf.map_mut("STACK")?)?; /// stack.push(42, 0)?; /// stack.push(43, 0)?; /// assert_eq!(stack.pop(0)?, 43);