add doc comments

This commit is contained in:
awtterpip
2023-12-27 20:03:41 -06:00
parent 660dd32d84
commit b43f875b3b
4 changed files with 65 additions and 87 deletions

View File

@@ -3,6 +3,9 @@ use std::rc::Rc;
use crate::prelude::*; use crate::prelude::*;
/// Entry point to the API
///
/// To see methods available for this struct, refer to [EntryTrait]
#[derive(Clone)] #[derive(Clone)]
pub struct Entry(Rc<dyn EntryTrait>); pub struct Entry(Rc<dyn EntryTrait>);
@@ -16,100 +19,71 @@ impl Entry {
} }
} }
/// Represents an intent to start a session with requested extensions.
///
/// To see methods available for this struct, refer to [InstanceTrait]
#[derive(Clone)] #[derive(Clone)]
pub struct Instance(Rc<dyn InstanceTrait>); pub struct Instance(Rc<dyn InstanceTrait>);
/// Represents a running XR application.
///
/// To see methods available for this struct, refer to [SessionTrait]
#[derive(Clone)] #[derive(Clone)]
pub struct Session(Rc<dyn SessionTrait>); pub struct Session(Rc<dyn SessionTrait>);
/// A view of one eye. Used to retrieve render data such as texture views and projection matrices.
///
/// To see methods available for this struct, refer to [ViewTrait]
#[derive(Clone)] #[derive(Clone)]
pub struct View(Rc<dyn ViewTrait>); pub struct View(Rc<dyn ViewTrait>);
/// Represents all XR input sources.
///
/// To see methods available for this struct, refer to [InputTrait]
#[derive(Clone)] #[derive(Clone)]
pub struct Input(Rc<dyn InputTrait>); pub struct Input(Rc<dyn InputTrait>);
/// Represents an XR Action. Can be used to retrieve input values or trigger output devices such as haptics.
///
/// The methods available to this struct are dependent upon the action type. For input values, use `.get()` to retrieve the values.
/// For haptics, please refer to [HapticTrait]
#[derive(Clone)] #[derive(Clone)]
pub struct Action<A: ActionType>(A::Inner); pub struct Action<A: ActionType>(Rc<A::Inner>);
impl Deref for Entry { macro_rules! impl_api {
type Target = dyn EntryTrait; ($($t:ty, $trait:ident; )*) => {
$(
impl std::ops::Deref for $t {
type Target = dyn $trait;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&*self.0 &*self.0
} }
} }
impl Deref for Instance { impl<T: $trait + 'static> From<T> for $t {
type Target = dyn InstanceTrait;
fn deref(&self) -> &Self::Target {
&*self.0
}
}
impl Deref for Session {
type Target = dyn SessionTrait;
fn deref(&self) -> &Self::Target {
&*self.0
}
}
impl Deref for View {
type Target = dyn ViewTrait;
fn deref(&self) -> &Self::Target {
&*self.0
}
}
impl Deref for Input {
type Target = dyn InputTrait;
fn deref(&self) -> &Self::Target {
&*self.0
}
}
impl<O, A> Deref for Action<A>
where
A: ActionType,
A::Inner: Deref<Target = O>,
{
type Target = O;
fn deref(&self) -> &Self::Target {
&*self.0
}
}
impl<T: EntryTrait + 'static> From<T> for Entry {
fn from(value: T) -> Self { fn from(value: T) -> Self {
Self(Rc::new(value)) Self(Rc::new(value))
} }
} }
)*
impl<T: InstanceTrait + 'static> From<T> for Instance { };
fn from(value: T) -> Self {
Self(Rc::new(value))
}
} }
impl<T: SessionTrait + 'static> From<T> for Session { impl_api! {
fn from(value: T) -> Self { Entry, EntryTrait;
Self(Rc::new(value)) Instance, InstanceTrait;
} Session, SessionTrait;
View, ViewTrait;
Input, InputTrait;
} }
impl<T: ViewTrait + 'static> From<T> for View { impl<A: ActionType> Deref for Action<A> {
fn from(value: T) -> Self { type Target = A::Inner;
Self(Rc::new(value))
}
}
impl<T: InputTrait + 'static> From<T> for Input { fn deref(&self) -> &Self::Target {
fn from(value: T) -> Self { &self.0
Self(Rc::new(value))
} }
} }

View File

