Repository: AmbientRun/Ambient
File: crates/native_std/src/uncategorized/download_asset.rs (Rust)

Code before:
                    .map_err(anyhow::Error::new)?
                    .0,
                {
                    let tmp_path = tmp_path.clone();
                    move |mut resp| {
                        let tmp_path = tmp_path.clone();
                        async move {
                            let mut file = tokio::fs::File::create(&tmp_path)
                                .await
                                .context(format!("Failed to create file: {tmp_path:?}"))?;
                            use std::borrow::BorrowMut;
                            while let Some(mut item) =
                                resp.chunk().await.context("Failed to download chunk")?
                            {
                                file.write_all_buf(item.borrow_mut())
                                    .await
                                    .context("Failed to write to tmp file")?;
                            }
                            file.flush().await.context("Failed to flush tmp file")?;
                            Ok(())
                        }
                    }
                },
            )
            .await?;
            std::fs::rename(&tmp_path, &path).context(format!(
                "Failed to rename tmp file, from: {tmp_path:?}, to: {path:?}"
            ))?;
            log::info!("Cached asset at {:?}", path);
        }

        return Ok(Arc::new(path));
    }
}

/// Limit the number of concurent file reads to 10
#[derive(Debug)]
struct FileReadSemaphore;
impl SyncAssetKey<Arc<Semaphore>> for FileReadSemaphore {
    fn load(&self, _assets: AssetCache) -> Arc<Semaphore> {
        Arc::new(Semaphore::new(10))
    }
}

/// Limit the number of concurent downloads to 5
#[derive(Debug)]
struct DownloadSemaphore;
impl SyncAssetKey<Arc<Semaphore>> for DownloadSemaphore {
    fn load(&self, _assets: AssetCache) -> Arc<Semaphore> {
        Arc::new(Semaphore::new(5))
    }

Review comment (suggestion):
```suggestion
/// Limit the number of concurrent file reads to 10
```

Code after:
                            use std::borrow::BorrowMut;
                            while let Some(mut item) =
                                resp.chunk().await.context("Failed to download chunk")?
                            {
                                file.write_all_buf(item.borrow_mut())
                                    .await
                                    .context("Failed to write to tmp file")?;
                            }
                            file.flush().await.context("Failed to flush tmp file")?;
                            Ok(())
                        }
                    }
                },
            )
            .await?;
            std::fs::rename(&tmp_path, &path).context(format!(
                "Failed to rename tmp file, from: {tmp_path:?}, to: {path:?}"
            ))?;
            log::info!("Cached asset at {:?}", path);
        }

        return Ok(Arc::new(path));
    }
}

/// Limit the number of conccurent file reads to 10
#[derive(Debug)]
struct FileReadSemaphore;
impl SyncAssetKey<Arc<Semaphore>> for FileReadSemaphore {
    fn load(&self, _assets: AssetCache) -> Arc<Semaphore> {
        Arc::new(Semaphore::new(10))
    }
}

/// Limit the number of concurent downloads to 5
#[derive(Debug)]
struct DownloadSemaphore;
impl SyncAssetKey<Arc<Semaphore>> for DownloadSemaphore {
    fn load(&self, _assets: AssetCache) -> Arc<Semaphore> {
        Arc::new(Semaphore::new(5))
    }
}

pub struct JsonFromUrl<T> {
    url: AbsAssetUrl,
    cache_on_disk: bool,
    _type: PhantomData<T>,
}

impl<T> Clone for JsonFromUrl<T> {
    fn clone(&self) -> Self {