CloneableString

Type Alias CloneableString 

Source
pub type CloneableString = Dynamic<CloneableStringInner>;
Expand description

A string wrapped as a cloneable secret.

This type provides a secure wrapper around a String that can be safely cloned while ensuring the underlying data is properly zeroized when no longer needed. Use this for sensitive text data like passwords, tokens, or cryptographic passphrases.

§Examples

use secure_gate::{CloneableString, ExposeSecret};

// Create from a string
let password: CloneableString = "secret123".to_string().into();

// Create from a string slice
let token: CloneableString = "token_value".into();

// Access the inner string
assert_eq!(password.expose_secret().0.as_str(), "secret123");

Aliased Type§

pub struct CloneableString(/* private fields */);

Implementations§

Source§

impl CloneableString

Source

pub fn init_with<F>(constructor: F) -> Self
where F: FnOnce() -> String,

Construct a cloneable string secret by building it in a closure.

This minimizes the time the secret spends on the stack:

  • The closure builds a temporary String.
  • It is immediately cloned to the heap.
  • The temporary is zeroized before returning.

Use this when reading passwords or tokens from user input.

§Example
use secure_gate::CloneableString;
use std::io::{self, Write};

fn read_password() -> io::Result<String> {
    let mut input = String::new();
    io::stdout().flush()?;
    io::stdin().read_line(&mut input)?;
    Ok(input.trim_end().to_string())
}

let pw = CloneableString::init_with(|| read_password().unwrap());
Source

pub fn try_init_with<F, E>(constructor: F) -> Result<Self, E>
where F: FnOnce() -> Result<String, E>,

Fallible version of init_with.

Same stack-minimization benefits as init_with, but allows for construction that may fail with an error. Useful when reading secrets from fallible sources like files, network connections, or user input that may encounter I/O errors.

Trait Implementations§

Source§

impl From<&str> for CloneableString

Wrap a string slice in a CloneableString.

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for CloneableString

Wrap a String in a CloneableString.

Source§

fn from(value: String) -> Self

Converts to this type from the input type.