datafusion_sql/
stack.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18pub use inner::StackGuard;
19
20/// A guard that sets the minimum stack size for the current thread to `min_stack_size` bytes.
21#[cfg(feature = "recursive_protection")]
22mod inner {
23    /// Sets the stack size to `min_stack_size` bytes on call to `new()` and
24    /// resets to the previous value when this structure is dropped.
25    pub struct StackGuard {
26        previous_stack_size: usize,
27    }
28
29    impl StackGuard {
30        /// Sets the stack size to `min_stack_size` bytes on call to `new()` and
31        /// resets to the previous value when this structure is dropped.
32        pub fn new(min_stack_size: usize) -> Self {
33            let previous_stack_size = recursive::get_minimum_stack_size();
34            recursive::set_minimum_stack_size(min_stack_size);
35            Self {
36                previous_stack_size,
37            }
38        }
39    }
40
41    impl Drop for StackGuard {
42        fn drop(&mut self) {
43            recursive::set_minimum_stack_size(self.previous_stack_size);
44        }
45    }
46}
47
48/// A stub implementation of the stack guard when the recursive protection
49/// feature is not enabled
50#[cfg(not(feature = "recursive_protection"))]
51mod inner {
52    /// A stub implementation of the stack guard when the recursive protection
53    /// feature is not enabled that does nothing
54    pub struct StackGuard;
55
56    impl StackGuard {
57        /// A stub implementation of the stack guard when the recursive protection
58        /// feature is not enabled
59        pub fn new(_min_stack_size: usize) -> Self {
60            Self
61        }
62    }
63}