Skip to main content

Map

Struct Map 

Source
pub struct Map<S, F> { /* private fields */ }
Expand description

A sensor whose readings pass through a function before they are returned.

Created by Sensor::map. The function runs on every read, so it can select one channel of a multi-channel measurement, convert a unit, or fold several fields into one value.

§Examples

use pamoja_core::{Result, Sensor};

struct Climate;
impl Sensor for Climate {
    type Reading = (f32, f32);
    async fn read(&mut self) -> Result<(f32, f32)> {
        Ok((21.5, 48.0))
    }
}

let mut humidity = Climate.map(|(_, humidity)| humidity);
assert_eq!(humidity.read().await?, 48.0);

Implementations§

Source§

impl<S, F> Map<S, F>

Source

pub fn new(inner: S, select: F) -> Map<S, F>

Wraps inner so each reading passes through select.

§Arguments
  • inner - the sensor being adapted.
  • select - the function applied to each reading.
§Returns

The adapted sensor.

Source

pub fn inner(&self) -> &S

Borrows the wrapped sensor.

§Returns

The sensor underneath the adapter.

Source

pub fn inner_mut(&mut self) -> &mut S

Mutably borrows the wrapped sensor.

§Returns

The sensor underneath the adapter.

Source

pub fn into_inner(self) -> S

Unwraps the sensor, discarding the adapter.

§Returns

The sensor underneath the adapter.

Trait Implementations§

Source§

impl<S, F, T> Sensor for Map<S, F>
where S: Sensor + Send, F: FnMut(S::Reading) -> T + Send,

Source§

type Reading = T

The value produced by a single read, for example a temperature or a fix.
Source§

async fn read(&mut self) -> Result<T>

Takes a single reading from the sensor. Read more
Source§

fn map<F, T>(self, select: F) -> Map<Self, F>
where Self: Sized, F: FnMut(Self::Reading) -> T,

Wraps this sensor so every reading passes through select first. Read more

Auto Trait Implementations§

§

impl<S, F> Freeze for Map<S, F>
where S: Freeze, F: Freeze,

§

impl<S, F> RefUnwindSafe for Map<S, F>

§

impl<S, F> Send for Map<S, F>
where S: Send, F: Send,

§

impl<S, F> Sync for Map<S, F>
where S: Sync, F: Sync,

§

impl<S, F> Unpin for Map<S, F>
where S: Unpin, F: Unpin,

§

impl<S, F> UnsafeUnpin for Map<S, F>
where S: UnsafeUnpin, F: UnsafeUnpin,

§

impl<S, F> UnwindSafe for Map<S, F>
where S: UnwindSafe, F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.