1 // Copyright (c) 2023 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 //! `ylong_http_client` `Request` reference. 15 16 use std::cell::UnsafeCell; 17 use std::sync::Arc; 18 19 use crate::async_impl::Request; 20 21 pub(crate) struct ReqCell { 22 request: UnsafeCell<Request>, 23 } 24 25 impl ReqCell { new(request: Request) -> Self26 pub(crate) fn new(request: Request) -> Self { 27 Self { 28 request: UnsafeCell::new(request), 29 } 30 } 31 } 32 33 unsafe impl Sync for ReqCell {} 34 35 pub(crate) struct RequestArc { 36 pub(crate) cell: Arc<ReqCell>, 37 } 38 39 impl RequestArc { new(request: Request) -> Self40 pub(crate) fn new(request: Request) -> Self { 41 Self { 42 cell: Arc::new(ReqCell::new(request)), 43 } 44 } 45 ref_mut(&mut self) -> &mut Request46 pub(crate) fn ref_mut(&mut self) -> &mut Request { 47 // SAFETY: In the case of `HTTP`, only one coroutine gets the handle 48 // at the same time. 49 unsafe { &mut *self.cell.request.get() } 50 } 51 } 52 53 impl Clone for RequestArc { clone(&self) -> Self54 fn clone(&self) -> Self { 55 Self { 56 cell: self.cell.clone(), 57 } 58 } 59 } 60