#Getting `&str` from `String` in const context

18 messages · Page 1 of 1 (latest)

copper glacier
#

is it possible

cedar stone
#

uh how did you get a non-empty String in a const context?

copper glacier
#

/// A single stackframe
#[derive(Debug)]
pub struct Stackframe {
    source_location: SourceLocation,
    name: Option<String>
}

impl Stackframe {
    /// Creates a new [`Stackframe`].
    pub const fn new(source_location: SourceLocation, name: Option<String>) -> Self {
        Self { source_location, name }
    }

    /// Fetches the source location of the stackframe.
    pub const fn source_location(&self) -> &SourceLocation {
        &self.source_location
    }

    /// Gets the name of the stackframe (which may not exist, for lambda functions).
    pub const fn name(&self) -> Option<&str> {
        // ???
    }
}```
cedar stone
#

oh

#

hmm

copper glacier
#

.as_str() and .as_ref() dont work and &*x doesnt work either

#

(not to mention .as_deref(), which would be nicer)

cedar stone
#

are you willing to use ultra unsafe features?

copper glacier
#

unstable or unsafe?

cedar stone
#

well actually its both

copper glacier
#

lol

#

i could just return Option<&String> and be done with it lol

cedar stone
#

oh yeah

#

wait

#

what if you just dont make this function const

copper glacier
#

that's also an option

cedar stone
#

its not useful in a const context anyway