Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
performer.advance();
assert_eq!(
performer.get::<Value>(value_out_dynamic),
Ok(ValueRef::Int32(14))
ValueRef::Int32(14)
);

/*
Expand Down
6 changes: 3 additions & 3 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use {
crate::{
endpoint::{EndpointHandle, EndpointInfo},
ffi::EnginePtr,
performer::{Endpoint, EndpointError, EndpointType, OutputEvent, Performer},
performer::{Endpoint, EndpointError, MakeEndpoint, OutputEvent, Performer},
program::Program,
},
std::{
Expand Down Expand Up @@ -194,7 +194,7 @@ impl Engine<Loaded> {
/// Returns an endpoint handle.
pub fn endpoint<T>(&mut self, id: impl AsRef<str>) -> Result<Endpoint<T>, EndpointError>
where
T: EndpointType,
T: MakeEndpoint,
{
let id = id.as_ref();

Expand All @@ -212,7 +212,7 @@ impl Engine<Loaded> {

self.state.endpoints.insert(handle, info.clone());

EndpointType::make(handle, info)
MakeEndpoint::make(handle, info)
}

/// Returns the details of the program loaded into the engine.
Expand Down
10 changes: 7 additions & 3 deletions src/performer/endpoints/event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
endpoint::{EndpointDirection, EndpointHandle, EndpointInfo},
performer::{Endpoint, EndpointError, EndpointType, Performer},
performer::{Endpoint, EndpointError, GetHandle, MakeEndpoint, Performer},
value::ValueRef,
};

Expand All @@ -16,7 +16,7 @@ pub struct OutputEvent {
handle: EndpointHandle,
}

impl EndpointType for InputEvent {
impl MakeEndpoint for InputEvent {
fn make(
handle: EndpointHandle,
endpoint: EndpointInfo,
Expand All @@ -31,13 +31,15 @@ impl EndpointType for InputEvent {

Ok(Endpoint(InputEvent { handle }))
}
}

impl GetHandle for InputEvent {
fn handle(&self) -> EndpointHandle {
self.handle
}
}

impl EndpointType for OutputEvent {
impl MakeEndpoint for OutputEvent {
fn make(handle: EndpointHandle, endpoint: EndpointInfo) -> Result<Endpoint<Self>, EndpointError>
where
Self: Sized,
Expand All @@ -52,7 +54,9 @@ impl EndpointType for OutputEvent {

Ok(Endpoint(Self { handle }))
}
}

impl GetHandle for OutputEvent {
fn handle(&self) -> EndpointHandle {
self.handle
}
Expand Down
16 changes: 13 additions & 3 deletions src/performer/endpoints/stream.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
crate::{
endpoint::{EndpointDirection, EndpointHandle, EndpointInfo},
performer::{Endpoint, EndpointError, EndpointType, Performer},
performer::{Endpoint, EndpointError, GetHandle, MakeEndpoint, Performer},
value::types::{IsScalar, Type},
},
std::marker::PhantomData,
Expand All @@ -27,7 +27,7 @@ where
_marker: PhantomData<T>,
}

impl<T> EndpointType for InputStream<T>
impl<T> MakeEndpoint for InputStream<T>
where
T: StreamType,
{
Expand All @@ -42,13 +42,18 @@ where
_marker: PhantomData,
}))
}
}

impl<T> GetHandle for InputStream<T>
where
T: StreamType,
{
fn handle(&self) -> EndpointHandle {
self.handle
}
}

impl<T> EndpointType for OutputStream<T>
impl<T> MakeEndpoint for OutputStream<T>
where
T: StreamType,
{
Expand All @@ -63,7 +68,12 @@ where
_marker: PhantomData,
}))
}
}

impl<T> GetHandle for OutputStream<T>
where
T: StreamType,
{
fn handle(&self) -> EndpointHandle {
self.handle
}
Expand Down
25 changes: 17 additions & 8 deletions src/performer/endpoints/value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
crate::{
endpoint::{EndpointDirection, EndpointHandle, EndpointInfo},
performer::{endpoints::Endpoint, EndpointError, EndpointType, Performer},
performer::{endpoints::Endpoint, EndpointError, GetHandle, MakeEndpoint, Performer},
value::{Value, ValueRef},
},
std::{any::TypeId, marker::PhantomData},
Expand All @@ -21,7 +21,7 @@ pub struct OutputValue<T = Value> {
_marker: PhantomData<T>,
}

impl<T> EndpointType for InputValue<T>
impl<T> MakeEndpoint for InputValue<T>
where
T: 'static,
{
Expand All @@ -36,13 +36,15 @@ where
_marker: PhantomData,
}))
}
}

impl<T> GetHandle for InputValue<T> {
fn handle(&self) -> EndpointHandle {
self.handle
}
}

impl<T> EndpointType for OutputValue<T>
impl<T> MakeEndpoint for OutputValue<T>
where
T: 'static,
{
Expand All @@ -57,7 +59,9 @@ where
_marker: PhantomData,
}))
}
}

impl<T> GetHandle for OutputValue<T> {
fn handle(&self) -> EndpointHandle {
self.handle
}
Expand Down Expand Up @@ -225,7 +229,7 @@ impl GetOutputValue for bool {
}

impl GetOutputValue for Value {
type Output<'a> = Result<ValueRef<'a>, ()>;
type Output<'a> = ValueRef<'a>;

fn get_output_value(
performer: &mut Performer,
Expand All @@ -237,11 +241,16 @@ impl GetOutputValue for Value {
.endpoints
.get(&endpoint.handle)
.and_then(|endpoint| endpoint.as_value())
.map(|value_endpoint| value_endpoint.ty().as_ref())
.expect("failed to determine endpoint type");
.map(|value_endpoint| value_endpoint.ty().as_ref());

ptr.copy_output_value(endpoint.handle, buffer);
debug_assert!(ty.is_some(), "endpoint should exist and be a value type");

Ok(ValueRef::new_from_slice(ty, &buffer[..ty.size()]))
match ty {
Some(ty) => {
ptr.copy_output_value(endpoint.handle, buffer);
ValueRef::new_from_slice(ty, &buffer[..ty.size()])
}
None => ValueRef::Void,
}
}
}
7 changes: 5 additions & 2 deletions src/performer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Performer {
/// Returns information about a given endpoint.
pub fn endpoint_info<T>(&self, Endpoint(endpoint): Endpoint<T>) -> Option<&EndpointInfo>
where
T: EndpointType,
T: GetHandle,
{
self.endpoints.get(&endpoint.handle())
}
Expand Down Expand Up @@ -171,14 +171,17 @@ pub enum EndpointError {
}

#[doc(hidden)]
pub trait EndpointType: sealed::Sealed {
pub trait MakeEndpoint: sealed::Sealed {
fn make(
handle: EndpointHandle,
endpoint: EndpointInfo,
) -> Result<Endpoint<Self>, EndpointError>
where
Self: Sized;
}

#[doc(hidden)]
pub trait GetHandle: sealed::Sealed {
fn handle(&self) -> EndpointHandle;
}

Expand Down
12 changes: 6 additions & 6 deletions tests/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn can_read_and_write_complex32_numbers() {

performer.advance();

let result: Complex32 = performer.get::<Value>(output).unwrap().try_into().unwrap();
let result: Complex32 = performer.get::<Value>(output).try_into().unwrap();

assert_eq!(
result,
Expand Down Expand Up @@ -175,7 +175,7 @@ fn can_read_and_write_complex64_numbers() {

performer.advance();

let result: Complex64 = performer.get::<Value>(output).unwrap().try_into().unwrap();
let result: Complex64 = performer.get::<Value>(output).try_into().unwrap();

assert_eq!(
result,
Expand Down Expand Up @@ -212,7 +212,7 @@ fn can_read_structs() {

performer.advance();

let value = performer.get::<Value>(output).unwrap();
let value = performer.get::<Value>(output);
let object = value.as_object().unwrap();

assert_eq!(object.field("a").unwrap(), ValueRef::Bool(true));
Expand Down Expand Up @@ -247,7 +247,7 @@ fn can_read_and_write_arrays() {

performer.advance();

let value = performer.get::<Value>(output).unwrap();
let value = performer.get::<Value>(output);
let array = value.as_array().unwrap();

assert_eq!(array.len(), 4);
Expand Down Expand Up @@ -511,7 +511,7 @@ fn read_and_write_vectors() {
performer.set::<Value>(input, [1, 2, 3, 4].into()).unwrap();
performer.advance();

let value = performer.get::<Value>(output).unwrap();
let value = performer.get::<Value>(output);
let array = value.as_array().unwrap();

let elems: Vec<_> = array.elems().collect();
Expand Down Expand Up @@ -770,7 +770,7 @@ fn string_endpoints() {

performer.advance();

let value = if let ValueRef::String(string) = performer.get::<Value>(out).unwrap() {
let value = if let ValueRef::String(string) = performer.get::<Value>(out) {
string
} else {
panic!("expected string");
Expand Down
4 changes: 2 additions & 2 deletions tests/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ fn loading_external_variables_struct() {

performer.advance();

let result: Complex32 = performer.get(out).unwrap().try_into().unwrap();
let result: Complex32 = performer.get(out).try_into().unwrap();
assert_eq!(result.real, 42.0);
assert_eq!(result.imag, 21.0);
}
Expand Down Expand Up @@ -262,7 +262,7 @@ fn loading_external_variables_array() {

performer.advance();

let value = performer.get(out).unwrap();
let value = performer.get(out);
let array = value.as_array().unwrap();

assert_eq!(array.get(0), Some(ValueRef::Int32(1)));
Expand Down