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 16use std::cell::UnsafeCell; 17use std::sync::Arc; 18 19use crate::async_impl::Request; 20 21pub(crate) struct ReqCell { 22 request: UnsafeCell<Request>, 23} 24 25impl ReqCell { 26 pub(crate) fn new(request: Request) -> Self { 27 Self { 28 request: UnsafeCell::new(request), 29 } 30 } 31} 32 33unsafe impl Sync for ReqCell {} 34 35pub(crate) struct RequestArc { 36 pub(crate) cell: Arc<ReqCell>, 37} 38 39impl RequestArc { 40 pub(crate) fn new(request: Request) -> Self { 41 Self { 42 cell: Arc::new(ReqCell::new(request)), 43 } 44 } 45 46 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 53impl Clone for RequestArc { 54 fn clone(&self) -> Self { 55 Self { 56 cell: self.cell.clone(), 57 } 58 } 59} 60