@@ -26,8 +26,8 @@ pub trait SessionTrait {
-> Option<(Device, Queue, AdapterInfo, Adapter, wgpu::Instance)>; -> Option<(Device, Queue, AdapterInfo, Adapter, wgpu::Instance)>;
/// Request input modules with the specified bindings. /// Request input modules with the specified bindings.
fn create_input(&self, bindings: Bindings) -> Result<Input>; fn create_input(&self, bindings: Bindings) -> Result<Input>;
/// Blocks until a rendering frame is available, then returns the texture views for the left and right eyes. /// Blocks until a rendering frame is available, then returns the views for the left and right eyes.
fn begin_frame(&self) -> Result<(TextureView, TextureView)>; fn begin_frame(&self) -> Result<(View, View)>;
/// Submits rendering work for this frame. /// Submits rendering work for this frame.
fn end_frame(&self) -> Result<()>; fn end_frame(&self) -> Result<()>;
} }
@@ -64,8 +64,12 @@ pub trait ActionTrait {
fn id(&self) -> ActionId; fn id(&self) -> ActionId;
} }
pub trait ActionInputTrait<A> {} /// Represents input actions, such as bools, floats, and poses
pub trait ActionInputTrait<A> {
fn get(&self) -> A;
}
/// Represents haptic actions.
pub trait HapticTrait {} pub trait HapticTrait {}
impl<T: InstanceTrait> EntryTrait for T { impl<T: InstanceTrait> EntryTrait for T {

View File

@@ -90,7 +90,7 @@ impl SessionTrait for WebXrSession {
.into()) .into())
} }
fn begin_frame(&self) -> Result<(wgpu::TextureView, wgpu::TextureView)> { fn begin_frame(&self) -> Result<(View, View)> {
let mut end_frame_sender = self.end_frame_sender.lock().unwrap(); let mut end_frame_sender = self.end_frame_sender.lock().unwrap();
if end_frame_sender.is_some() { if end_frame_sender.is_some() {
Err(XrError::Placeholder)? Err(XrError::Placeholder)?

View File

@@ -33,13 +33,13 @@ pub struct Haptic;
pub struct Pose; pub struct Pose;
pub trait ActionType: Sized { pub trait ActionType: Sized {
type Inner; type Inner: ?Sized;
fn get(input: &dyn InputTrait, path: ActionId) -> Result<Action<Self>>; fn get(input: &dyn InputTrait, path: ActionId) -> Result<Action<Self>>;
} }
impl ActionType for Haptic { impl ActionType for Haptic {
type Inner = Rc<dyn HapticTrait>; type Inner = dyn HapticTrait;
fn get(input: &dyn InputTrait, path: ActionId) -> Result<Action<Self>> { fn get(input: &dyn InputTrait, path: ActionId) -> Result<Action<Self>> {
input.get_haptics(path) input.get_haptics(path)
@@ -47,7 +47,7 @@ impl ActionType for Haptic {
} }
impl ActionType for Pose { impl ActionType for Pose {
type Inner = Rc<dyn ActionInputTrait<Pose>>; type Inner = dyn ActionInputTrait<Pose>;
fn get(input: &dyn InputTrait, path: ActionId) -> Result<Action<Self>> { fn get(input: &dyn InputTrait, path: ActionId) -> Result<Action<Self>> {
input.get_pose(path) input.get_pose(path)
@@ -55,7 +55,7 @@ impl ActionType for Pose {
} }
impl ActionType for f32 { impl ActionType for f32 {
type Inner = Rc<dyn ActionInputTrait<f32>>; type Inner = dyn ActionInputTrait<f32>;
fn get(input: &dyn InputTrait, path: ActionId) -> Result<Action<Self>> { fn get(input: &dyn InputTrait, path: ActionId) -> Result<Action<Self>> {
input.get_float(path) input.get_float(path)
@@ -63,7 +63,7 @@ impl ActionType for f32 {
} }
impl ActionType for bool { impl ActionType for bool {
type Inner = Rc<dyn ActionInputTrait<bool>>; type Inner = dyn ActionInputTrait<bool>;
fn get(input: &dyn InputTrait, path: ActionId) -> Result<Action<Self>> { fn get(input: &dyn InputTrait, path: ActionId) -> Result<Action<Self>> {
input.get_bool(path) input.get_bool(path)