Skip to content

Commit

Permalink
remove Superposition
Browse files Browse the repository at this point in the history
  • Loading branch information
shumbo committed Dec 2, 2024
1 parent e1484c4 commit 5ac0739
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 96 deletions.
67 changes: 1 addition & 66 deletions chorus_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro::{self, TokenStream};
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Fields};
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(ChoreographyLocation)]
pub fn derive_choreography_location(input: TokenStream) -> TokenStream {
Expand All @@ -23,68 +23,3 @@ pub fn derive_choreography_location(input: TokenStream) -> TokenStream {
};
output.into()
}

#[proc_macro_derive(Superposition)]
pub fn derive_superposition(input: TokenStream) -> TokenStream {
// Parse the input tokens into a syntax tree
let input = syn::parse_macro_input!(input as DeriveInput);

// Get the name of the struct or enum
let name = &input.ident;

// Generate the implementation of the Superposition trait
let expanded = match input.data {
Data::Struct(data) => match data.fields {
Fields::Named(fields) => {
let field_names = fields.named.iter().map(|field| &field.ident);
quote! {
impl Superposition for #name {
fn remote() -> Self {
#name {
#( #field_names: <_ as Superposition>::remote(), )*
}
}
}
}
}
Fields::Unnamed(fields) => {
let fields = (0..fields.unnamed.len()).map(|_| {
quote! {
<_ as Superposition>::remote()
}
});
quote! {
impl Superposition for #name {
fn remote() -> Self {
#name(
#(#fields),*
)
}
}
}
}
Fields::Unit => {
quote! {
impl Superposition for #name {
fn remote() -> Self {
#name
}
}
}
}
},
Data::Enum(_) => {
quote! {
compile_error!("Superposition cannot be derived automatically for enums");
}
}
Data::Union(_) => {
quote! {
compile_error!("Superposition cannot be derived automatically for unions");
}
}
};

// Convert the generated tokens back into a TokenStream
TokenStream::from(expanded)
}
3 changes: 1 addition & 2 deletions chorus_lib/examples/runner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate chorus_lib;
use chorus_lib::core::{
ChoreoOp, Choreography, ChoreographyLocation, Located, LocationSet, Runner, Superposition,
ChoreoOp, Choreography, ChoreographyLocation, Located, LocationSet, Runner,
};

#[derive(ChoreographyLocation)]
Expand All @@ -14,7 +14,6 @@ fn get_random_number() -> u32 {
42 // for presentation purpose
}

#[derive(Superposition)]
struct BobCarolResult {
is_even_at_bob: Located<bool, Bob>,
is_even_at_carol: Located<bool, Carol>,
Expand Down
37 changes: 9 additions & 28 deletions chorus_lib/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@ pub trait ChoreographyLocation: Copy {
pub trait Portable: Serialize + DeserializeOwned {}
impl<T: Serialize + DeserializeOwned> Portable for T {}

/// Represents a value that might *NOT* be located at a location. Values returned by `enclave` must satisfy this trait.
///
/// In most cases, you don't need to implement this trait manually. You can derive it using `#[derive(Superposition)]` as long as all the fields consist of located values.
pub trait Superposition {
/// Constructs a struct that is *NOT* located at a location.
fn remote() -> Self;
}

impl Superposition for () {
fn remote() -> Self {
()
}
}

/// Represents a value located at a location.
pub type Located<V, L1> = MultiplyLocated<V, LocationSet!(L1)>;

Expand All @@ -71,6 +57,14 @@ where
phantom: PhantomData,
}
}

/// Constructs a struct located at another location
fn remote() -> Self {
MultiplyLocated {
value: None,
phantom: PhantomData,
}
}
}

impl<V, LS1, LS2> MultiplyLocated<MultiplyLocated<V, LS1>, LS2>
Expand Down Expand Up @@ -104,19 +98,6 @@ where
}
}

impl<V, L> Superposition for MultiplyLocated<V, L>
where
L: LocationSet,
{
/// Constructs a struct located at another location
fn remote() -> Self {
MultiplyLocated {
value: None,
phantom: PhantomData,
}
}
}

/// Represents a mapping from location names to values
pub struct Quire<V, L>
where
Expand Down Expand Up @@ -1495,4 +1476,4 @@ impl<RunnerLS: LocationSet> Runner<RunnerLS> {
}

extern crate chorus_derive;
pub use chorus_derive::{ChoreographyLocation, Superposition};
pub use chorus_derive::ChoreographyLocation;

0 comments on commit 5ac0739

Please sign in to comment.