A better Rust ATProto crate
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

trait for deser workarounds, v1

+67 -1
+66
crates/jacquard-common/src/bos.rs
··· 189 189 /// for strings of 22 bytes or fewer. 190 190 pub type DefaultStr = SmolStr; 191 191 192 + /// Construct `S` from either a borrowed `&'de str` or an owned `SmolStr`. 193 + /// 194 + /// Validated string types (Handle, Did, Nsid, etc.) may need to normalise input during 195 + /// deserialization (e.g. lowercasing handles). The normalisation always produces a `SmolStr`, 196 + /// but when the input is already valid the deserializer wants to borrow directly from the 197 + /// input buffer. This trait provides the two construction paths so that a single custom 198 + /// deserialize visitor can produce any backing type. 199 + pub trait StrConsumer<'de, 'r>: Bos<str> + Sized { 200 + /// Construct from a borrowed string slice (zero-copy path). 201 + fn from_borrowed_str(s: &'de str) -> Self; 202 + 203 + /// Construct from an owned `SmolStr` (normalisation/allocation path). 204 + fn from_smolstr(s: &'r SmolStr) -> Self; 205 + } 206 + 207 + impl<'de, 'r> StrConsumer<'de, 'r> for SmolStr { 208 + #[inline] 209 + fn from_borrowed_str(s: &'de str) -> Self { 210 + SmolStr::new(s) 211 + } 212 + 213 + #[inline] 214 + fn from_smolstr(s: &'r SmolStr) -> Self { 215 + s.clone() 216 + } 217 + } 218 + 219 + impl<'de, 'r> StrConsumer<'de, 'r> for String { 220 + #[inline] 221 + fn from_borrowed_str(s: &'de str) -> Self { 222 + s.into() 223 + } 224 + 225 + #[inline] 226 + fn from_smolstr(s: &'r SmolStr) -> Self { 227 + s.clone().into() 228 + } 229 + } 230 + 231 + impl<'de, 'r> StrConsumer<'de, 'r> for &'de str 232 + where 233 + 'r: 'de, 234 + { 235 + #[inline] 236 + fn from_borrowed_str(s: &'de str) -> Self { 237 + s 238 + } 239 + 240 + #[inline] 241 + fn from_smolstr(s: &'r SmolStr) -> Self { 242 + s.as_str() 243 + } 244 + } 245 + 246 + impl<'de, 'r> StrConsumer<'de, 'r> for CowStr<'de> { 247 + #[inline] 248 + fn from_borrowed_str(s: &'de str) -> Self { 249 + CowStr::Borrowed(s) 250 + } 251 + 252 + #[inline] 253 + fn from_smolstr(s: &'r SmolStr) -> Self { 254 + CowStr::Owned(s.clone()) 255 + } 256 + } 257 + 192 258 #[cfg(test)] 193 259 mod tests { 194 260 use super::*;
+1 -1
crates/jacquard-common/src/lib.rs
··· 214 214 215 215 pub use cowstr::CowStr; 216 216 pub use into_static::IntoStatic; 217 - pub use bos::{Bos, BorrowOrShare, DefaultStr}; 217 + pub use bos::{Bos, BorrowOrShare, DefaultStr, StrConsumer}; 218 218 219 219 /// A copy-on-write immutable string type that uses [`smol_str::SmolStr`] for 220 220 /// the "owned" variant.