Skip to main content

hyperlane_core/context/
impl.rs

1use super::*;
2
3/// Implementation of `Default` trait for `Context`.
4impl Default for Context {
5    /// Creates a default `Context` instance.
6    ///
7    /// # Returns
8    ///
9    /// - `Context` - A new context with default values and a static default server.
10    #[inline(always)]
11    fn default() -> Self {
12        Self {
13            request: Request::default(),
14            response: Response::default(),
15            route_params: HashMapXxHash3_64::default(),
16            original_target: None,
17            original_head: None,
18            attributes: ThreadSafeAttributeStore::default(),
19        }
20    }
21}
22
23/// Implementation of `PartialEq` trait for `Context`.
24impl PartialEq for Context {
25    /// Compares two `Context` instances for equality.
26    ///
27    /// # Arguments
28    ///
29    /// - `&Self` - The first `Context` instance.
30    /// - `&Self` - The second `Context` instance.
31    ///
32    /// # Returns
33    ///
34    /// - `bool` - True if the instances are equal, otherwise false.
35    #[inline(always)]
36    fn eq(&self, other: &Self) -> bool {
37        self.get_request() == other.get_request()
38            && self.get_response() == other.get_response()
39            && self.get_route_params() == other.get_route_params()
40            && self.get_attributes().len() == other.get_attributes().len()
41    }
42}
43
44/// Implementation of `Eq` trait for `Context`.
45impl Eq for Context {}
46
47/// Structured response serialization for [`Context`].
48impl Context {
49    /// Builds the response for the incoming request without sending it.
50    ///
51    /// A wire `HEAD` request receives only the status line and headers, even if
52    /// middleware rewrites the request method. A standalone context uses its
53    /// current request method. Explicit raw stream writes are not affected.
54    ///
55    /// # Returns
56    ///
57    /// - `Vec<u8>` - The serialized response bytes.
58    #[inline(always)]
59    #[must_use]
60    pub fn build_response(&mut self) -> Vec<u8> {
61        let is_head: bool = self
62            .try_get_original_head()
63            .unwrap_or_else(|| self.get_request().get_method().is_head());
64        if is_head {
65            self.get_mut_response().build_head()
66        } else {
67            self.get_mut_response().build()
68        }
69    }
70
71    /// Appends the serialized response for the incoming request into `out`.
72    ///
73    /// Preserves the same `HEAD` handling as [`Context::build_response`] while
74    /// reusing `out` without per-request heap allocation.
75    ///
76    /// # Arguments
77    ///
78    /// - `&mut Vec<u8>` - Destination wire buffer.
79    #[inline(always)]
80    pub fn build_response_into(&mut self, out: &mut Vec<u8>) {
81        let is_head: bool = self
82            .try_get_original_head()
83            .unwrap_or_else(|| self.get_request().get_method().is_head());
84        if is_head {
85            self.get_mut_response().build_head_into(out);
86        } else {
87            self.get_mut_response().build_into(out);
88        }
89    }
90
91    /// Serializes and sends the response over `stream` without per-response allocation.
92    ///
93    /// Preserves the same `HEAD` handling as [`Context::build_response`].
94    ///
95    /// # Arguments
96    ///
97    /// - `&mut Stream` - The active response stream.
98    ///
99    /// # Returns
100    ///
101    /// - `Result<(), ResponseError>` - Ok on success or transport error.
102    #[inline(always)]
103    pub async fn try_send_response(&mut self, stream: &mut Stream) -> Result<(), ResponseError> {
104        let is_head: bool = self
105            .try_get_original_head()
106            .unwrap_or_else(|| self.get_request().get_method().is_head());
107        if is_head {
108            stream.try_send_response_head(self.get_mut_response()).await
109        } else {
110            stream.try_send_response(self.get_mut_response()).await
111        }
112    }
113}
114
115/// Implementation of `From` trait for converting `usize` address into `&Context`.
116impl From<usize> for &'static Context {
117    /// Converts a memory address into a reference to `Context`.
118    ///
119    /// # Arguments
120    ///
121    /// - `usize` - The memory address of the `Context` instance.
122    ///
123    /// # Returns
124    ///
125    /// - `&'static Context` - A reference to the `Context` at the given address.
126    ///
127    /// # Safety
128    ///
129    /// - The address is guaranteed to be a valid `Context` instance
130    ///   that was previously converted from a reference and is managed by the runtime.
131    #[inline(always)]
132    fn from(address: usize) -> &'static Context {
133        unsafe { &*(address as *const Context) }
134    }
135}
136
137/// Implementation of `From` trait for converting `usize` address into `&mut Context`.
138impl<'a> From<usize> for &'a mut Context {
139    /// Converts a memory address into a mutable reference to `Context`.
140    ///
141    /// # Arguments
142    ///
143    /// - `usize` - The memory address of the `Context` instance.
144    ///
145    /// # Returns
146    ///
147    /// - `&'a mut Context` - A mutable reference to the `Context` at the given address.
148    ///
149    /// # Safety
150    ///
151    /// - The address is guaranteed to be a valid `Context` instance
152    ///   that was previously converted from a reference and is managed by the runtime.
153    #[inline(always)]
154    fn from(address: usize) -> &'a mut Context {
155        unsafe { &mut *(address as *mut Context) }
156    }
157}
158
159/// Implementation of `From` trait for converting `&Context` into `usize` address.
160impl From<&Context> for usize {
161    /// Converts a reference to `Context` into its memory address.
162    ///
163    /// # Arguments
164    ///
165    /// - `&Context` - The reference to the `Context` instance.
166    ///
167    /// # Returns
168    ///
169    /// - `usize` - The memory address of the `Context` instance.
170    #[inline(always)]
171    fn from(ctx: &Context) -> Self {
172        ctx as *const Context as usize
173    }
174}
175
176/// Implementation of `From` trait for converting `&mut Context` into `usize` address.
177impl From<&mut Context> for usize {
178    /// Converts a mutable reference to `Context` into its memory address.
179    ///
180    /// # Arguments
181    ///
182    /// - `&mut Context` - The mutable reference to the `Context` instance.
183    ///
184    /// # Returns
185    ///
186    /// - `usize` - The memory address of the `Context` instance.
187    #[inline(always)]
188    fn from(ctx: &mut Context) -> Self {
189        ctx as *mut Context as usize
190    }
191}
192
193/// Implementation of `AsRef` trait for `Context`.
194impl AsRef<Context> for Context {
195    /// Converts `&Context` to `&Context` via memory address conversion.
196    ///
197    /// # Returns
198    ///
199    /// - `&Self` - A reference to the `Context` instance.
200    #[inline(always)]
201    fn as_ref(&self) -> &Self {
202        let address: usize = self.into();
203        address.into()
204    }
205}
206
207/// Implementation of `AsMut` trait for `Context`.
208impl AsMut<Context> for Context {
209    /// Converts `&mut Context` to `&mut Context` via memory address conversion.
210    ///
211    /// # Returns
212    ///
213    /// - `&mut Self` - A mutable reference to the `Context` instance.
214    #[inline(always)]
215    fn as_mut(&mut self) -> &mut Self {
216        let address: usize = self.into();
217        address.into()
218    }
219}
220
221/// Implementation of `Lifetime` trait for `Context`.
222impl Lifetime for Context {
223    /// Converts a reference to the context into a `'static` reference.
224    ///
225    /// # Returns
226    ///
227    /// - `&'static Self` - A reference to the context with a `'static` lifetime.
228    ///
229    /// # Safety
230    ///
231    /// - The address is guaranteed to be a valid `Self` instance
232    ///   that was previously converted from a reference and is managed by the runtime.
233    #[inline(always)]
234    unsafe fn leak(&self) -> &'static Self {
235        let address: usize = self.into();
236        address.into()
237    }
238
239    /// Converts a reference to the context into a `'static` mutable reference.
240    ///
241    /// # Returns
242    ///
243    /// - `&'static mut Self` - A mutable reference to the context with a `'static` lifetime.
244    ///
245    /// # Safety
246    ///
247    /// - The address is guaranteed to be a valid `Self` instance
248    ///   that was previously converted from a reference and is managed by the runtime.
249    #[inline(always)]
250    unsafe fn leak_mut(&self) -> &'static mut Self {
251        let address: usize = self.into();
252        address.into()
253    }
254}
255
256/// Implementation of methods for `Context` structure.
257impl Context {
258    /// Clears the attributes the application set for the previous request.
259    ///
260    /// The original request target lives in its own field, so every attribute
261    /// in the map is request-local and the map is emptied outright. The map
262    /// keeps its capacity, so a connection that sets the same attributes over
263    /// and over stops allocating for them.
264    ///
265    /// # Returns
266    ///
267    /// - `&mut Self` - The context ready for the next request.
268    #[inline(always)]
269    pub(crate) fn reset_request_attributes(&mut self) -> &mut Self {
270        let attrs: &mut ThreadSafeAttributeStore = self.get_mut_attributes();
271        if !attrs.is_empty() {
272            return_attr_entries(attrs.drain());
273        }
274        self
275    }
276
277    /// Clears all route parameters while retaining the map's allocated capacity.
278    ///
279    /// Used between keep-alive requests on the same connection to avoid
280    /// reallocating the parameter map for every request.
281    ///
282    /// # Returns
283    ///
284    /// - `&mut Self` - A mutable reference to self for chaining.
285    #[inline(always)]
286    pub(crate) fn clear_route_params(&mut self) -> &mut Self {
287        let params: &mut HashMapXxHash3_64<String, String> = self.get_mut_route_params();
288        if !params.is_empty() {
289            return_route_param_entries(params.drain());
290        }
291        self
292    }
293
294    /// Attempts to retrieve a specific route parameter by its name.
295    ///
296    /// # Arguments
297    ///
298    /// - `T` - The name of the route parameter to retrieve.
299    ///
300    /// # Returns
301    ///
302    /// - `Option<String>` - The value of the route parameter if it exists.
303    #[inline(always)]
304    pub fn try_get_route_param<T>(&self, name: T) -> Option<String>
305    where
306        T: AsRef<str>,
307    {
308        self.get_route_params().get(name.as_ref()).cloned()
309    }
310
311    /// Retrieves a specific route parameter by its name, panicking if not found.
312    ///
313    /// # Arguments
314    ///
315    /// - `T` - The name of the route parameter to retrieve.
316    ///
317    /// # Returns
318    ///
319    /// - `String` - The value of the route parameter if it exists.
320    ///
321    /// # Panics
322    ///
323    /// - If the route parameter is not found.
324    #[inline(always)]
325    pub fn get_route_param<T>(&self, name: T) -> String
326    where
327        T: AsRef<str>,
328    {
329        self.try_get_route_param(name).unwrap()
330    }
331
332    /// Returns the original wire request target before routing or query-map conversion.
333    ///
334    /// # Returns
335    ///
336    /// - `Option<String>` - The unchanged path and query, or none for a manually constructed context.
337    pub fn get_request_target(&self) -> Option<String> {
338        self.get_request_target_ref().cloned()
339    }
340
341    /// Returns the original wire request target without copying it.
342    ///
343    /// # Returns
344    ///
345    /// - `Option<&String>` - The unchanged path and query, or none for a manually constructed context.
346    pub(crate) fn get_request_target_ref(&self) -> Option<&String> {
347        self.original_target.as_ref()
348    }
349
350    /// Takes the original target string out of the context for pooling.
351    ///
352    /// # Returns
353    ///
354    /// - `Option<String>` - The previously stored target string, if any.
355    #[inline(always)]
356    pub(crate) fn take_original_target(&mut self) -> Option<String> {
357        self.get_mut_original_target().take()
358    }
359
360    /// Stores the original target for the current request before user middleware runs.
361    ///
362    /// # Arguments
363    ///
364    /// - `T` - The unmodified target supplied by the protocol transport.
365    ///
366    /// # Returns
367    ///
368    /// - `&mut Self` - The context retaining its request-local target snapshot.
369    pub(crate) fn set_request_target<T>(&mut self, target: T) -> &mut Self
370    where
371        T: AsRef<str> + Into<String>,
372    {
373        let stored: &mut Option<String> = &mut self.original_target;
374        match stored {
375            Some(existing) => {
376                existing.clear();
377                existing.push_str(target.as_ref());
378            }
379            None => {
380                *stored = Some(target.into());
381            }
382        }
383        self
384    }
385
386    /// Validates and formats a plain external attribute key into a caller-provided stack buffer in a single pass.
387    ///
388    /// # Arguments
389    ///
390    /// - `&str` - Candidate external attribute key.
391    /// - `&'a mut [MaybeUninit<u8>; 128]` - Uninitialized stack buffer receiving `External("<key>")`.
392    ///
393    /// # Returns
394    ///
395    /// - `Option<&'a str>` - Borrowed formatted attribute map key when all bytes are unescaped printable ASCII.
396    #[inline(always)]
397    fn try_format_plain_external_key<'a>(
398        key: &str,
399        buf: &'a mut [MaybeUninit<u8>; 128],
400    ) -> Option<&'a str> {
401        let key_bytes: &[u8] = key.as_bytes();
402        let key_len: usize = key_bytes.len();
403        if key_len > EXTERNAL_ATTR_STACK_LIMIT {
404            return None;
405        }
406        let prefix_len: usize = EXTERNAL_ATTR_PREFIX.len();
407        let suffix_len: usize = EXTERNAL_ATTR_SUFFIX.len();
408        let total: usize = prefix_len + key_len + suffix_len;
409        // SAFETY: `key_len <= EXTERNAL_ATTR_STACK_LIMIT` guarantees `total <= 128`. Every copied
410        // byte is verified to be printable ASCII (`0x20..0x7f`) without `"` or `\`, so `0..total`
411        // is fully initialized and valid UTF-8.
412        unsafe {
413            let dst: *mut u8 = buf.as_mut_ptr().cast::<u8>();
414            let key_dst: *mut u8 = dst.add(prefix_len);
415            let mut idx: usize = 0;
416            while idx < key_len {
417                let byte: u8 = *key_bytes.get_unchecked(idx);
418                if !(0x20..0x7f).contains(&byte)
419                    || byte == EXTERNAL_ATTR_QUOTE
420                    || byte == EXTERNAL_ATTR_BACKSLASH
421                {
422                    return None;
423                }
424                *key_dst.add(idx) = byte;
425                idx += 1;
426            }
427            ptr::copy_nonoverlapping(EXTERNAL_ATTR_PREFIX.as_ptr(), dst, prefix_len);
428            ptr::copy_nonoverlapping(
429                EXTERNAL_ATTR_SUFFIX.as_ptr(),
430                key_dst.add(key_len),
431                suffix_len,
432            );
433            Some(from_utf8_unchecked(slice::from_raw_parts(dst, total)))
434        }
435    }
436
437    /// Returns the static map key for an internal framework attribute.
438    ///
439    /// # Arguments
440    ///
441    /// - `InternalAttribute` - The internal attribute variant.
442    ///
443    /// # Returns
444    ///
445    /// - `&'static str` - Formatted map key matching `Attribute::Internal(key).to_string()`.
446    #[inline(always)]
447    fn internal_attribute_key(key: InternalAttribute) -> &'static str {
448        match key {
449            InternalAttribute::TaskPanicData => INTERNAL_TASK_PANIC_KEY,
450            InternalAttribute::RequestErrorData => INTERNAL_REQUEST_ERROR_KEY,
451        }
452    }
453
454    /// Attempts to borrow a specific attribute by its key without cloning the stored value.
455    ///
456    /// # Arguments
457    ///
458    /// - `K` - The key of the attribute to retrieve.
459    ///
460    /// # Returns
461    ///
462    /// - `Option<&V>` - Borrowed reference to the attribute value if present and of type `V`.
463    #[inline(always)]
464    pub fn try_get_attribute_ref<V, K>(&self, key: K) -> Option<&V>
465    where
466        V: AnySendSyncClone,
467        K: AsRef<str>,
468    {
469        if self.get_attributes().is_empty() {
470            return None;
471        }
472        let key: &str = key.as_ref();
473        let mut stack_buf: [MaybeUninit<u8>; 128] = [const { MaybeUninit::uninit() }; 128];
474        if let Some(formatted) = Self::try_format_plain_external_key(key, &mut stack_buf) {
475            return self
476                .get_attributes()
477                .get(formatted)
478                .and_then(|arc: &ArcAnySendSync| arc.downcast_ref::<V>());
479        }
480        self.get_attributes()
481            .get(&Attribute::External(key.to_owned()).to_string())
482            .and_then(|arc: &ArcAnySendSync| arc.downcast_ref::<V>())
483    }
484
485    /// Attempts to retrieve a specific attribute by its key, casting it to the specified type.
486    ///
487    /// The original request target is readable under its own key, because the
488    /// framework stores it in its own field rather than in the attribute map.
489    ///
490    /// # Arguments
491    ///
492    /// - `K` - The key of the attribute to retrieve.
493    ///
494    /// # Returns
495    ///
496    /// - `Option<V>` - The attribute value if it exists and can be cast to the specified type.
497    #[inline(always)]
498    pub fn try_get_attribute<V, K>(&self, key: K) -> Option<V>
499    where
500        V: AnySendSyncClone,
501        K: AsRef<str>,
502    {
503        let key: &str = key.as_ref();
504        if key == REQUEST_TARGET_ATTRIBUTE {
505            let target: Option<&String> = self.get_request_target_ref();
506            return match target {
507                Some(target) => {
508                    // The target is held as a plain string, so it is boxed
509                    // here to take the same downcast the map takes. This path
510                    // is not on the request path: the framework stores and
511                    // reads the target through its own field.
512                    let value: ArcAnySendSync = Arc::new(target.clone());
513                    value.downcast_ref::<V>().cloned()
514                }
515                None => None,
516            };
517        }
518        if self.get_attributes().is_empty() {
519            return None;
520        }
521        let mut stack_buf: [MaybeUninit<u8>; 128] = [const { MaybeUninit::uninit() }; 128];
522        if let Some(formatted) = Self::try_format_plain_external_key(key, &mut stack_buf) {
523            return self
524                .get_attributes()
525                .get(formatted)
526                .and_then(|arc: &ArcAnySendSync| arc.downcast_ref::<V>())
527                .cloned();
528        }
529        self.get_attributes()
530            .get(&Attribute::External(key.to_owned()).to_string())
531            .and_then(|arc: &ArcAnySendSync| arc.downcast_ref::<V>())
532            .cloned()
533    }
534
535    /// Retrieves a specific attribute by its key, casting it to the specified type, panicking if not found.
536    ///
537    /// # Arguments
538    ///
539    /// - `K` - The key of the attribute to retrieve.
540    ///
541    /// # Returns
542    ///
543    /// - `V` - The attribute value if it exists and can be cast to the specified type.
544    ///
545    /// # Panics
546    ///
547    /// - If the attribute is not found.
548    #[inline(always)]
549    pub fn get_attribute<V, K>(&self, key: K) -> V
550    where
551        V: AnySendSyncClone,
552        K: AsRef<str>,
553    {
554        self.try_get_attribute(key).unwrap()
555    }
556
557    /// Sets an attribute in the context.
558    ///
559    /// # Arguments
560    ///
561    /// - `K` - The key of the attribute to set.
562    /// - `V` - The value of the attribute.
563    ///
564    /// # Returns
565    ///
566    /// - `&mut Self` - A reference to the modified context.
567    #[inline(always)]
568    pub fn set_attribute<K, V>(&mut self, key: K, value: V) -> &mut Self
569    where
570        K: AsRef<str>,
571        V: AnySendSyncClone,
572    {
573        let key_str: &str = key.as_ref();
574        let boxed: ArcAnySendSync = Arc::new(value);
575        let mut stack_buf: [MaybeUninit<u8>; 128] = [const { MaybeUninit::uninit() }; 128];
576        if let Some(formatted) = Self::try_format_plain_external_key(key_str, &mut stack_buf) {
577            let attrs: &mut ThreadSafeAttributeStore = self.get_mut_attributes();
578            if let Some(existing) = attrs.get_mut(formatted) {
579                *existing = boxed;
580            } else {
581                let mut key_buf: String = take_attr_key_buffer();
582                key_buf.push_str(formatted);
583                attrs.insert(key_buf, boxed);
584            }
585            return self;
586        }
587        let map_key: String = Attribute::External(key_str.to_owned()).to_string();
588        self.get_mut_attributes().insert(map_key, boxed);
589        self
590    }
591
592    /// Removes an attribute from the context.
593    ///
594    /// # Arguments
595    ///
596    /// - `K` - The key of the attribute to remove.
597    ///
598    /// # Returns
599    ///
600    /// - `&mut Self` - A reference to the modified context.
601    #[inline(always)]
602    pub fn remove_attribute<K>(&mut self, key: K) -> &mut Self
603    where
604        K: AsRef<str>,
605    {
606        let key_str: &str = key.as_ref();
607        let mut stack_buf: [MaybeUninit<u8>; 128] = [const { MaybeUninit::uninit() }; 128];
608        if let Some(formatted) = Self::try_format_plain_external_key(key_str, &mut stack_buf) {
609            self.get_mut_attributes().remove(formatted);
610            return self;
611        }
612        self.get_mut_attributes()
613            .remove(&Attribute::External(key_str.to_owned()).to_string());
614        self
615    }
616
617    /// Clears all attributes from the context.
618    ///
619    /// # Returns
620    ///
621    /// - `&mut Self` - A reference to the modified context.
622    #[inline(always)]
623    pub fn clear_attribute(&mut self) -> &mut Self {
624        self.get_mut_attributes().clear();
625        self
626    }
627
628    /// Retrieves an internal framework attribute.
629    ///
630    /// # Arguments
631    ///
632    /// - `InternalAttribute` - The internal attribute key to retrieve.
633    ///
634    /// # Returns
635    ///
636    /// - `Option<V>` - The attribute value if it exists and can be cast to the specified type.
637    #[inline(always)]
638    fn try_get_internal_attribute<V>(&self, key: InternalAttribute) -> Option<V>
639    where
640        V: AnySendSyncClone,
641    {
642        self.get_attributes()
643            .get(Self::internal_attribute_key(key))
644            .and_then(|arc: &ArcAnySendSync| arc.downcast_ref::<V>())
645            .cloned()
646    }
647
648    /// Retrieves an internal framework attribute.
649    ///
650    /// # Arguments
651    ///
652    /// - `InternalAttribute` - The internal attribute key to retrieve.
653    ///
654    /// # Returns
655    ///
656    /// - `V` - The attribute value if it exists and can be cast to the specified type.
657    ///
658    /// # Panics
659    ///
660    /// - If the attribute is not found.
661    #[inline(always)]
662    fn get_internal_attribute<V>(&self, key: InternalAttribute) -> V
663    where
664        V: AnySendSyncClone,
665    {
666        self.try_get_internal_attribute(key).unwrap()
667    }
668
669    /// Sets an internal framework attribute.
670    ///
671    /// # Arguments
672    ///
673    /// - `InternalAttribute` - The internal attribute key to set.
674    /// - `V` - The value of the attribute.
675    ///
676    /// # Returns
677    ///
678    /// - `&mut Self` - A reference to the modified context.
679    #[inline(always)]
680    fn set_internal_attribute<V>(&mut self, key: InternalAttribute, value: V) -> &mut Self
681    where
682        V: AnySendSyncClone,
683    {
684        self.get_mut_attributes().insert(
685            Self::internal_attribute_key(key).to_owned(),
686            Arc::new(value),
687        );
688        self
689    }
690
691    /// Stores panic data for the current task context.
692    ///
693    /// # Arguments
694    ///
695    /// - `PanicData` - The panic data specific to the current task.
696    ///
697    /// # Returns
698    ///
699    /// - `&mut Self` - Reference to the modified context for method chaining.
700    #[inline(always)]
701    pub fn set_task_panic(&mut self, panic_data: PanicData) -> &mut Self {
702        self.set_internal_attribute(InternalAttribute::TaskPanicData, panic_data)
703    }
704
705    /// Retrieves panic data associated with the current task.
706    ///
707    /// # Returns
708    ///
709    /// - `Option<PanicData>` - Task panic data if a panic was caught during execution.
710    #[inline(always)]
711    pub fn try_get_task_panic_data(&self) -> Option<PanicData> {
712        self.try_get_internal_attribute(InternalAttribute::TaskPanicData)
713    }
714
715    /// Retrieves panic data associated with the current task.
716    ///
717    /// # Returns
718    ///
719    /// - `PanicData` - Task panic data if available.
720    ///
721    /// # Panics
722    ///
723    /// - If no task panic data is found.
724    #[inline(always)]
725    pub fn get_task_panic_data(&self) -> PanicData {
726        self.get_internal_attribute(InternalAttribute::TaskPanicData)
727    }
728
729    /// Sets the request error information for the context.
730    ///
731    /// # Arguments
732    ///
733    /// - `RequestError` - The request error information to store.
734    ///
735    /// # Returns
736    ///
737    /// - `&mut Self` - A reference to the modified context.
738    #[inline(always)]
739    pub(crate) fn set_request_error_data(&mut self, request_error: RequestError) -> &mut Self {
740        self.set_internal_attribute(InternalAttribute::RequestErrorData, request_error)
741    }
742
743    /// Retrieves request error information if an error occurred during handling.
744    ///
745    /// # Returns
746    ///
747    /// - `Option<RequestError>` - The request error information if an error was caught.
748    #[inline(always)]
749    pub fn try_get_request_error_data(&self) -> Option<RequestError> {
750        self.try_get_internal_attribute(InternalAttribute::RequestErrorData)
751    }
752
753    /// Retrieves request error information if an error occurred during handling.
754    ///
755    /// # Returns
756    ///
757    /// - `RequestError` - The request error information if an error was caught.
758    ///
759    /// # Panics
760    ///
761    /// - If the request error information is not found.
762    #[inline(always)]
763    pub fn get_request_error_data(&self) -> RequestError {
764        self.get_internal_attribute(InternalAttribute::RequestErrorData)
765    }
766}