Hacked together object system

pull/1/head
Able 2023-03-30 13:53:52 -05:00
parent 248f9442b9
commit c3b557fab0
6 changed files with 145 additions and 3 deletions

1
Cargo.lock generated
View File

@ -330,6 +330,7 @@ dependencies = [
name = "std"
version = "0.1.0"
dependencies = [
"hashbrown 0.13.1",
"versioning",
]

View File

@ -17,8 +17,9 @@ impl AudioInterface for AudioObject {
Ok(())
}
fn stop() -> Result<(), AudioErrors> {
todo!();
fn stop(&mut self) -> Result<(), AudioErrors> {
self.stop();
self.playback_ring = [0; 2048];
Ok(())
}
@ -33,7 +34,7 @@ pub trait AudioInterface {
fn play() -> Result<(), AudioErrors>;
// Stop will clear the buffer and stop playback
fn stop() -> Result<(), AudioErrors>;
fn stop(&mut self) -> Result<(), AudioErrors>;
fn fill_playback_buffer(&mut self, playback_slice: [u8; 128]) -> Result<(), AudioErrors>;
}

View File

@ -7,3 +7,4 @@ edition = "2021"
[dependencies]
versioning = { path = "../versioning" }
hashbrown = "0.13"

View File

@ -2,12 +2,19 @@
#![feature(panic_info_message)]
#![no_std]
#[cfg(not(test))]
mod entry;
pub mod env;
pub mod exit;
pub mod io;
pub mod object;
#[cfg(not(test))]
pub mod panic;
extern crate alloc;
use core::{arch::asm, fmt::Display};
#[prelude_import]

View File

@ -0,0 +1,84 @@
pub mod types;
use types::*;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use hashbrown::HashMap;
#[derive(Debug)]
pub enum ObjectErrors {
PropertyNotFound,
NoProperties,
FailureToInsert,
}
pub struct Object {
properties: HashMap<Types, Types>,
children: Option<Vec<Object>>,
}
impl Object {
pub fn new(name: Types) -> Self {
let mut obj = Self {
properties: HashMap::new(),
children: None,
};
let _ = obj.set_value("name".into(), name);
obj
}
pub fn get_value(&mut self, key: Types) -> Result<Types, ObjectErrors> {
use ObjectErrors::*;
if self.properties.is_empty() {
return Err(NoProperties);
}
let ret = self.properties.get(&key).unwrap();
Ok(ret.clone())
}
pub fn set_value(&mut self, key: Types, value: Types) -> Result<(), ObjectErrors> {
let ret = self.properties.insert(key, value);
match ret {
Some(_) => {}
None => return Err(ObjectErrors::FailureToInsert),
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::{types::Types, Object};
#[test]
fn object_creation() {
let obj = Object::new(Types::new("hi"));
}
#[test]
fn property_set() {
let key = Types::Byte(8);
let value = Types::Byte(9);
let mut obj = Object::new("hi".into());
obj.set_value(key, value);
}
#[test]
fn property_read() {
let key = Types::new("name");
let mut obj = Object::new("hi".into());
obj.set_value("name".into(), "hi".into()).unwrap();
let ret = obj.get_value(key);
match ret {
Ok(val) => assert_eq!(val, Types::new("hi")),
Err(_err) => todo!(),
}
}
}
#[inline]
fn type_of<T>(_: &T) -> &str {
core::any::type_name::<T>()
}

View File

@ -0,0 +1,48 @@
use core::fmt::Debug;
use core::fmt::Display;
use alloc::string::{String, ToString};
use super::type_of;
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub enum Types {
Null,
Byte(u8),
String(String),
}
impl Types {
pub fn new<T: Debug + Display>(value: T) -> Self {
let type_of = type_of(&value);
match type_of {
"&str" => return Self::String(value.to_string()),
"alloc::string::String" => return Self::String(value.to_string()),
type_name => {
panic!(
"
Invalid type: {}
{}",
value, type_name
);
}
}
}
}
impl From<u8> for Types {
fn from(value: u8) -> Self {
Self::Byte(value)
}
}
impl From<String> for Types {
fn from(value: String) -> Self {
Self::String(value)
}
}
impl From<&str> for Types {
fn from(value: &str) -> Self {
Self::String(value.to_string())
}
}