soroban_sdk/symbol.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
use core::{cmp::Ordering, convert::Infallible, fmt::Debug};
use super::{
env::internal::{Env as _, EnvBase as _, Symbol as SymbolVal, SymbolSmall},
ConversionError, Env, TryFromVal, TryIntoVal, Val,
};
#[cfg(not(target_family = "wasm"))]
use super::env::SymbolStr;
#[cfg(not(target_family = "wasm"))]
use crate::env::internal::xdr::{ScSymbol, ScVal};
use crate::{
env::MaybeEnv,
unwrap::{UnwrapInfallible, UnwrapOptimized},
};
/// Symbol is a short string with a limited character set.
///
/// Valid characters are `a-zA-Z0-9_` and maximum length is 32 characters.
///
/// Symbols are used for the for symbolic identifiers, such as function
/// names and user-defined structure field/enum variant names. That's why
/// these idenfiers have limited length.
///
/// While Symbols up to 32 characters long are allowed, Symbols that are 9
/// characters long or shorter are more efficient at runtime and also can be
/// computed at compile time.
#[derive(Clone)]
pub struct Symbol {
env: MaybeEnv,
val: SymbolVal,
}
impl Debug for Symbol {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
#[cfg(target_family = "wasm")]
write!(f, "Symbol(..)")?;
#[cfg(not(target_family = "wasm"))]
write!(f, "Symbol({})", self.to_string())?;
Ok(())
}
}
impl Eq for Symbol {}
impl PartialEq for Symbol {
fn eq(&self, other: &Self) -> bool {
self.partial_cmp(other) == Some(Ordering::Equal)
}
}
impl PartialOrd for Symbol {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(Ord::cmp(self, other))
}
}
impl Ord for Symbol {
fn cmp(&self, other: &Self) -> Ordering {
let self_raw = self.val.to_val();
let other_raw = other.val.to_val();
match (
SymbolSmall::try_from(self_raw),
SymbolSmall::try_from(other_raw),
) {
// Compare small symbols.
(Ok(self_sym), Ok(other_sym)) => self_sym.cmp(&other_sym),
// The object-to-small symbol comparisons are handled by `obj_cmp`,
// so it's safe to handle all the other cases using it.
_ => {
let env: Option<Env> =
match (self.env.clone().try_into(), other.env.clone().try_into()) {
(Err(_), Err(_)) => None,
(Err(_), Ok(e)) => Some(e),
(Ok(e), Err(_)) => Some(e),
(Ok(e1), Ok(e2)) => {
e1.check_same_env(&e2).unwrap_infallible();
Some(e1)
}
};
if let Some(env) = env {
let v = env.obj_cmp(self_raw, other_raw).unwrap_infallible();
v.cmp(&0)
} else {
panic!("symbol object is missing the env reference");
}
}
}
}
}
impl TryFromVal<Env, SymbolVal> for Symbol {
type Error = Infallible;
fn try_from_val(env: &Env, val: &SymbolVal) -> Result<Self, Self::Error> {
Ok(unsafe { Symbol::unchecked_new(env.clone(), *val) })
}
}
impl TryFromVal<Env, Val> for Symbol {
type Error = ConversionError;
fn try_from_val(env: &Env, val: &Val) -> Result<Self, Self::Error> {
Ok(SymbolVal::try_from_val(env, val)?
.try_into_val(env)
.unwrap_infallible())
}
}
impl TryFromVal<Env, Symbol> for Val {
type Error = ConversionError;
fn try_from_val(_env: &Env, v: &Symbol) -> Result<Self, Self::Error> {
Ok(v.to_val())
}
}
impl TryFromVal<Env, &Symbol> for Val {
type Error = ConversionError;
fn try_from_val(_env: &Env, v: &&Symbol) -> Result<Self, Self::Error> {
Ok(v.to_val())
}
}
impl TryFromVal<Env, &str> for Symbol {
type Error = ConversionError;
fn try_from_val(env: &Env, val: &&str) -> Result<Self, Self::Error> {
Ok(SymbolVal::try_from_val(env, val)?
.try_into_val(env)
.unwrap_infallible())
}
}
#[cfg(not(target_family = "wasm"))]
impl From<&Symbol> for ScVal {
fn from(v: &Symbol) -> Self {
// This conversion occurs only in test utilities, and theoretically all
// values should convert to an ScVal because the Env won't let the host
// type to exist otherwise, unwrapping. Even if there are edge cases
// that don't, this is a trade off for a better test developer
// experience.
if let Ok(ss) = SymbolSmall::try_from(v.val) {
ScVal::try_from(ss).unwrap()
} else {
let e: Env = v.env.clone().try_into().unwrap();
ScVal::try_from_val(&e, &v.to_val()).unwrap()
}
}
}
#[cfg(not(target_family = "wasm"))]
impl From<Symbol> for ScVal {
fn from(v: Symbol) -> Self {
(&v).into()
}
}
#[cfg(not(target_family = "wasm"))]
impl TryFromVal<Env, Symbol> for ScVal {
type Error = ConversionError;
fn try_from_val(_e: &Env, v: &Symbol) -> Result<Self, ConversionError> {
Ok(v.into())
}
}
#[cfg(not(target_family = "wasm"))]
impl TryFromVal<Env, ScVal> for Symbol {
type Error = ConversionError;
fn try_from_val(env: &Env, val: &ScVal) -> Result<Self, Self::Error> {
Ok(SymbolVal::try_from_val(env, &Val::try_from_val(env, val)?)?
.try_into_val(env)
.unwrap_infallible())
}
}
#[cfg(not(target_family = "wasm"))]
impl TryFromVal<Env, ScSymbol> for Symbol {
type Error = ConversionError;
fn try_from_val(env: &Env, val: &ScSymbol) -> Result<Self, Self::Error> {
Ok(SymbolVal::try_from_val(env, val)?
.try_into_val(env)
.unwrap_infallible())
}
}
impl Symbol {
/// Creates a new Symbol given a string with valid characters.
///
/// Valid characters are `a-zA-Z0-9_` and maximum string length is 32
/// characters.
///
/// Use `symbol_short!` for constant symbols that are 9 characters or less.
///
/// Use `Symbol::try_from_val(env, s)`/`s.try_into_val(env)` in case if
/// failures need to be handled gracefully.
///
/// ### Panics
///
/// When the input string is not representable by Symbol.
pub fn new(env: &Env, s: &str) -> Self {
Self {
env: env.clone().into(),
val: s.try_into_val(env).unwrap_optimized(),
}
}
/// Creates a new Symbol given a short string with valid characters.
///
/// Valid characters are `a-zA-Z0-9_` and maximum length is 9 characters.
///
/// The conversion can happen at compile time if called in a const context,
/// such as:
///
/// ```rust
/// # use soroban_sdk::Symbol;
/// const SYMBOL: Symbol = Symbol::short("abcde");
/// ```
///
/// Note that when called from a non-const context the conversion will occur
/// at runtime and the conversion logic will add considerable number of
/// bytes to built wasm file. For this reason the function should be generally
/// avoided:
///
/// ```rust
/// # use soroban_sdk::Symbol;
/// let SYMBOL: Symbol = Symbol::short("abcde"); // AVOID!
/// ```
///
/// Instead use the `symbol_short!()` macro that will ensure the conversion always occurs in a const-context:
///
/// ```rust
/// # use soroban_sdk::{symbol_short, Symbol};
/// let SYMBOL: Symbol = symbol_short!("abcde"); // 👍
/// ```
///
/// ### Panics
///
/// When the input string is not representable by Symbol.
#[doc(hidden)]
#[deprecated(note = "use [symbol_short!()]")]
pub const fn short(s: &str) -> Self {
if let Ok(sym) = SymbolSmall::try_from_str(s) {
Symbol {
env: MaybeEnv::none(),
val: SymbolVal::from_small(sym),
}
} else {
panic!("short symbols are limited to 9 characters");
}
}
#[inline(always)]
pub(crate) unsafe fn unchecked_new(env: Env, val: SymbolVal) -> Self {
Self {
env: env.into(),
val,
}
}
pub fn as_val(&self) -> &Val {
self.val.as_val()
}
pub fn to_val(&self) -> Val {
self.val.to_val()
}
pub fn to_symbol_val(&self) -> SymbolVal {
self.val
}
}
#[cfg(not(target_family = "wasm"))]
extern crate std;
#[cfg(not(target_family = "wasm"))]
impl ToString for Symbol {
fn to_string(&self) -> String {
if let Ok(s) = SymbolSmall::try_from(self.val) {
s.to_string()
} else {
let e: Env = self.env.clone().try_into().unwrap_optimized();
SymbolStr::try_from_val(&e, &self.val)
.unwrap_optimized()
.to_string()
}
}
}