diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-04-02 15:47:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-02 09:47:57 -0400 |
commit | 058579da562989ed15c86598053644bbc86c6747 (patch) | |
tree | 7f0f2bf30684dcbb350b93d987771f17a4abd250 /op_crates/fetch/lib.rs | |
parent | adf57610904cb4f4ef25fb077f6e39c9017a4ea9 (diff) |
refactor(ops): remove variadic buffers (#9944)
Diffstat (limited to 'op_crates/fetch/lib.rs')
-rw-r--r-- | op_crates/fetch/lib.rs | 35 |
1 files changed, 14 insertions, 21 deletions
diff --git a/op_crates/fetch/lib.rs b/op_crates/fetch/lib.rs index c35c675ad..19f2566c4 100644 --- a/op_crates/fetch/lib.rs +++ b/op_crates/fetch/lib.rs @@ -4,6 +4,7 @@ use deno_core::error::bad_resource_id; use deno_core::error::generic_error; +use deno_core::error::null_opbuf; use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::futures::Future; @@ -13,7 +14,6 @@ use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::url::Url; use deno_core::AsyncRefCell; -use deno_core::BufVec; use deno_core::CancelFuture; use deno_core::CancelHandle; use deno_core::CancelTryFuture; @@ -124,7 +124,7 @@ pub struct FetchArgs { pub fn op_fetch<FP>( state: &mut OpState, args: FetchArgs, - data: &mut [ZeroCopyBuf], + data: Option<ZeroCopyBuf>, ) -> Result<Value, AnyError> where FP: FetchPermissions + 'static, @@ -165,8 +165,8 @@ where let mut request = client.request(method, url); let maybe_request_body_rid = if args.has_body { - match data.len() { - 0 => { + match data { + None => { // If no body is passed, we return a writer for streaming the body. let (tx, rx) = mpsc::channel::<std::io::Result<Vec<u8>>>(1); request = request.body(Body::wrap_stream(ReceiverStream::new(rx))); @@ -179,12 +179,11 @@ where Some(request_body_rid) } - 1 => { + Some(data) => { // If a body is passed, we use it, and don't return a body for streaming. - request = request.body(Vec::from(&*data[0])); + request = request.body(Vec::from(&*data)); None } - _ => panic!("Invalid number of arguments"), } } else { None @@ -217,7 +216,7 @@ pub struct FetchSendArgs { pub async fn op_fetch_send( state: Rc<RefCell<OpState>>, args: FetchSendArgs, - _data: BufVec, + _data: Option<ZeroCopyBuf>, ) -> Result<Value, AnyError> { let request = state .borrow_mut() @@ -285,14 +284,11 @@ pub struct FetchRequestWriteArgs { pub async fn op_fetch_request_write( state: Rc<RefCell<OpState>>, args: FetchRequestWriteArgs, - data: BufVec, + data: Option<ZeroCopyBuf>, ) -> Result<Value, AnyError> { let rid = args.rid; - - let buf = match data.len() { - 1 => Vec::from(&*data[0]), - _ => panic!("Invalid number of arguments"), - }; + let data = data.ok_or_else(null_opbuf)?; + let buf = Vec::from(&*data); let resource = state .borrow() @@ -315,13 +311,10 @@ pub struct FetchResponseReadArgs { pub async fn op_fetch_response_read( state: Rc<RefCell<OpState>>, args: FetchResponseReadArgs, - data: BufVec, + data: Option<ZeroCopyBuf>, ) -> Result<Value, AnyError> { let rid = args.rid; - - if data.len() != 1 { - panic!("Invalid number of arguments"); - } + let data = data.ok_or_else(null_opbuf)?; let resource = state .borrow() @@ -330,7 +323,7 @@ pub async fn op_fetch_response_read( .ok_or_else(bad_resource_id)?; let mut reader = RcRef::map(&resource, |r| &r.reader).borrow_mut().await; let cancel = RcRef::map(resource, |r| &r.cancel); - let mut buf = data[0].clone(); + let mut buf = data.clone(); let read = reader.read(&mut buf).try_or_cancel(cancel).await?; Ok(json!({ "read": read })) } @@ -397,7 +390,7 @@ pub struct CreateHttpClientOptions { pub fn op_create_http_client<FP>( state: &mut OpState, args: CreateHttpClientOptions, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option<ZeroCopyBuf>, ) -> Result<Value, AnyError> where FP: FetchPermissions + 'static, |