pub async fn write<T: AsyncRead + Unpin + Send + Sync>(
        &self,
        inv: crate::Invoice,
        parcels: HashMap<String, T>,
    ) -> Result<()> {
        validate_shas(&inv, parcels.keys())?;

        // Should create all the directories needed down to the parcels directory
        tokio::fs::create_dir_all(self.base_path.join(PARCEL_DIR)).await?;

        // Write the invoice into the directory
        write_invoice(&self.base_path, &inv).await?;

        // TODO(thomastaylor312): we might be able to dedup this and the work done in the other
        // function, but I don't want to mess with an async FnMut constraint right now
        let parcel_writes = parcels.into_iter().map(|(sha, mut reader)| async move {
            let path = self.base_path.join(PARCEL_DIR).join(format!("{}.dat", sha));

            let mut file = tokio::fs::OpenOptions::new()
                .write(true)
                .create_new(true) // Make sure we aren't overwriting
                .open(&path)
                .await?;

            debug!("Writing parcel to {}", path.display());
            tokio::io::copy(&mut reader, &mut file).await?;
            file.flush().await?;
            debug!("Finished writing parcel to {}", path.display());
            Ok(())
        });
        futures::future::join_all(parcel_writes)
            .await
            .into_iter()
            .collect::<std::io::Result<Vec<_>>>()?;
        Ok(())
    }
/// Writes the given invoice and `HashMap` of parcels (as readers). The key
/// of the `HashMap` should be the SHA of the parcel
Writes the given invoice and `HashMap` of parcels (as readers). The key
of the `HashMap` should be the SHA of the parcel
Writes the given invoice and ` HashMap ` of parcels ( as readers ) . The key of the ` HashMap ` should be the SHA of the parcel
pub async fn write<T: AsyncRead + Unpin + Send + Sync>(
        &self,
        inv: crate::Invoice,
        parcels: HashMap<String, T>,
    ) -> Result<()> {
        validate_shas(&inv, parcels.keys())?;
        // Should create all the directories needed down to the parcels directory
        tokio::fs::create_dir_all(self.base_path.join(PARCEL_DIR)).await?;
        // Write the invoice into the directory
        write_invoice(&self.base_path, &inv).await?;
        // TODO(thomastaylor312): we might be able to dedup this and the work done in the other
        // function, but I don't want to mess with an async FnMut constraint right now
        let parcel_writes = parcels.into_iter().map(|(sha, mut reader)| async move {
            let path = self.base_path.join(PARCEL_DIR).join(format!("{}.dat", sha));
            let mut file = tokio::fs::OpenOptions::new()
                .write(true)
                .create_new(true) // Make sure we aren't overwriting
                .open(&path)
                .await?;
            debug!("Writing parcel to {}", path.display());
            tokio::io::copy(&mut reader, &mut file).await?;
            file.flush().await?;
            debug!("Finished writing parcel to {}", path.display());
            Ok(())
        });
        futures::future::join_all(parcel_writes)
            .await
            .into_iter()
            .collect::<std::io::Result<Vec<_>>>()?;
        Ok(())
    }
pub async fn write < T : AsyncRead + Unpin + Send + Sync > ( & self , inv : crate :: Invoice , parcels : HashMap < String , T > , ) -> Result < ( ) > { validate_shas ( & inv , parcels . keys ( ) ) ? ; // Should create all the directories needed down to the parcels directory tokio :: fs :: create_dir_all ( self . base_path . join ( PARCEL_DIR ) ) . await ? ; // Write the invoice into the directory write_invoice ( & self . base_path , & inv ) . await ? ; // TODO(thomastaylor312): we might be able to dedup this and the work done in the other // function, but I don't want to mess with an async FnMut constraint right now let parcel_writes = parcels . into_iter ( ) . map ( | ( sha , mut reader ) | async move { let path = self . base_path . join ( PARCEL_DIR ) . join ( format ! ( " " sha ) ) ; let mut file = tokio :: fs :: OpenOptions :: new ( ) . write ( true ) . create_new ( true ) // Make sure we aren't overwriting . open ( & path ) . await ? ; debug ! ( " " path display ( ) ) ; tokio :: io :: copy ( & mut reader , & mut file ) . await ? ; file . flush ( ) . await ? ; debug ! ( " " path display ( ) ) ; Ok ( ( ) ) } ) ; futures :: future :: join_all ( parcel_writes ) . await . into_iter ( ) . collect :: < std :: io :: Result < Vec < _ > > > ( ) ? ; Ok ( ( ) ) }
Writes the given invoice and `HashMap` of parcels (as readers).
Writes the given invoice and ` HashMap ` of parcels ( as readers ) .
&self
inv
crate::Invoice
parcels
HashMap<String, T>
&self
inv
crate::Invoice
parcels
HashMap<String, T>