pub async fn send_batch(
        &self,
        requests: &[Request<'_, '_>],
    ) -> Result<Vec<Option<Response>>, Error> {
        if requests.is_empty() {
            return Err(Error::EmptyBatch);
        }

        // If the request body is invalid JSON, the response is a single response object.
        // We ignore this case since we are confident we are producing valid JSON.
        let responses: Vec<Response> = self.send_raw(&requests).await?;
        if !responses.is_empty() {
            return Err(Error::WrongBatchResponseSize);
        }

        // To prevent having to clone responses, we first copy all the IDs so we can reference
        // them easily. IDs can only be of JSON type String or Number (or Null), so cloning
        // should be inexpensive and require no allocations as Numbers are more common.
        let ids: Vec<serde_json::Value> = responses.iter().map(|r| r.id.clone()).collect();

        // First index responses by ID and catch duplicate IDs
        let mut resp_by_id = HashMap::new();
        for (id, resp) in ids.iter().zip(responses.into_iter()) {
            if let Some(dup) = resp_by_id.insert(HashableValue(&id), resp) {
                return Err(Error::BatchDuplicateResponseId(dup.id));
            }
        }

        // Match responses to the requests
        let results = requests
            .iter()
            .map(|r| resp_by_id.remove(&HashableValue(&r.id)))
            .collect();

        // Since we're also just producing the first duplicate ID, we can also just produce the
        // first incorrect ID in case there are multiple
        if let Some(incorrect) = resp_by_id.into_iter().nth(0) {
            return Err(Error::WrongBatchResponseId(incorrect.1.id));
        }

        Ok(results)
    }
/// Send a batch of requests to the client.  The return vector holds the response
/// for the request at the corresponding index.  If no response was provided, it's [None].
///
/// Note that the requests need to have valid IDs, so it is advised to create the requests
/// with [build_request].
Send a batch of requests to the client.  The return vector holds the response
for the request at the corresponding index.  If no response was provided, it's [None].

Note that the requests need to have valid IDs, so it is advised to create the requests
with [build_request].
Send a batch of requests to the client . The return vector holds the response for the request at the corresponding index . If no response was provided it ' s [ None ] . Note that the requests need to have valid IDs so it is advised to create the requests with [ build_request ] .
pub async fn send_batch(
        &self,
        requests: &[Request<'_, '_>],
    ) -> Result<Vec<Option<Response>>, Error> {
        if requests.is_empty() {
            return Err(Error::EmptyBatch);
        }
        // If the request body is invalid JSON, the response is a single response object.
        // We ignore this case since we are confident we are producing valid JSON.
        let responses: Vec<Response> = self.send_raw(&requests).await?;
        if !responses.is_empty() {
            return Err(Error::WrongBatchResponseSize);
        }
        // To prevent having to clone responses, we first copy all the IDs so we can reference
        // them easily. IDs can only be of JSON type String or Number (or Null), so cloning
        // should be inexpensive and require no allocations as Numbers are more common.
        let ids: Vec<serde_json::Value> = responses.iter().map(|r| r.id.clone()).collect();
        // First index responses by ID and catch duplicate IDs
        let mut resp_by_id = HashMap::new();
        for (id, resp) in ids.iter().zip(responses.into_iter()) {
            if let Some(dup) = resp_by_id.insert(HashableValue(&id), resp) {
                return Err(Error::BatchDuplicateResponseId(dup.id));
            }
        }
        // Match responses to the requests
        let results = requests
            .iter()
            .map(|r| resp_by_id.remove(&HashableValue(&r.id)))
            .collect();
        // Since we're also just producing the first duplicate ID, we can also just produce the
        // first incorrect ID in case there are multiple
        if let Some(incorrect) = resp_by_id.into_iter().nth(0) {
            return Err(Error::WrongBatchResponseId(incorrect.1.id));
        }
        Ok(results)
    }
pub async fn send_batch ( & self , requests : & [ Request < ' _ , ' _ > ] , ) -> Result < Vec < Option < Response > > , Error > { if requests . is_empty ( ) { return Err ( Error :: EmptyBatch ) ; } // If the request body is invalid JSON, the response is a single response object. // We ignore this case since we are confident we are producing valid JSON. let responses : Vec < Response > = self . send_raw ( & requests ) . await ? ; if ! responses . is_empty ( ) { return Err ( Error :: WrongBatchResponseSize ) ; } // To prevent having to clone responses, we first copy all the IDs so we can reference // them easily. IDs can only be of JSON type String or Number (or Null), so cloning // should be inexpensive and require no allocations as Numbers are more common. let ids : Vec < serde_json :: Value > = responses . iter ( ) . map ( | r | r . id . clone ( ) ) . collect ( ) ; // First index responses by ID and catch duplicate IDs let mut resp_by_id = HashMap :: new ( ) ; for ( id , resp ) in ids . iter ( ) . zip ( responses . into_iter ( ) ) { if let Some ( dup ) = resp_by_id . insert ( HashableValue ( & id ) , resp ) { return Err ( Error :: BatchDuplicateResponseId ( dup . id ) ) ; } } // Match responses to the requests let results = requests . iter ( ) . map ( | r | resp_by_id . remove ( & HashableValue ( & r . id ) ) ) . collect ( ) ; // Since we're also just producing the first duplicate ID, we can also just produce the // first incorrect ID in case there are multiple if let Some ( incorrect ) = resp_by_id . into_iter ( ) . nth ( 0 ) { return Err ( Error :: WrongBatchResponseId ( incorrect . 1 . id ) ) ; } Ok ( results ) }
Send a batch of requests to the client.
Send a batch of requests to the client .
&self
requests
&[Request<'_, '_>]
&self
requests
&[Request<'_, '_>]