16dbb5987Sopenharmony_ci// Copyright (c) 2023 Huawei Device Co., Ltd.
26dbb5987Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
36dbb5987Sopenharmony_ci// you may not use this file except in compliance with the License.
46dbb5987Sopenharmony_ci// You may obtain a copy of the License at
56dbb5987Sopenharmony_ci//
66dbb5987Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
76dbb5987Sopenharmony_ci//
86dbb5987Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
96dbb5987Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
106dbb5987Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
116dbb5987Sopenharmony_ci// See the License for the specific language governing permissions and
126dbb5987Sopenharmony_ci// limitations under the License.
136dbb5987Sopenharmony_ci
146dbb5987Sopenharmony_ci//! `ylong_http_client` `Request` reference.
156dbb5987Sopenharmony_ci
166dbb5987Sopenharmony_ciuse std::cell::UnsafeCell;
176dbb5987Sopenharmony_ciuse std::sync::Arc;
186dbb5987Sopenharmony_ci
196dbb5987Sopenharmony_ciuse crate::async_impl::Request;
206dbb5987Sopenharmony_ci
216dbb5987Sopenharmony_cipub(crate) struct ReqCell {
226dbb5987Sopenharmony_ci    request: UnsafeCell<Request>,
236dbb5987Sopenharmony_ci}
246dbb5987Sopenharmony_ci
256dbb5987Sopenharmony_ciimpl ReqCell {
266dbb5987Sopenharmony_ci    pub(crate) fn new(request: Request) -> Self {
276dbb5987Sopenharmony_ci        Self {
286dbb5987Sopenharmony_ci            request: UnsafeCell::new(request),
296dbb5987Sopenharmony_ci        }
306dbb5987Sopenharmony_ci    }
316dbb5987Sopenharmony_ci}
326dbb5987Sopenharmony_ci
336dbb5987Sopenharmony_ciunsafe impl Sync for ReqCell {}
346dbb5987Sopenharmony_ci
356dbb5987Sopenharmony_cipub(crate) struct RequestArc {
366dbb5987Sopenharmony_ci    pub(crate) cell: Arc<ReqCell>,
376dbb5987Sopenharmony_ci}
386dbb5987Sopenharmony_ci
396dbb5987Sopenharmony_ciimpl RequestArc {
406dbb5987Sopenharmony_ci    pub(crate) fn new(request: Request) -> Self {
416dbb5987Sopenharmony_ci        Self {
426dbb5987Sopenharmony_ci            cell: Arc::new(ReqCell::new(request)),
436dbb5987Sopenharmony_ci        }
446dbb5987Sopenharmony_ci    }
456dbb5987Sopenharmony_ci
466dbb5987Sopenharmony_ci    pub(crate) fn ref_mut(&mut self) -> &mut Request {
476dbb5987Sopenharmony_ci        // SAFETY: In the case of `HTTP`, only one coroutine gets the handle
486dbb5987Sopenharmony_ci        // at the same time.
496dbb5987Sopenharmony_ci        unsafe { &mut *self.cell.request.get() }
506dbb5987Sopenharmony_ci    }
516dbb5987Sopenharmony_ci}
526dbb5987Sopenharmony_ci
536dbb5987Sopenharmony_ciimpl Clone for RequestArc {
546dbb5987Sopenharmony_ci    fn clone(&self) -> Self {
556dbb5987Sopenharmony_ci        Self {
566dbb5987Sopenharmony_ci            cell: self.cell.clone(),
576dbb5987Sopenharmony_ci        }
586dbb5987Sopenharmony_ci    }
596dbb5987Sopenharmony_ci}
60