#![allow(clippy::let_unit_value)]
#[rustfmt::skip]
#[path = "gen/proxy.rs"]
mod impls;
use std::{future::Future, sync::Arc, time::SystemTime};
use abi_stable::{
declare_root_module_statics,
erased_types::TD_Opaque,
library::RootModule,
package_version_strings,
prefix_type::PrefixTypeTrait,
rtry, sabi_trait,
sabi_types::VersionStrings,
std_types::{RBox, RBoxError, RDuration, ROk, ROption, RString, RVec},
StableAbi,
};
use anyhow::format_err;
use arci::nalgebra;
pub(crate) use self::impls::*;
use crate::PluginProxy;
type RResult<T, E = RError> = abi_stable::std_types::RResult<T, E>;
fn block_in_place<T>(f: impl Future<Output = T>) -> T {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(f)
}
#[repr(C)]
#[derive(StableAbi)]
pub(crate) struct RSystemTime {
duration_since_epoch: RDuration,
}
impl TryFrom<SystemTime> for RSystemTime {
type Error = RError;
fn try_from(val: SystemTime) -> Result<Self, Self::Error> {
let duration_since_epoch = val
.duration_since(SystemTime::UNIX_EPOCH)
.map_err(|_| format_err!("SystemTime must be later than UNIX_EPOCH"))?;
Ok(Self {
duration_since_epoch: duration_since_epoch.into(),
})
}
}
impl TryFrom<RSystemTime> for SystemTime {
type Error = RError;
fn try_from(val: RSystemTime) -> Result<Self, Self::Error> {
let duration_since_epoch = val.duration_since_epoch.into();
SystemTime::UNIX_EPOCH
.checked_add(duration_since_epoch)
.ok_or_else(|| format_err!("overflow deserializing SystemTime").into())
}
}
#[repr(C)]
#[derive(StableAbi)]
pub(crate) struct RIsometry2F64 {
rotation: RUnitComplexF64,
translation: RTranslation2F64,
}
impl From<nalgebra::Isometry2<f64>> for RIsometry2F64 {
fn from(val: nalgebra::Isometry2<f64>) -> Self {
Self {
rotation: val.rotation.into(),
translation: val.translation.into(),
}
}
}
impl From<RIsometry2F64> for nalgebra::Isometry2<f64> {
fn from(val: RIsometry2F64) -> Self {
Self::from_parts(val.translation.into(), val.rotation.into())
}
}
#[repr(C)]
#[derive(StableAbi)]
struct RUnitComplexF64 {
re: f64,
im: f64,
}
impl From<nalgebra::UnitComplex<f64>> for RUnitComplexF64 {
fn from(val: nalgebra::UnitComplex<f64>) -> Self {
let val = val.into_inner();
Self {
re: val.re,
im: val.im,
}
}
}
impl From<RUnitComplexF64> for nalgebra::UnitComplex<f64> {
fn from(val: RUnitComplexF64) -> Self {
Self::from_complex(nalgebra::Complex {
re: val.re,
im: val.im,
})
}
}
#[repr(C)]
#[derive(StableAbi)]
struct RTranslation2F64 {
x: f64,
y: f64,
}
impl From<nalgebra::Translation2<f64>> for RTranslation2F64 {
fn from(val: nalgebra::Translation2<f64>) -> Self {
Self {
x: val.vector.x,
y: val.vector.y,
}
}
}
impl From<RTranslation2F64> for nalgebra::Translation2<f64> {
fn from(val: RTranslation2F64) -> Self {
Self::new(val.x, val.y)
}
}
#[repr(C)]
#[derive(StableAbi)]
pub(crate) struct RIsometry3F64 {
rotation: RUnitQuaternionF64,
translation: RTranslation3F64,
}
impl From<nalgebra::Isometry3<f64>> for RIsometry3F64 {
fn from(val: nalgebra::Isometry3<f64>) -> Self {
Self {
rotation: val.rotation.into(),
translation: val.translation.into(),
}
}
}
impl From<RIsometry3F64> for nalgebra::Isometry3<f64> {
fn from(val: RIsometry3F64) -> Self {
Self::from_parts(val.translation.into(), val.rotation.into())
}
}
#[repr(C)]
#[derive(StableAbi)]
struct RUnitQuaternionF64 {
x: f64,
y: f64,
z: f64,
w: f64,
}
impl From<nalgebra::UnitQuaternion<f64>> for RUnitQuaternionF64 {
fn from(val: nalgebra::UnitQuaternion<f64>) -> Self {
let val = val.into_inner();
Self {
x: val.coords.x,
y: val.coords.y,
z: val.coords.z,
w: val.coords.w,
}
}
}
impl From<RUnitQuaternionF64> for nalgebra::UnitQuaternion<f64> {
fn from(val: RUnitQuaternionF64) -> Self {
Self::from_quaternion(nalgebra::Quaternion::new(val.w, val.x, val.y, val.z))
}
}
#[repr(C)]
#[derive(StableAbi)]
struct RTranslation3F64 {
x: f64,
y: f64,
z: f64,
}
impl From<nalgebra::Translation3<f64>> for RTranslation3F64 {
fn from(val: nalgebra::Translation3<f64>) -> Self {
Self {
x: val.vector.x,
y: val.vector.y,
z: val.vector.z,
}
}
}
impl From<RTranslation3F64> for nalgebra::Translation3<f64> {
fn from(val: RTranslation3F64) -> Self {
Self::new(val.x, val.y, val.z)
}
}
#[repr(C)]
#[derive(StableAbi)]
pub(crate) struct RError {
repr: RBoxError,
}
impl From<arci::Error> for RError {
fn from(e: arci::Error) -> Self {
Self {
repr: RBoxError::from_box(e.into()),
}
}
}
impl From<anyhow::Error> for RError {
fn from(e: anyhow::Error) -> Self {
Self {
repr: RBoxError::from_box(e.into()),
}
}
}
impl From<RError> for arci::Error {
fn from(e: RError) -> Self {
Self::Other(format_err!("{}", e.repr))
}
}
#[repr(C)]
#[derive(StableAbi)]
#[must_use]
pub(crate) struct RBlockingWait(RBoxWait);
impl RBlockingWait {
fn from_fn(f: impl FnOnce() -> RResult<()> + Send + 'static) -> Self {
Self(RBoxWait::from_value(f, TD_Opaque))
}
}
impl From<arci::WaitFuture> for RBlockingWait {
fn from(wait: arci::WaitFuture) -> Self {
Self::from_fn(move || block_in_place(wait).map_err(RError::from).into())
}
}
impl From<RBlockingWait> for arci::WaitFuture {
fn from(wait: RBlockingWait) -> Self {
arci::WaitFuture::new(async move {
tokio::task::spawn_blocking(move || wait.0.wait())
.await
.map_err(|e| arci::Error::Other(e.into()))?
.into_result()?;
Ok(())
})
}
}
type RBoxWait = RWaitTrait_TO<RBox<()>>;
#[sabi_trait]
trait RWaitTrait: Send + 'static {
fn wait(self) -> RResult<()>;
}
impl<F> RWaitTrait for F
where
F: FnOnce() -> RResult<()> + Send + 'static,
{
fn wait(self) -> RResult<()> {
self()
}
}
#[repr(C)]
#[derive(StableAbi)]
pub(crate) struct RTrajectoryPoint {
positions: RVec<f64>,
velocities: ROption<RVec<f64>>,
time_from_start: RDuration,
}
impl From<arci::TrajectoryPoint> for RTrajectoryPoint {
fn from(val: arci::TrajectoryPoint) -> Self {
Self {
positions: val.positions.into_iter().collect(),
velocities: val.velocities.map(|v| v.into_iter().collect()).into(),
time_from_start: val.time_from_start.into(),
}
}
}
impl From<RTrajectoryPoint> for arci::TrajectoryPoint {
fn from(val: RTrajectoryPoint) -> Self {
Self {
positions: val.positions.into_iter().map(f64::from).collect(),
velocities: val
.velocities
.into_option()
.map(|v| v.into_iter().map(f64::from).collect()),
time_from_start: val.time_from_start.into(),
}
}
}
#[repr(C)]
#[derive(StableAbi)]
pub(crate) struct RBaseVelocity {
x: f64,
y: f64,
theta: f64,
}
impl From<arci::BaseVelocity> for RBaseVelocity {
fn from(val: arci::BaseVelocity) -> Self {
Self {
x: val.x,
y: val.y,
theta: val.theta,
}
}
}
impl From<RBaseVelocity> for arci::BaseVelocity {
fn from(val: RBaseVelocity) -> Self {
Self {
x: val.x,
y: val.y,
theta: val.theta,
}
}
}
#[repr(C)]
#[derive(StableAbi)]
pub(crate) enum RGamepadEvent {
ButtonPressed(RButton),
ButtonReleased(RButton),
AxisChanged(RAxis, f64),
Connected,
Disconnected,
Unknown,
}
impl From<arci::gamepad::GamepadEvent> for RGamepadEvent {
fn from(e: arci::gamepad::GamepadEvent) -> Self {
match e {
arci::gamepad::GamepadEvent::ButtonPressed(b) => Self::ButtonPressed(b.into()),
arci::gamepad::GamepadEvent::ButtonReleased(b) => Self::ButtonReleased(b.into()),
arci::gamepad::GamepadEvent::AxisChanged(a, v) => Self::AxisChanged(a.into(), v),
arci::gamepad::GamepadEvent::Connected => Self::Connected,
arci::gamepad::GamepadEvent::Disconnected => Self::Disconnected,
arci::gamepad::GamepadEvent::Unknown => Self::Unknown,
}
}
}
impl From<RGamepadEvent> for arci::gamepad::GamepadEvent {
fn from(e: RGamepadEvent) -> Self {
match e {
RGamepadEvent::ButtonPressed(b) => Self::ButtonPressed(b.into()),
RGamepadEvent::ButtonReleased(b) => Self::ButtonReleased(b.into()),
RGamepadEvent::AxisChanged(a, v) => Self::AxisChanged(a.into(), v),
RGamepadEvent::Connected => Self::Connected,
RGamepadEvent::Disconnected => Self::Disconnected,
RGamepadEvent::Unknown => Self::Unknown,
}
}
}
#[repr(C)]
#[derive(StableAbi)]
pub(crate) enum RButton {
South,
East,
North,
West,
LeftTrigger,
LeftTrigger2,
RightTrigger,
RightTrigger2,
Select,
Start,
Mode,
LeftThumb,
RightThumb,
DPadUp,
DPadDown,
DPadLeft,
DPadRight,
Unknown,
}
impl From<arci::gamepad::Button> for RButton {
fn from(b: arci::gamepad::Button) -> Self {
match b {
arci::gamepad::Button::South => Self::South,
arci::gamepad::Button::East => Self::East,
arci::gamepad::Button::North => Self::North,
arci::gamepad::Button::West => Self::West,
arci::gamepad::Button::LeftTrigger => Self::LeftTrigger,
arci::gamepad::Button::LeftTrigger2 => Self::LeftTrigger2,
arci::gamepad::Button::RightTrigger => Self::RightTrigger,
arci::gamepad::Button::RightTrigger2 => Self::RightTrigger2,
arci::gamepad::Button::Select => Self::Select,
arci::gamepad::Button::Start => Self::Start,
arci::gamepad::Button::Mode => Self::Mode,
arci::gamepad::Button::LeftThumb => Self::LeftThumb,
arci::gamepad::Button::RightThumb => Self::RightThumb,
arci::gamepad::Button::DPadUp => Self::DPadUp,
arci::gamepad::Button::DPadDown => Self::DPadDown,
arci::gamepad::Button::DPadLeft => Self::DPadLeft,
arci::gamepad::Button::DPadRight => Self::DPadRight,
arci::gamepad::Button::Unknown => Self::Unknown,
}
}
}
impl From<RButton> for arci::gamepad::Button {
fn from(b: RButton) -> Self {
match b {
RButton::South => Self::South,
RButton::East => Self::East,
RButton::North => Self::North,
RButton::West => Self::West,
RButton::LeftTrigger => Self::LeftTrigger,
RButton::LeftTrigger2 => Self::LeftTrigger2,
RButton::RightTrigger => Self::RightTrigger,
RButton::RightTrigger2 => Self::RightTrigger2,
RButton::Select => Self::Select,
RButton::Start => Self::Start,
RButton::Mode => Self::Mode,
RButton::LeftThumb => Self::LeftThumb,
RButton::RightThumb => Self::RightThumb,
RButton::DPadUp => Self::DPadUp,
RButton::DPadDown => Self::DPadDown,
RButton::DPadLeft => Self::DPadLeft,
RButton::DPadRight => Self::DPadRight,
RButton::Unknown => Self::Unknown,
}
}
}
#[repr(C)]
#[derive(StableAbi)]
pub(crate) enum RAxis {
LeftStickX,
LeftStickY,
LeftTrigger,
RightStickX,
RightStickY,
RightTrigger,
DPadX,
DPadY,
Unknown,
}
impl From<arci::gamepad::Axis> for RAxis {
fn from(a: arci::gamepad::Axis) -> Self {
match a {
arci::gamepad::Axis::LeftStickX => Self::LeftStickX,
arci::gamepad::Axis::LeftStickY => Self::LeftStickY,
arci::gamepad::Axis::LeftTrigger => Self::LeftTrigger,
arci::gamepad::Axis::RightStickX => Self::RightStickX,
arci::gamepad::Axis::RightStickY => Self::RightStickY,
arci::gamepad::Axis::RightTrigger => Self::RightTrigger,
arci::gamepad::Axis::DPadX => Self::DPadX,
arci::gamepad::Axis::DPadY => Self::DPadY,
arci::gamepad::Axis::Unknown => Self::Unknown,
}
}
}
impl From<RAxis> for arci::gamepad::Axis {
fn from(a: RAxis) -> Self {
match a {
RAxis::LeftStickX => Self::LeftStickX,
RAxis::LeftStickY => Self::LeftStickY,
RAxis::LeftTrigger => Self::LeftTrigger,
RAxis::RightStickX => Self::RightStickX,
RAxis::RightStickY => Self::RightStickY,
RAxis::RightTrigger => Self::RightTrigger,
RAxis::DPadX => Self::DPadX,
RAxis::DPadY => Self::DPadY,
RAxis::Unknown => Self::Unknown,
}
}
}
pub(crate) type JointTrajectoryClientTraitObject = RJointTrajectoryClientTrait_TO<RBox<()>>;
#[sabi_trait]
pub(crate) trait RJointTrajectoryClientTrait: Send + Sync + 'static {
fn joint_names(&self) -> RVec<RString>;
fn current_joint_positions(&self) -> RResult<RVec<f64>>;
fn send_joint_positions(
&self,
positions: RVec<f64>,
duration: RDuration,
) -> RResult<RBlockingWait>;
fn send_joint_trajectory(&self, trajectory: RVec<RTrajectoryPoint>) -> RResult<RBlockingWait>;
}
impl<T> RJointTrajectoryClientTrait for T
where
T: ?Sized + arci::JointTrajectoryClient + 'static,
{
fn joint_names(&self) -> RVec<RString> {
arci::JointTrajectoryClient::joint_names(self)
.into_iter()
.map(|s| s.into())
.collect()
}
fn current_joint_positions(&self) -> RResult<RVec<f64>> {
ROk(
rtry!(arci::JointTrajectoryClient::current_joint_positions(self))
.into_iter()
.collect(),
)
}
fn send_joint_positions(
&self,
positions: RVec<f64>,
duration: RDuration,
) -> RResult<RBlockingWait> {
ROk(rtry!(arci::JointTrajectoryClient::send_joint_positions(
self,
positions.into_iter().map(f64::from).collect(),
duration.into(),
))
.into())
}
fn send_joint_trajectory(&self, trajectory: RVec<RTrajectoryPoint>) -> RResult<RBlockingWait> {
ROk(rtry!(arci::JointTrajectoryClient::send_joint_trajectory(
self,
trajectory
.into_iter()
.map(arci::TrajectoryPoint::from)
.collect(),
))
.into())
}
}
pub(crate) type GamepadTraitObject = RGamepadTrait_TO<RBox<()>>;
#[sabi_trait]
pub(crate) trait RGamepadTrait: Send + Sync + Clone + 'static {
fn next_event(&self) -> RGamepadEvent;
fn stop(&self);
}
impl<T> RGamepadTrait for Arc<T>
where
T: ?Sized + arci::Gamepad + 'static,
{
fn next_event(&self) -> RGamepadEvent {
block_in_place(arci::Gamepad::next_event(&**self)).into()
}
fn stop(&self) {
arci::Gamepad::stop(&**self);
}
}
#[doc(hidden)]
#[allow(missing_debug_implementations)]
#[repr(C)]
#[derive(StableAbi)]
#[sabi(kind(Prefix))]
#[sabi(missing_field(panic))]
pub struct PluginMod {
#[sabi(last_prefix_field)]
pub(crate) plugin_constructor: extern "C" fn() -> PluginProxy,
}
impl RootModule for PluginMod_Ref {
const BASE_NAME: &'static str = "plugin";
const NAME: &'static str = "plugin";
const VERSION_STRINGS: VersionStrings = package_version_strings!();
declare_root_module_statics!(PluginMod_Ref);
}
impl PluginMod_Ref {
#[doc(hidden)]
pub fn new(plugin_constructor: extern "C" fn() -> PluginProxy) -> Self {
PluginMod { plugin_constructor }.leak_into_prefix()
}
}