Skip to content

Commit

Permalink
Derive works again.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed May 22, 2024
1 parent df5dded commit 7a75d37
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 48 deletions.
44 changes: 22 additions & 22 deletions copper/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum CopperListState {

/// This structure maintains the entire memory needed by Copper for one process for the inter task communication.
#[derive(Debug)]
pub struct CopperListsManager<T: Sized + PartialEq, const N: usize> {
pub struct CuListsManager<T: Sized + PartialEq, const N: usize> {
copper_list_states: [CopperListState; N],
data: Box<[T; N]>,
length: usize,
Expand All @@ -38,7 +38,7 @@ pub type AscIter<'a, T> = Chain<SliceIter<'a, T>, SliceIter<'a, T>>;
/// An mutable ascending iterator over `CircularQueue<T>`.
pub type AscIterMut<'a, T> = Chain<SliceIterMut<'a, T>, SliceIterMut<'a, T>>;

impl<T: Sized + PartialEq, const N: usize> PartialEq for CopperListsManager<T, N> {
impl<T: Sized + PartialEq, const N: usize> PartialEq for CuListsManager<T, N> {
fn eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
Expand All @@ -47,15 +47,15 @@ impl<T: Sized + PartialEq, const N: usize> PartialEq for CopperListsManager<T, N
}
}

impl<T: Sized + PartialEq, const N: usize> CopperListsManager<T, N> {
impl<T: Sized + PartialEq, const N: usize> CuListsManager<T, N> {
pub fn new() -> Self {
let data = unsafe {
let layout = std::alloc::Layout::new::<[T; N]>();
let ptr = std::alloc::alloc_zeroed(layout) as *mut [T; N];
Box::from_raw(ptr)
};
const INITIAL_SLSTATE: CopperListState = CopperListState::Free;
CopperListsManager {
CuListsManager {
copper_list_states: [INITIAL_SLSTATE; N],
data,
length: 0,
Expand Down Expand Up @@ -165,15 +165,15 @@ mod tests {

#[test]
fn empty_queue() {
let q = CopperListsManager::<i32, 5>::new();
let q = CuListsManager::<i32, 5>::new();

assert!(q.is_empty());
assert_eq!(q.iter().next(), None);
}

#[test]
fn partially_full_queue() {
let mut q = CopperListsManager::<i32, 5>::new();
let mut q = CuListsManager::<i32, 5>::new();
*q.create().unwrap() = 1;
*q.create().unwrap() = 2;
*q.create().unwrap() = 3;
Expand All @@ -187,7 +187,7 @@ mod tests {

#[test]
fn full_queue() {
let mut q = CopperListsManager::<_, 5>::new();
let mut q = CuListsManager::<_, 5>::new();
*q.create().unwrap() = 1;
*q.create().unwrap() = 2;
*q.create().unwrap() = 3;
Expand All @@ -201,7 +201,7 @@ mod tests {

#[test]
fn over_full_queue() {
let mut q = CopperListsManager::<_, 5>::new();
let mut q = CuListsManager::<_, 5>::new();
*q.create().unwrap() = 1;
*q.create().unwrap() = 2;
*q.create().unwrap() = 3;
Expand All @@ -216,7 +216,7 @@ mod tests {

#[test]
fn clear() {
let mut q = CopperListsManager::<_, 5>::new();
let mut q = CuListsManager::<_, 5>::new();
*q.create().unwrap() = 1;
*q.create().unwrap() = 2;
*q.create().unwrap() = 3;
Expand All @@ -242,7 +242,7 @@ mod tests {

#[test]
fn mutable_iterator() {
let mut q = CopperListsManager::<_, 5>::new();
let mut q = CuListsManager::<_, 5>::new();
*q.create().unwrap() = 1;
*q.create().unwrap() = 2;
*q.create().unwrap() = 3;
Expand All @@ -259,7 +259,7 @@ mod tests {

#[test]
fn zero_sized() {
let mut q = CopperListsManager::<_, 5>::new();
let mut q = CuListsManager::<_, 5>::new();
*q.create().unwrap() = ();
*q.create().unwrap() = ();
*q.create().unwrap() = ();
Expand All @@ -275,19 +275,19 @@ mod tests {

#[test]
fn empty_queue_eq() {
let q1 = CopperListsManager::<i32, 5>::new();
let q2 = CopperListsManager::<i32, 5>::new();
let q1 = CuListsManager::<i32, 5>::new();
let q2 = CuListsManager::<i32, 5>::new();
assert_eq!(q1, q2);
}

#[test]
fn partially_full_queue_eq() {
let mut q1 = CopperListsManager::<i32, 5>::new();
let mut q1 = CuListsManager::<i32, 5>::new();
*q1.create().unwrap() = 1;
*q1.create().unwrap() = 2;
*q1.create().unwrap() = 3;

let mut q2 = CopperListsManager::<i32, 5>::new();
let mut q2 = CuListsManager::<i32, 5>::new();
*q2.create().unwrap() = 1;
*q2.create().unwrap() = 2;
assert_ne!(q1, q2);
Expand All @@ -301,14 +301,14 @@ mod tests {

#[test]
fn full_queue_eq() {
let mut q1 = CopperListsManager::<i32, 5>::new();
let mut q1 = CuListsManager::<i32, 5>::new();
*q1.create().unwrap() = 1;
*q1.create().unwrap() = 2;
*q1.create().unwrap() = 3;
*q1.create().unwrap() = 4;
*q1.create().unwrap() = 5;

let mut q2 = CopperListsManager::<i32, 5>::new();
let mut q2 = CuListsManager::<i32, 5>::new();
*q2.create().unwrap() = 1;
*q2.create().unwrap() = 2;
*q2.create().unwrap() = 3;
Expand All @@ -320,15 +320,15 @@ mod tests {

#[test]
fn clear_eq() {
let mut q1 = CopperListsManager::<i32, 5>::new();
let mut q1 = CuListsManager::<i32, 5>::new();
*q1.create().unwrap() = 1;
*q1.create().unwrap() = 2;
*q1.create().unwrap() = 3;
*q1.create().unwrap() = 4;
*q1.create().unwrap() = 5;
q1.clear();

let mut q2 = CopperListsManager::<i32, 5>::new();
let mut q2 = CuListsManager::<i32, 5>::new();
assert_eq!(q1, q2);

*q2.create().unwrap() = 1;
Expand All @@ -338,12 +338,12 @@ mod tests {

#[test]
fn zero_sized_eq() {
let mut q1 = CopperListsManager::<_, 3>::new();
let mut q1 = CuListsManager::<_, 3>::new();
*q1.create().unwrap() = ();
*q1.create().unwrap() = ();
*q1.create().unwrap() = ();

let mut q2 = CopperListsManager::<_, 3>::new();
let mut q2 = CuListsManager::<_, 3>::new();
*q2.create().unwrap() = ();
*q2.create().unwrap() = ();
assert_ne!(q1, q2);
Expand All @@ -360,6 +360,6 @@ mod tests {

#[test]
fn be_sure_we_wont_stackoverflow_at_init() {
let _ = CopperListsManager::<[u8; 10_000_000], 3>::new();
let _ = CuListsManager::<[u8; 10_000_000], 3>::new();
}
}
18 changes: 9 additions & 9 deletions copper/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use petgraph::dot::Config as PetConfig;
use petgraph::dot::Dot;
use petgraph::stable_graph::StableDiGraph;
use ron::extensions::Extensions;
use ron::Options;
use ron::value::Value as RonValue;
use ron::Options;
use serde::{Deserialize, Serialize};
use uom::si::rational::Time;
use uom::si::time::nanosecond;
Expand Down Expand Up @@ -138,19 +138,19 @@ impl Node {
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CopperConfig {
pub struct CuConfig {
pub graph: StableDiGraph<Node, String, NodeId>,
}

impl Default for CopperConfig {
impl Default for CuConfig {
fn default() -> Self {
CopperConfig {
CuConfig {
graph: StableDiGraph::new(),
}
}
}

impl CopperConfig {
impl CuConfig {
pub fn add_node(&mut self, node: Node) -> NodeId {
self.graph.add_node(node).index() as NodeId
}
Expand Down Expand Up @@ -239,26 +239,26 @@ mod tests {

#[test]
fn test_plain_serialize() {
let mut config = CopperConfig::default();
let mut config = CuConfig::default();
let n1 = config.add_node(Node::new("test1", "package::Plugin1"));
let n2 = config.add_node(Node::new("test2", "package::Plugin2"));
config.connect(n1, n2, "msgpkg::MsgType");
let serialized = config.serialize();
let deserialized = CopperConfig::deserialize(&serialized);
let deserialized = CuConfig::deserialize(&serialized);
assert_eq!(config.graph.node_count(), deserialized.graph.node_count());
assert_eq!(config.graph.edge_count(), deserialized.graph.edge_count());
}

#[test]
fn test_serialize_with_params() {
let mut config = CopperConfig::default();
let mut config = CuConfig::default();
let mut camera = Node::new("copper-camera", "camerapkg::Camera")
.set_base_period(Time::new::<second>(60.into()));
camera.set_param::<Value>("resolution-height", 1080.into());
config.add_node(camera);
let serialized = config.serialize();
println!("{}", serialized);
let deserialized = CopperConfig::deserialize(&serialized);
let deserialized = CuConfig::deserialize(&serialized);
assert_eq!(
deserialized
.get_node(0)
Expand Down
26 changes: 13 additions & 13 deletions copper_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use syn::meta::parser;
use syn::Fields::{Named, Unit, Unnamed};
use syn::{parse_macro_input, parse_quote, parse_str, Field, ItemStruct, LitStr, Type, TypeTuple};

use copper::config::CopperConfig;
use copper::config::CuConfig;
use format::{highlight_rust_code, rustfmt_generated_code};

mod format;
Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream {
let config_content = std::fs::read_to_string(&config_full_path)
.unwrap_or_else(|_| panic!("Failed to read configuration file: {:?}", &config_full_path));

let copper_config = CopperConfig::deserialize(&config_content);
let copper_config = CuConfig::deserialize(&config_content);

let (all_tasks_types_names, all_tasks_types) = extract_tasks_types(&copper_config);

Expand All @@ -52,15 +52,15 @@ pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream {

// add that to a new field
let task_instances_field: Field = parse_quote! {
task_instances: CopperTasks
task_instances: CuTasks
};

let (all_msgs_types_names, all_msgs_types) = extract_msgs_types(&copper_config);

let msgs_types_tuple: TypeTuple = parse_quote! { (#(#all_msgs_types),*,)};

let copper_lists_field: Field = parse_quote! {
copper_lists: CircularQueue<CopperList, 10>
copper_lists: CuListsManager<CuList, 10>
};

let name = &item_struct.ident;
Expand Down Expand Up @@ -109,17 +109,17 @@ pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream {
// Convert the modified struct back into a TokenStream
let result = quote! {
use std::fs::read_to_string;
use copper::common::CircularQueue;
use copper::config::CuConfig;
use copper::config::Node;
use copper::config::CopperConfig;
use copper::config::NodeInstanceConfig;
use copper::common::CuListsManager;
use copper::cutask::CuSinkTask; // Needed for the instantiation of tasks
use copper::cutask::CuSrcTask; // Needed for the instantiation of tasks
use copper::cutask::CuTask; // Needed for the instantiation of tasks
use copper::cutask::CuSinkTask; // Needed for the instantiation of tasks
use copper::CuResult;

pub type CopperList = #msgs_types_tuple;
pub type CopperTasks = #task_types_tuple;
pub type CuList = #msgs_types_tuple;
pub type CuTasks = #task_types_tuple;

pub #item_struct

Expand All @@ -130,15 +130,15 @@ pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream {

let config_content = read_to_string(config_filename)
.unwrap_or_else(|_| panic!("Failed to read configuration file: {:?}", &config_filename));
let copper_config = CopperConfig::deserialize(&config_content);
let copper_config = CuConfig::deserialize(&config_content);
let all_instances_configs: Vec<Option<&NodeInstanceConfig>> = copper_config.get_all_nodes().iter().map(|node_config| node_config.get_instance_config()).collect();

let task_instances = (
#(#task_instances_init_code),*,
);
Ok(#name {
task_instances,
copper_lists: CircularQueue::new(),
copper_lists: CuListsManager::new(),
})
}

Expand All @@ -161,7 +161,7 @@ pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream {
}

/// Extract all the tasks types in their index order
fn extract_tasks_types(copper_config: &CopperConfig) -> (Vec<String>, Vec<Type>) {
fn extract_tasks_types(copper_config: &CuConfig) -> (Vec<String>, Vec<Type>) {
let all_nodes = copper_config.get_all_nodes();

// Collect all the type names used by our configs.
Expand All @@ -179,7 +179,7 @@ fn extract_tasks_types(copper_config: &CopperConfig) -> (Vec<String>, Vec<Type>)
}

/// Extract all the messages types in their index order
fn extract_msgs_types(copper_config: &CopperConfig) -> (Vec<String>, Vec<Type>) {
fn extract_msgs_types(copper_config: &CuConfig) -> (Vec<String>, Vec<Type>) {
let all_edges = copper_config.get_all_edges();

let all_types_names: Vec<String> = all_edges
Expand Down
4 changes: 2 additions & 2 deletions examples/config-gen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use uom::si::time::second;
use uom::si::time::Time;

use copper::config::CopperConfig;
use copper::config::CuConfig;
use copper::config::Node;
use copper::config::Value;

/// This is a simple example to show how to programmatically generate a configuration.
/// This is useful for making variations of your robot more easily.
fn main() {
// Generate a config
let mut copperconfig = CopperConfig::default();
let mut copperconfig = CuConfig::default();
let mut camera =
Node::new("camera", "camerapkg::Camera").set_base_period(Time::new::<second>(60.into()));
camera.set_param::<Value>("resolution-height", 1080.into());
Expand Down
4 changes: 2 additions & 2 deletions examples/pluginload/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use uom::si::rational::Time;
use uom::si::time::second;

use copper::config::CopperConfig;
use copper::config::CuConfig;
use copper::config::Node;

fn main() {
let mut copperconfig = CopperConfig::default();
let mut copperconfig = CuConfig::default();
let mut camera =
Node::new("copper-camera", "camerapkg::Camera")
.set_base_period(Time::new::<second>(60.into()));
Expand Down

0 comments on commit 7a75d37

Please sign in to comment.