Pretty print `RValue` in a Rust-like syntax. Using `{:#}` would expand to multiple lines. Similar to `std::fmt::Debug`, but the goal is to make the format string almost-compile.
(&self, f: &mut std::fmt::Formatter<'_>)
| 283 | /// |
| 284 | /// Similar to `std::fmt::Debug`, but the goal is to make the format string almost-compile. |
| 285 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 286 | let pretty = f.alternate(); |
| 287 | let width = f.width().unwrap_or_default(); |
| 288 | match self { |
| 289 | Self::Unit => write!(f, "()")?, |
| 290 | Self::Prim(v) => { |
| 291 | if pretty { |
| 292 | write!(f, "{:#}", v)? |
| 293 | } else { |
| 294 | write!(f, "{}", v)? |
| 295 | } |
| 296 | } |
| 297 | Self::Bytes { typename, value } => { |
| 298 | if typename.starts_with('&') { |
| 299 | write!(f, "&")?; |
| 300 | } |
| 301 | print_bytes(f, &value)?; |
| 302 | } |
| 303 | Self::Ref { |
| 304 | typename, value, .. |
| 305 | } => { |
| 306 | let b = match *typename { |
| 307 | RefType::Ref => { |
| 308 | write!(f, "&")?; |
| 309 | "" |
| 310 | } |
| 311 | RefType::Ptr => { |
| 312 | write!(f, "*")?; |
| 313 | "" |
| 314 | } |
| 315 | RefType::Box => { |
| 316 | write!(f, "alloc::boxed::Box::new(")?; |
| 317 | ")" |
| 318 | } |
| 319 | }; |
| 320 | if pretty { |
| 321 | write!(f, "{value:#width$}", width = width)?; |
| 322 | } else { |
| 323 | write!(f, "{value}")?; |
| 324 | } |
| 325 | write!(f, "{}", b)?; |
| 326 | } |
| 327 | Self::DynRef { |
| 328 | typename, value, .. |
| 329 | } => { |
| 330 | if typename.starts_with("&dyn ") { |
| 331 | write!(f, "{} ", typename)?; |
| 332 | } else { |
| 333 | write!( |
| 334 | f, |
| 335 | "{}::new(", |
| 336 | typename.replacen("alloc::boxed::Box<", "alloc::boxed::Box::<", 1) |
| 337 | )?; |
| 338 | } |
| 339 | if pretty { |
| 340 | write!(f, "{value:#width$}", width = width)?; |
| 341 | } else { |
| 342 | write!(f, "{value}")?; |
nothing calls this directly
no test coverage detected