Skip to main content

jvm_bytecode/
array_class_definition.rs

1use alloc::{
2    borrow::Cow,
3    boxed::Box,
4    string::{String, ToString},
5    sync::Arc,
6};
7use core::fmt::{self, Debug, Formatter};
8
9use jvm::{ArrayClassDefinition, ClassInstance, Jvm, Result};
10
11use crate::array_class_instance::ArrayClassInstanceImpl;
12
13struct ArrayClassDefinitionInner {
14    element_type_name: String,
15}
16
17#[derive(Clone)]
18pub struct ArrayClassDefinitionImpl {
19    inner: Arc<ArrayClassDefinitionInner>,
20}
21
22impl ArrayClassDefinitionImpl {
23    pub fn new(element_type_name: &str) -> Self {
24        Self {
25            inner: Arc::new(ArrayClassDefinitionInner {
26                element_type_name: element_type_name.to_string(),
27            }),
28        }
29    }
30}
31
32#[async_trait::async_trait]
33impl ArrayClassDefinition for ArrayClassDefinitionImpl {
34    fn element_type_name(&self) -> Cow<'_, str> {
35        (&self.inner.element_type_name).into()
36    }
37
38    async fn instantiate_array(&self, _: &Jvm, length: usize) -> Result<Box<dyn ClassInstance>> {
39        Ok(Box::new(ArrayClassInstanceImpl::new(self, length)))
40    }
41}
42
43impl Debug for ArrayClassDefinitionImpl {
44    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
45        write!(f, "ArrayClass({})", self.inner.element_type_name)
46    }
47}