This commit is contained in:
Jonas Kruckenberg
2023-09-10 14:00:39 +02:00
parent 5059b872f2
commit 64f3d3fc10
4 changed files with 54 additions and 22 deletions

View File

@@ -314,8 +314,7 @@ export async function {ident} ({params}) : {result} {{
let result = func
.result
.as_ref()
.map(|result| self.print_function_result(result))
.unwrap_or("void".to_string());
.map_or("void".to_string(), |result| self.print_function_result(result));
let deserialize_result = func
.result

View File

@@ -5,15 +5,13 @@
clippy::unused_self
)]
use std::borrow::Cow;
use std::collections::HashSet;
use std::path::PathBuf;
use heck::ToKebabCase;
use heck::{ToSnakeCase, ToUpperCamelCase};
use proc_macro2::TokenStream;
use quote::format_ident;
use quote::quote;
use std::collections::HashSet;
use std::path::PathBuf;
use tauri_bindgen_core::{Generate, GeneratorBuilder, TypeInfo, TypeInfos};
use tauri_bindgen_gen_rust::{print_generics, BorrowMode, FnSig, RustGenerator};
use wit_parser::{Function, FunctionResult, Interface, Type, TypeDefKind};
@@ -378,7 +376,7 @@ impl Host {
_ => quote! { () },
};
let mod_name = format!("{}::resource::{}", mod_name, resource_name);
let mod_name = format!("{mod_name}::resource::{resource_name}");
let get_r_ident = format_ident!("get_{}", resource_name.to_snake_case());
quote! {

View File

@@ -741,7 +741,7 @@ impl SerdeUtils {
info |= Self::collect_type_info(typedefs, &case.ty);
}
}
TypeDefKind::Enum(_) => {
TypeDefKind::Enum(_) | TypeDefKind::Resource(_) => {
info |= SerdeUtils::U32;
}
TypeDefKind::Flags(fields) => {
@@ -753,9 +753,6 @@ impl SerdeUtils {
wit_parser::Int::U128 => SerdeUtils::U128,
};
}
TypeDefKind::Resource(_) => {
info |= SerdeUtils::U32;
}
}
log::debug!("collected info for {:?}: {:?}", typedefs[id].ident, info,);

View File

@@ -1,4 +1,3 @@
use std::marker::PhantomData;
use std::sync::Arc;
use std::sync::RwLock;
use std::{any::Any, collections::HashMap};
@@ -20,7 +19,8 @@ pub struct ResourceTableInner {
}
impl ResourceTable {
/// Create an empty table. New insertions will begin at 3, above stdio.
/// Create an empty table.
#[must_use]
pub fn new() -> Self {
Self(RwLock::new(ResourceTableInner {
map: HashMap::new(),
@@ -29,6 +29,14 @@ impl ResourceTable {
}
/// Insert a resource at the next available index.
///
/// # Errors
///
/// Returns an error if the table is full.
///
/// # Panics
///
/// Panics if the resource is already borrowed.
pub fn push<T: Any + Send + Sync>(&self, a: Arc<T>) -> Result<ResourceId> {
let mut inner = self.0.write().unwrap();
// NOTE: The performance of this new key calculation could be very bad once keys wrap
@@ -48,12 +56,20 @@ impl ResourceTable {
}
/// Check if the table has a resource at the given index.
///
/// # Panics
///
/// Panics if the resource is already borrowed.
pub fn contains_key(&self, key: ResourceId) -> bool {
self.0.read().unwrap().map.contains_key(&key)
}
/// Check if the resource at a given index can be downcast to a given type.
/// Note: this will always fail if the resource is already borrowed.
///
/// # Panics
///
/// Panics if the resource is already borrowed.
pub fn is<T: Any + Sized>(&self, key: ResourceId) -> bool {
if let Some(r) = self.0.read().unwrap().map.get(&key) {
r.is::<T>()
@@ -64,6 +80,14 @@ impl ResourceTable {
/// Get an Arc reference to a resource of a given type at a given index. Multiple
/// immutable references can be borrowed at any given time.
///
/// # Errors
///
/// Returns an error if the resource is not of the given type.
///
/// # Panics
///
/// Panics if the resource is already borrowed.
pub fn get<T: Any + Send + Sync + Sized>(&self, key: ResourceId) -> Result<Arc<T>> {
if let Some(r) = self.0.read().unwrap().map.get(&key).cloned() {
r.downcast::<T>()
@@ -74,22 +98,36 @@ impl ResourceTable {
}
/// Get a mutable reference to a resource of a given type at a given index.
/// Only one such reference can be borrowed at any given time.
///
/// # Errors
///
/// Returns an error if the resource is not of the given type or if the resource is already borrowed.
///
/// # Panics
///
/// Panics if the resource is already borrowed.
pub fn get_mut<T: Any>(&mut self, key: ResourceId) -> Result<&mut T> {
let entry = match self.0.get_mut().unwrap().map.get_mut(&key) {
Some(entry) => entry,
None => return Err(anyhow::anyhow!("key not in table")),
};
let entry = match Arc::get_mut(entry) {
Some(entry) => entry,
None => return Err(anyhow::anyhow!("cannot mutably borrow shared file")),
};
let entry = self
.0
.get_mut()
.unwrap()
.map
.get_mut(&key)
.ok_or(anyhow::anyhow!("key not in table"))?;
let entry =
Arc::get_mut(entry).ok_or(anyhow::anyhow!("cannot mutably borrow shared file"))?;
entry
.downcast_mut::<T>()
.ok_or_else(|| anyhow::anyhow!("element is a different type"))
}
/// Remove a resource at a given index from the table and returns it.
///
/// # Panics
///
/// Panics if the resource is already borrowed.
pub fn take<T: Any + Send + Sync>(&self, key: ResourceId) -> Option<Arc<T>> {
self.0
.write()