i been trying to implement a singly linked list on a microcontroller, so cant use use heap, cant use heapless crate due to my list size is unknown. solution i thought is everytime i need a node i will create it in bottom function stack, and everywhere else i will use reference to it. implementation is somewhat like this.
struct Node<'a, T>{
object: RefCell<T>,
next: Option<&'a Node<'a, T>>
}
struct List{
head: Option<&'a Node<'a, T>>
}
impl<'a, T:Object> List<'a, T>{
pub fn push(&mut self, node: &'a Node<'a, T>){
¦ self.head = Some(node)
}
Now in main function i am creating Node and pushing a reference to that node to List, like
let head = None;
let node = Node{ RefCell::new(object), next: None};
head.push(&node);
Until this everything is working fine, but the problem is when i conditionals or loops, i have problem like let say i want to create 5 nodes then if i create those nodes inside of for loop then at the end of the loop nodes will be dropped and cant have dangling pointers.
i failed to find workaround this, any help?
if any way i can use conditionals without dropping my Nodes at the end of block.
ex:
let node;
if node_count > 0{
node = Node::new(
Object::new(..<some values>..),
node_list.head.take()
);
// Here i get borrowed value does not live long enough
node_list.push(&enemy);
};
I have to find a way to it, its a dead end for my personal project