0% found this document useful (0 votes)
14 views2 pages

Rust Object Management with Mutex

The document defines a generic struct `Objects` that manages a collection of `Object` instances using a `HashMap` and a thread-safe list for adding new objects. It includes methods for adding objects, retrieving them by name, and accessing the internal object list. Additionally, the `Object` struct encapsulates an object type and a drawing mechanism.

Uploaded by

zzirioss
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Rust Object Management with Mutex

The document defines a generic struct `Objects` that manages a collection of `Object` instances using a `HashMap` and a thread-safe list for adding new objects. It includes methods for adding objects, retrieving them by name, and accessing the internal object list. Additionally, the `Object` struct encapsulates an object type and a drawing mechanism.

Uploaded by

zzirioss
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

use std::sync::{Mutex, Arc};

use hashbrown::HashMap;

use crate::draw::Draw;

#[derive(Default)]
pub struct Objects<T> {
object_list: HashMap<String, Object<T>>,
object_add_list: Arc<Mutex<Vec<(String, Object<T>)>>>
}

impl<T> Objects<T> {
pub fn add(&self, name: &str, object: Object<T>) {
let mut object_add_list_lock = self.object_add_list
.lock()
.expect("Failed add object to object_add_list");
object_add_list_lock.push((name.to_string(), object));
}

pub fn get(&self, name: &str) -> Option<&T> {


match self.object_list.get(name) {
Some(a) => Some([Link]()),
None => None,
}
}

pub(crate) fn get_object_list(&self) -> &HashMap<String, Object<T>> {


&self.object_list
}

pub(crate) fn update(&mut self) {

}
}

pub struct Object<T> {


draw: Draw,
object_type: T,
}

impl<T> Object<T> {
pub(crate) fn new(object_type: T, draw: Draw) -> Self {
Self {
object_type,
draw,
}
}

pub fn obj(&self) -> &T {


&self.object_type
}
}

/*pub struct ObjRef<'a, T> {


object_add_list_lock: MutexGuard<'a, Vec<(String, T)>>
}
impl<'a, T> ObjRef<'a, T> {
pub(crate) fn new(mutex: MutexGuard<'a, Vec<(String, T)>>) -> Self {
Self {
object_add_list_lock: mutex
}
}

pub fn obj(&self) -> &T {


&self.object_add_list_lock
.last()
.expect("Failed get obj for objRef")
.1
}

pub fn name(&self) -> &String {


&self.object_add_list_lock
.last()
.expect("Failed get name for objRef")
.0
}
} */

You might also like