Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/fast/stacksafe"
rust-version = "1.80.0"
version = "0.1.2"
version = "0.1.3"

[workspace.dependencies]
# workspace dependencies
stacksafe = { version = "0.1.2", path = "stacksafe" }
stacksafe-macro = { version = "=0.1.2", path = "stacksafe-macro" }
stacksafe = { version = "0.1.3", path = "stacksafe" }
stacksafe-macro = { version = "=0.1.3", path = "stacksafe-macro" }

# crates.io dependencies
derive-visitor = "0.4"
Expand Down
32 changes: 32 additions & 0 deletions stacksafe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,38 @@ impl<T> StackSafe<T> {
pub fn new(value: T) -> Self {
StackSafe(std::mem::ManuallyDrop::new(value))
}

/// Consumes the [`StackSafe<T>`] wrapper and returns the inner value.
///
/// # Panics
///
/// In debug builds, panics if called outside of a stack-safe context.
/// This helps ensure that recursive data structure access is properly
/// protected against stack overflow.
///
/// # Examples
///
/// ```rust
/// # #[stacksafe::stacksafe]
/// # fn main() {
/// use stacksafe::StackSafe;
///
/// let wrapped = StackSafe::new(vec![1, 2, 3]);
/// let inner = wrapped.into_inner();
/// assert_eq!(inner, vec![1, 2, 3]);
/// # }
/// ```
pub fn into_inner(mut self) -> T {
debug_assert!(
crate::internal::is_protected(),
"`StackSafe` should only be accessed within a stack-safe context\n\
help: add `#[stacksafe::stacksafe]` to the function containing this access"
);

let value = unsafe { std::mem::ManuallyDrop::take(&mut self.0) };
std::mem::forget(self);
value
}
}

impl<T> From<T> for StackSafe<T> {
Expand Down
Loading