can_socket/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
//! CAN socket
//!
//! This library exposes a [`CanSocket`] and related types,
//! allowing you to communicate over a Controller Area Network (CAN) bus.
//!
//! The is a standard blocking or non-blocking [`CanSocket`],
//! and an asynchronous [`tokio::CanSocket`].
//!
//! This library uses the `SocketCAN` interface and only works on Linux.
//!
//! Supported features:
//! * Bind sockets to specific interfaces by name or index.
//! * Bind sockets to *all* CAN interfaces at the same time.
//! * Send and receive data frames and RTR frames.
//! * Send and receive standard frames and extended frames.
//! * Setting per-socket filters.
//! * Control over the `loopback` and `recv_own_msgs` options.
//! * Constructing compile-time checked CAN IDs.
#![cfg_attr(feature = "doc-cfg", feature(doc_cfg))]
#![warn(missing_docs)]
#![warn(missing_debug_implementations)]
pub mod error;
#[cfg(feature = "tokio")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "tokio")))]
pub mod tokio;
mod id;
pub use id::{ExtendedId, CanId, StandardId, MAX_EXTENDED_ID, MAX_STANDARD_ID};
mod filter;
pub use filter::CanFilter;
mod frame;
pub use frame::{CanFrame, CanData};
mod interface;
pub use interface::CanInterface;
mod socket;
pub use socket::CanSocket;
mod sys;
/// Trait for types that can be used as a timeout or deadline.
pub trait Deadline {
/// Get the instant at which the timeout/deadline expires.
fn deadline(&self) -> std::time::Instant;
}
impl Deadline for std::time::Duration {
/// Get the instant at which the timeout/deadline expires.
///
/// If the `"tokio"` feature is enabled, this uses [`tokio::time::Instant`][::tokio::time::Instant] to compute the deadline.
/// This means that [`tokio::time::pause()`][::tokio::time::pause] and [`tokio::time::advance()`][::tokio::time::advance] will work as expected.
fn deadline(&self) -> std::time::Instant {
// Use tokio's `Instant::now()` when the `tokio` feature is enabled.
// This ensures that tokio::time::pause() and tokio::time::advance() will work.
#[cfg(feature = "tokio")]
{
(::tokio::time::Instant::now() + *self).into_std()
}
#[cfg(not(feature = "tokio"))]
{
std::time::Instant::now() + *self
}
}
}
impl Deadline for std::time::Instant {
fn deadline(&self) -> std::time::Instant {
*self
}
}
#[cfg(feature = "tokio")]
impl Deadline for ::tokio::time::Instant {
fn deadline(&self) -> std::time::Instant {
self.into_std()
}
}