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 <astoycos@redhat.com>
pull/397/head
Andrew Stoycos 2 years ago
parent 8009361694
commit 939d16cce5

@ -23,7 +23,7 @@ use crate::{
/// # let mut bpf = aya::Bpf::load(&[])?; /// # let mut bpf = aya::Bpf::load(&[])?;
/// use aya::maps::Array; /// 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)?; /// array.set(1, 42, 0)?;
/// assert_eq!(array.get(&1, 0)?, 42); /// assert_eq!(array.get(&1, 0)?, 42);
/// # Ok::<(), aya::BpfError>(()) /// # Ok::<(), aya::BpfError>(())

@ -33,7 +33,7 @@ use crate::{
/// use aya::maps::{PerCpuArray, PerCpuValues}; /// use aya::maps::{PerCpuArray, PerCpuValues};
/// use aya::util::nr_cpus; /// 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 /// // set array[1] = 42 for all cpus
/// let nr_cpus = nr_cpus()?; /// let nr_cpus = nr_cpus()?;

@ -27,7 +27,7 @@ use crate::{
/// use aya::maps::ProgramArray; /// use aya::maps::ProgramArray;
/// use aya::programs::CgroupSkb; /// 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: &CgroupSkb = bpf.program("example_prog_0").unwrap().try_into()?;
/// let prog_0_fd = prog_0.fd().unwrap(); /// let prog_0_fd = prog_0.fd().unwrap();
/// let prog_1: &CgroupSkb = bpf.program("example_prog_1").unwrap().try_into()?; /// let prog_1: &CgroupSkb = bpf.program("example_prog_1").unwrap().try_into()?;

@ -19,7 +19,7 @@ use crate::{
/// # let mut bpf = aya::Bpf::load(&[])?; /// # let mut bpf = aya::Bpf::load(&[])?;
/// use aya::maps::bloom_filter::BloomFilter; /// 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)?; /// bloom_filter.insert(1, 0)?;
/// ///

@ -21,7 +21,7 @@ use crate::{
/// # let mut bpf = aya::Bpf::load(&[])?; /// # let mut bpf = aya::Bpf::load(&[])?;
/// use aya::maps::HashMap; /// 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 port 80 to 8080
/// redirect_ports.insert(80, 8080, 0); /// redirect_ports.insert(80, 8080, 0);

@ -25,13 +25,13 @@ use crate::{
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # let bpf = aya::Bpf::load(&[])?; /// # let mut bpf = aya::Bpf::load(&[])?;
/// use aya::maps::PerCpuHashMap; /// use aya::maps::PerCpuHashMap;
/// ///
/// const CPU_IDS: u8 = 1; /// const CPU_IDS: u8 = 1;
/// const WAKEUPS: u8 = 2; /// 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 cpu_ids = unsafe { hm.get(&CPU_IDS, 0)? };
/// let wakeups = unsafe { hm.get(&WAKEUPS, 0)? }; /// let wakeups = unsafe { hm.get(&WAKEUPS, 0)? };
/// for (cpu_id, wakeups) in cpu_ids.iter().zip(wakeups.iter()) { /// for (cpu_id, wakeups) in cpu_ids.iter().zip(wakeups.iter()) {
@ -107,7 +107,7 @@ impl<T: AsMut<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
/// ///
/// const RETRIES: u8 = 1; /// 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( /// hm.insert(
/// RETRIES, /// RETRIES,
/// PerCpuValues::try_from(vec![3u32; nr_cpus()?])?, /// PerCpuValues::try_from(vec![3u32; nr_cpus()?])?,

@ -24,7 +24,7 @@ use crate::{
/// use aya::maps::lpm_trie::{LpmTrie, Key}; /// use aya::maps::lpm_trie::{LpmTrie, Key};
/// use std::net::Ipv4Addr; /// 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); /// let ipaddr = Ipv4Addr::new(8, 8, 8, 8);
/// // The following represents a key for the "8.8.8.8/16" subnet. /// // 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. /// // The first argument - the prefix length - represents how many bytes should be matched against. The second argument is the actual data to be matched.

@ -22,7 +22,7 @@
//! use aya::maps::SockMap; //! use aya::maps::SockMap;
//! use aya::programs::SkMsg; //! 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 map_fd = intercept_egress.fd()?;
//! let prog: &mut SkMsg = bpf.program_mut("intercept_egress_packet").unwrap().try_into()?; //! let prog: &mut SkMsg = bpf.program_mut("intercept_egress_packet").unwrap().try_into()?;
//! prog.load()?; //! prog.load()?;
@ -281,7 +281,7 @@ macro_rules! impl_try_from_map {
fn try_from(map: &'a Map) -> Result<$tx<&'a MapData>, MapError> { fn try_from(map: &'a Map) -> Result<$tx<&'a MapData>, MapError> {
match map { match map {
Map::$ty(m) => { Map::$ty(m) => {
$tx::<&'a MapData>::new(m) $tx::new(m)
}, },
_ => Err(MapError::UnexpectedMapType), _ => 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> { fn try_from(map: &'a mut Map) -> Result<$tx<&'a mut MapData>, MapError> {
match map { match map {
Map::$ty(m) => { Map::$ty(m) => {
$tx::<&'a mut MapData>::new(m) $tx::new(m)
}, },
_ => Err(MapError::UnexpectedMapType), _ => Err(MapError::UnexpectedMapType),
} }
@ -307,7 +307,7 @@ macro_rules! impl_try_from_map {
fn try_from(map: Map) -> Result<$tx<MapData>, MapError> { fn try_from(map: Map) -> Result<$tx<MapData>, MapError> {
match map { match map {
Map::$ty(m) => { Map::$ty(m) => {
$tx::<MapData>::new(m) $tx::new(m)
}, },
_ => Err(MapError::UnexpectedMapType), _ => 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> { fn try_from(map: &'a Map) -> Result<$ty<&'a MapData , V>, MapError> {
match map { match map {
Map::$ty(m) => { Map::$ty(m) => {
$ty::<&'a MapData,V>::new(m) $ty::new(m)
}, },
_ => Err(MapError::UnexpectedMapType), _ => 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> { fn try_from(map: &'a mut Map) -> Result<$ty<&'a mut MapData, V>, MapError> {
match map { match map {
Map::$ty(m) => { Map::$ty(m) => {
$ty::<&'a mut MapData,V>::new(m) $ty::new(m)
}, },
_ => Err(MapError::UnexpectedMapType), _ => Err(MapError::UnexpectedMapType),
} }
@ -365,7 +365,7 @@ macro_rules! impl_try_from_map_generic_key_or_value {
fn try_from(map: Map) -> Result<$ty<MapData, V>, MapError> { fn try_from(map: Map) -> Result<$ty<MapData, V>, MapError> {
match map { match map {
Map::$ty(m) => { Map::$ty(m) => {
$ty::<MapData,V>::new(m) $ty::new(m)
}, },
_ => Err(MapError::UnexpectedMapType), _ => 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> { fn try_from(map: &'a Map) -> Result<$ty<&'a MapData,V,K>, MapError> {
match map { match map {
Map::$ty(m) => { Map::$ty(m) => {
$ty::<&'a MapData,V,K>::new(m) $ty::new(m)
}, },
_ => Err(MapError::UnexpectedMapType), _ => 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> { fn try_from(map: &'a mut Map) -> Result<$ty<&'a mut MapData, V, K>, MapError> {
match map { match map {
Map::$ty(m) => { Map::$ty(m) => {
$ty::<&'a mut MapData,V,K>::new(m) $ty::new(m)
}, },
_ => Err(MapError::UnexpectedMapType), _ => Err(MapError::UnexpectedMapType),
} }

@ -53,7 +53,7 @@ use crate::maps::{
/// use tokio::task; // or async_std::task /// use tokio::task; // or async_std::task
/// ///
/// // try to convert the PERF_ARRAY map to an AsyncPerfEventArray /// // 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()? { /// for cpu_id in online_cpus()? {
/// // open a separate perf buffer for each cpu /// // open a separate perf buffer for each cpu

@ -110,7 +110,7 @@ impl<T: AsMut<MapData> + AsRef<MapData>> AsRawFd for PerfEventArrayBuffer<T> {
/// use aya::util::online_cpus; /// use aya::util::online_cpus;
/// use bytes::BytesMut; /// 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 /// // 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. /// // running on as the array index.

@ -21,7 +21,7 @@ use crate::{
/// # let mut bpf = aya::Bpf::load(&[])?; /// # let mut bpf = aya::Bpf::load(&[])?;
/// use aya::maps::Queue; /// 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(42, 0)?;
/// queue.push(43, 0)?; /// queue.push(43, 0)?;
/// assert_eq!(queue.pop(0)?, 42); /// assert_eq!(queue.pop(0)?, 42);

@ -46,7 +46,7 @@ use crate::{
/// use aya::maps::SockHash; /// use aya::maps::SockHash;
/// use aya::programs::SkMsg; /// 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 map_fd = intercept_egress.fd()?;
/// ///
/// let prog: &mut SkMsg = bpf.program_mut("intercept_egress_packet").unwrap().try_into()?; /// let prog: &mut SkMsg = bpf.program_mut("intercept_egress_packet").unwrap().try_into()?;
@ -54,7 +54,7 @@ use crate::{
/// prog.attach(map_fd)?; /// prog.attach(map_fd)?;
/// ///
/// let mut client = TcpStream::connect("127.0.0.1:1234")?; /// 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)?; /// intercept_egress.insert(1234, client.as_raw_fd(), 0)?;
/// ///
@ -104,8 +104,10 @@ impl<T: AsRef<MapData>, K: Pod> SockHash<T, K> {
MapKeys::new(self.inner.as_ref()) MapKeys::new(self.inner.as_ref())
} }
/// Returns the map's file descriptor, used for instances where programs /// Returns the map's file descriptor.
/// are attached to maps. ///
/// The returned file descriptor can be used to attach programs that work with
/// socket maps, like [`SkMsg`] and [`SkSkb`].
pub fn fd(&self) -> Result<SockMapFd, MapError> { pub fn fd(&self) -> Result<SockMapFd, MapError> {
Ok(SockMapFd(self.inner.as_ref().fd_or_err()?)) Ok(SockMapFd(self.inner.as_ref().fd_or_err()?))
} }

@ -30,7 +30,7 @@ use crate::{
/// use aya::maps::SockMap; /// use aya::maps::SockMap;
/// use aya::programs::SkSkb; /// 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 map_fd = intercept_ingress.fd()?;
/// ///
/// let prog: &mut SkSkb = bpf.program_mut("intercept_ingress_packet").unwrap().try_into()?; /// let prog: &mut SkSkb = bpf.program_mut("intercept_ingress_packet").unwrap().try_into()?;
@ -60,8 +60,10 @@ impl<T: AsRef<MapData>> SockMap<T> {
MapKeys::new(self.inner.as_ref()) MapKeys::new(self.inner.as_ref())
} }
/// Returns the map's file descriptor, used for instances where programs /// Returns the map's file descriptor.
/// are attached to maps. ///
/// The returned file descriptor can be used to attach programs that work with
/// socket maps, like [`SkMsg`] and [`SkSkb`].
pub fn fd(&self) -> Result<SockMapFd, MapError> { pub fn fd(&self) -> Result<SockMapFd, MapError> {
Ok(SockMapFd(self.inner.as_ref().fd_or_err()?)) Ok(SockMapFd(self.inner.as_ref().fd_or_err()?))
} }

@ -21,7 +21,7 @@ use crate::{
/// # let mut bpf = aya::Bpf::load(&[])?; /// # let mut bpf = aya::Bpf::load(&[])?;
/// use aya::maps::Stack; /// 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(42, 0)?;
/// stack.push(43, 0)?; /// stack.push(43, 0)?;
/// assert_eq!(stack.pop(0)?, 43); /// assert_eq!(stack.pop(0)?, 43);

Loading…
Cancel
Save