Skip to content

Commit

Permalink
all: Rename STRING_DEBUG and STRING_DISPLAY to DEBUG_FMT and DISPLAY_FMT
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 1, 2024
1 parent d3b1241 commit f49d1da
Show file tree
Hide file tree
Showing 38 changed files with 177 additions and 182 deletions.
14 changes: 7 additions & 7 deletions book/src/template_literals.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ protocol* and it can be very efficient to build complex strings out of it.

[a concept borrowed from EcmaScript]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

## The `STRING_DISPLAY` protocol
## The `DISPLAY_FMT` protocol

The `STRING_DISPLAY` protocol is a function that can be implemented by any
The `DISPLAY_FMT` protocol is a function that can be implemented by any
*external* type which allows it to be used in a template string.

It expects a function with the signature `fn(&self, buf: &mut String) -> fmt::Result`.
Expand All @@ -40,15 +40,15 @@ pub struct StatusCode {
}
impl StatusCode {
#[rune::function(protocol = STRING_DISPLAY)]
fn string_display(&self, f: &mut Formatter) -> fmt::Result {
#[rune::function(protocol = DISPLAY_FMT)]
fn display_fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.inner)
}
}
pub fn module() -> Result<Module, ContextError> {
let mut module = Module::new(["http"]);
module.function_meta(StatusCode::string_display)?;
module.function_meta(StatusCode::display_fmt)?;
Ok(module)
}
```
Expand All @@ -62,10 +62,10 @@ types which do not implement this protocol will fail to run.

```text
$> cargo run -- run scripts/book/template_literals/not_a_template.rn
== ! (`Vec` does not implement the `string_display` protocol (at 5)) (77.7µs)
== ! (`Vec` does not implement the `display_fmt` protocol (at 5)) (77.7µs)
error: virtual machine error
┌─ scripts/book/template_literals/not_a_template.rn:3:9
3 │ dbg!(`${vec}`);
│ ^^^^^^^^ `Vec` does not implement the `string_display` protocol
│ ^^^^^^^^ `Vec` does not implement the `display_fmt` protocol
```
8 changes: 4 additions & 4 deletions crates/rune-core/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ define! {
};

/// Protocol function used by template strings.
pub const STRING_DISPLAY: Protocol = Protocol {
name: "STRING_DISPLAY",
pub const DISPLAY_FMT: Protocol = Protocol {
name: "DISPLAY_FMT",
hash: 0x811b62957ea9d9f9u64,
repr: Some("println(\"{}\", $value)"),
doc: docstring! {
Expand All @@ -489,8 +489,8 @@ define! {
};

/// Protocol function used by custom debug impls.
pub const STRING_DEBUG: Protocol = Protocol {
name: "STRING_DEBUG",
pub const DEBUG_FMT: Protocol = Protocol {
name: "DEBUG_FMT",
hash: 0x4064e3867aaa0717u64,
repr: Some("println(\"{:?}\", $value)"),
doc: docstring! {
Expand Down
4 changes: 2 additions & 2 deletions crates/rune-modules/src/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ impl From<base64::DecodeSliceError> for DecodeError {
}

impl DecodeError {
#[rune::function(instance, protocol = STRING_DISPLAY)]
fn string_display(&self, f: &mut Formatter) -> VmResult<()> {
#[rune::function(instance, protocol = DISPLAY_FMT)]
fn display_fmt(&self, f: &mut Formatter) -> VmResult<()> {
rune::vm_write!(f, "{}", self.inner)
}
}
24 changes: 12 additions & 12 deletions crates/rune-modules/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ pub fn module(_stdio: bool) -> Result<Module, ContextError> {
module.function_meta(StatusCode::cmp__meta)?;
module.implement_trait::<StatusCode>(item!(::std::cmp::Ord))?;
module.function_meta(StatusCode::hash__meta)?;
module.function_meta(StatusCode::string_debug__meta)?;
module.function_meta(StatusCode::string_display__meta)?;
module.function_meta(StatusCode::debug_fmt__meta)?;
module.function_meta(StatusCode::display_fmt__meta)?;

module
.constant(
Expand Down Expand Up @@ -1326,7 +1326,7 @@ pub fn module(_stdio: bool) -> Result<Module, ContextError> {
module.function_meta(Version::cmp__meta)?;
module.implement_trait::<Version>(item!(::std::cmp::Ord))?;
module.function_meta(Version::hash__meta)?;
module.function_meta(Version::string_debug__meta)?;
module.function_meta(Version::debug_fmt__meta)?;

module
.constant(
Expand Down Expand Up @@ -1429,7 +1429,7 @@ pub fn module(_stdio: bool) -> Result<Module, ContextError> {
})?;

module.ty::<Error>()?;
module.function_meta(Error::string_display__meta)?;
module.function_meta(Error::display_fmt__meta)?;
Ok(module)
}

Expand All @@ -1448,8 +1448,8 @@ impl From<reqwest::Error> for Error {

impl Error {
/// Write a display representation the error.
#[rune::function(keep, instance, protocol = STRING_DISPLAY)]
fn string_display(&self, f: &mut Formatter) -> VmResult<()> {
#[rune::function(keep, instance, protocol = DISPLAY_FMT)]
fn display_fmt(&self, f: &mut Formatter) -> VmResult<()> {
rune::vm_write!(f, "{}", self.inner)
}
}
Expand Down Expand Up @@ -1800,8 +1800,8 @@ impl StatusCode {
///
/// println!("{not_found:?}");
/// ```
#[rune::function(keep, instance, protocol = STRING_DEBUG)]
fn string_debug(&self, f: &mut Formatter) -> VmResult<()> {
#[rune::function(keep, instance, protocol = DEBUG_FMT)]
fn debug_fmt(&self, f: &mut Formatter) -> VmResult<()> {
rune::vm_write!(f, "{:?}", self.inner)
}

Expand All @@ -1816,8 +1816,8 @@ impl StatusCode {
///
/// println!("{not_found}");
/// ```
#[rune::function(keep, instance, protocol = STRING_DISPLAY)]
fn string_display(&self, f: &mut Formatter) -> VmResult<()> {
#[rune::function(keep, instance, protocol = DISPLAY_FMT)]
fn display_fmt(&self, f: &mut Formatter) -> VmResult<()> {
rune::vm_write!(f, "{}", self.inner)
}
}
Expand Down Expand Up @@ -1983,8 +1983,8 @@ impl Version {
///
/// println!("{:?}", http2);
/// ```
#[rune::function(keep, instance, protocol = STRING_DEBUG)]
fn string_debug(&self, f: &mut Formatter) -> VmResult<()> {
#[rune::function(keep, instance, protocol = DEBUG_FMT)]
fn debug_fmt(&self, f: &mut Formatter) -> VmResult<()> {
rune::vm_write!(f, "{:?}", self.inner)
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rune-modules/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ struct Error {
}

impl Error {
#[rune::function(protocol = STRING_DISPLAY)]
#[rune::function(protocol = DISPLAY_FMT)]
pub(crate) fn display(&self, f: &mut Formatter) -> VmResult<()> {
vm_write!(f, "{}", self.error)
}

#[rune::function(protocol = STRING_DEBUG)]
#[rune::function(protocol = DEBUG_FMT)]
pub(crate) fn debug(&self, f: &mut Formatter) -> VmResult<()> {
vm_write!(f, "{:?}", self.error)
}
Expand Down
46 changes: 23 additions & 23 deletions crates/rune-modules/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn module(_stdio: bool) -> Result<Module, ContextError> {
module.function_meta(Command::new__meta)?;
module.function_meta(Command::arg__meta)?;
module.function_meta(Command::args__meta)?;
module.function_meta(Command::string_debug__meta)?;
module.function_meta(Command::debug_fmt__meta)?;
#[cfg(unix)]
module.function_meta(Command::arg0__meta)?;
module.function_meta(Command::stdin__meta)?;
Expand All @@ -70,7 +70,7 @@ pub fn module(_stdio: bool) -> Result<Module, ContextError> {
module.function_meta(Command::spawn__meta)?;

module.ty::<Child>()?;
module.function_meta(Child::string_debug__meta)?;
module.function_meta(Child::debug_fmt__meta)?;
module.function_meta(Child::stdin__meta)?;
module.function_meta(Child::stdout__meta)?;
module.function_meta(Child::stderr__meta)?;
Expand All @@ -83,28 +83,28 @@ pub fn module(_stdio: bool) -> Result<Module, ContextError> {
module.ty::<ExitStatus>()?;
module.function_meta(ExitStatus::code__meta)?;
module.function_meta(ExitStatus::success__meta)?;
module.function_meta(ExitStatus::string_display__meta)?;
module.function_meta(ExitStatus::string_debug__meta)?;
module.function_meta(ExitStatus::display_fmt__meta)?;
module.function_meta(ExitStatus::debug_fmt__meta)?;

module.ty::<Output>()?;
module.function_meta(Output::string_debug__meta)?;
module.function_meta(Output::debug_fmt__meta)?;

module.ty::<Stdio>()?;
module.function_meta(Stdio::null__meta)?;
module.function_meta(Stdio::inherit__meta)?;
module.function_meta(Stdio::piped__meta)?;
module.function_meta(Stdio::string_debug__meta)?;
module.function_meta(Stdio::debug_fmt__meta)?;

module.ty::<ChildStdin>()?;
module.function_meta(ChildStdin::string_debug__meta)?;
module.function_meta(ChildStdin::debug_fmt__meta)?;
module.function_meta(ChildStdin::try_into_stdio__meta)?;

module.ty::<ChildStdout>()?;
module.function_meta(ChildStdout::string_debug__meta)?;
module.function_meta(ChildStdout::debug_fmt__meta)?;
module.function_meta(ChildStdout::try_into_stdio__meta)?;

module.ty::<ChildStderr>()?;
module.function_meta(ChildStderr::string_debug__meta)?;
module.function_meta(ChildStderr::debug_fmt__meta)?;
module.function_meta(ChildStderr::try_into_stdio__meta)?;

Ok(module)
Expand Down Expand Up @@ -424,8 +424,8 @@ impl Command {
})
}

#[rune::function(keep, protocol = STRING_DEBUG)]
fn string_debug(&self, f: &mut Formatter) -> VmResult<()> {
#[rune::function(keep, protocol = DEBUG_FMT)]
fn debug_fmt(&self, f: &mut Formatter) -> VmResult<()> {
vm_write!(f, "{self:?}")
}
}
Expand Down Expand Up @@ -616,8 +616,8 @@ impl Child {
})
}

#[rune::function(keep, protocol = STRING_DEBUG)]
fn string_debug(&self, f: &mut Formatter) -> VmResult<()> {
#[rune::function(keep, protocol = DEBUG_FMT)]
fn debug_fmt(&self, f: &mut Formatter) -> VmResult<()> {
vm_write!(f, "{:?}", self.inner)
}
}
Expand All @@ -644,8 +644,8 @@ struct Output {
}

impl Output {
#[rune::function(keep, protocol = STRING_DEBUG)]
fn string_debug(&self, f: &mut Formatter) -> VmResult<()> {
#[rune::function(keep, protocol = DEBUG_FMT)]
fn debug_fmt(&self, f: &mut Formatter) -> VmResult<()> {
vm_write!(f, "{self:?}")
}
}
Expand Down Expand Up @@ -713,13 +713,13 @@ impl ExitStatus {
self.inner.code()
}

#[rune::function(keep, protocol = STRING_DISPLAY)]
fn string_display(&self, f: &mut Formatter) -> VmResult<()> {
#[rune::function(keep, protocol = DISPLAY_FMT)]
fn display_fmt(&self, f: &mut Formatter) -> VmResult<()> {
vm_write!(f, "{}", self.inner)
}

#[rune::function(keep, protocol = STRING_DEBUG)]
fn string_debug(&self, f: &mut Formatter) -> VmResult<()> {
#[rune::function(keep, protocol = DEBUG_FMT)]
fn debug_fmt(&self, f: &mut Formatter) -> VmResult<()> {
vm_write!(f, "{:?}", self.inner)
}
}
Expand Down Expand Up @@ -756,8 +756,8 @@ impl Stdio {
}
}

#[rune::function(keep, protocol = STRING_DEBUG)]
fn string_debug(&self, f: &mut Formatter) -> VmResult<()> {
#[rune::function(keep, protocol = DEBUG_FMT)]
fn debug_fmt(&self, f: &mut Formatter) -> VmResult<()> {
vm_write!(f, "{:?}", self.inner)
}
}
Expand All @@ -784,8 +784,8 @@ macro_rules! stdio_stream {
})
}

#[rune::function(keep, protocol = STRING_DEBUG)]
fn string_debug(&self, f: &mut Formatter) -> VmResult<()> {
#[rune::function(keep, protocol = DEBUG_FMT)]
fn debug_fmt(&self, f: &mut Formatter) -> VmResult<()> {
vm_write!(f, "{:?}", self.inner)
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/rune-modules/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn module(_stdio: bool) -> Result<Module, ContextError> {
m.function_meta(Duration::cmp__meta)?;
m.implement_trait::<Duration>(item!(::std::cmp::Ord))?;
m.function_meta(Duration::hash__meta)?;
m.function_meta(Duration::string_debug__meta)?;
m.function_meta(Duration::debug_fmt__meta)?;
m.function_meta(Duration::clone__meta)?;
m.implement_trait::<Duration>(item!(::std::clone::Clone))?;

Expand Down Expand Up @@ -208,7 +208,7 @@ pub fn module(_stdio: bool) -> Result<Module, ContextError> {
m.function_meta(Instant::cmp__meta)?;
m.implement_trait::<Instant>(item!(::std::cmp::Ord))?;
m.function_meta(Instant::hash__meta)?;
m.function_meta(Instant::string_debug__meta)?;
m.function_meta(Instant::debug_fmt__meta)?;
m.function_meta(Instant::clone__meta)?;
m.implement_trait::<Instant>(item!(::std::clone::Clone))?;

Expand Down Expand Up @@ -841,8 +841,8 @@ impl Duration {
///
/// println!("{second:?}");
/// ```
#[rune::function(keep, instance, protocol = STRING_DEBUG)]
fn string_debug(&self, f: &mut Formatter) -> VmResult<()> {
#[rune::function(keep, instance, protocol = DEBUG_FMT)]
fn debug_fmt(&self, f: &mut Formatter) -> VmResult<()> {
rune::vm_write!(f, "{:?}", self.inner)
}

Expand Down Expand Up @@ -1290,8 +1290,8 @@ impl Instant {
///
/// println!("{now:?}");
/// ```
#[rune::function(keep, instance, protocol = STRING_DEBUG)]
fn string_debug(&self, f: &mut Formatter) -> VmResult<()> {
#[rune::function(keep, instance, protocol = DEBUG_FMT)]
fn debug_fmt(&self, f: &mut Formatter) -> VmResult<()> {
rune::vm_write!(f, "{:?}", self.inner)
}

Expand Down
8 changes: 4 additions & 4 deletions crates/rune-modules/src/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ pub mod de {
}

impl Error {
#[rune::function(protocol = STRING_DISPLAY)]
#[rune::function(protocol = DISPLAY_FMT)]
pub(crate) fn display(&self, f: &mut Formatter) -> VmResult<()> {
vm_write!(f, "{}", self.error)
}

#[rune::function(protocol = STRING_DEBUG)]
#[rune::function(protocol = DEBUG_FMT)]
pub(crate) fn debug(&self, f: &mut Formatter) -> VmResult<()> {
vm_write!(f, "{:?}", self.error)
}
Expand Down Expand Up @@ -105,12 +105,12 @@ pub mod ser {
}

impl Error {
#[rune::function(protocol = STRING_DISPLAY)]
#[rune::function(protocol = DISPLAY_FMT)]
pub(crate) fn display(&self, f: &mut Formatter) -> VmResult<()> {
vm_write!(f, "{}", self.error)
}

#[rune::function(protocol = STRING_DEBUG)]
#[rune::function(protocol = DEBUG_FMT)]
pub(crate) fn debug(&self, f: &mut Formatter) -> VmResult<()> {
vm_write!(f, "{:?}", self.error)
}
Expand Down
10 changes: 5 additions & 5 deletions crates/rune/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ pub(crate) use rune_macros::__internal_impl_any;
/// Type::name)]` argument. If `instance` is specified it is an associated
/// instance function that can be defined externally.
/// * Instance functions can be made a protocol function
/// `#[rune::function(protocol = STRING_DISPLAY)]`.
/// `#[rune::function(protocol = DISPLAY_FMT)]`.
///
/// # Instance and associated functions
///
Expand Down Expand Up @@ -408,8 +408,8 @@ pub(crate) use rune_macros::__internal_impl_any;
/// /* .. */
/// }
///
/// #[rune::function(instance, protocol = STRING_DISPLAY)]
/// fn string_display(this: &Struct, f: &mut Formatter) -> VmResult<()> {
/// #[rune::function(instance, protocol = DISPLAY_FMT)]
/// fn display_fmt(this: &Struct, f: &mut Formatter) -> VmResult<()> {
/// /* .. */
/// # todo!()
/// }
Expand Down Expand Up @@ -497,15 +497,15 @@ pub(crate) use rune_macros::__internal_impl_any;
/// }
/// }
///
/// /// Display the string using the [`STRING_DISPLAY`] protocol.
/// /// Display the string using the [`DISPLAY_FMT`] protocol.
/// ///
/// /// # Examples
/// ///
/// /// ```rune
/// /// let string = String::new("hello");
/// /// assert_eq!(format!("{}", string), "hello");
/// /// ```
/// #[rune::function(protocol = STRING_DISPLAY)]
/// #[rune::function(protocol = DISPLAY_FMT)]
/// fn display(&self, f: &mut Formatter) -> VmResult<()> {
/// vm_write!(f, "{}", self.inner);
/// VmResult::Ok(())
Expand Down
Loading

0 comments on commit f49d1da

Please sign in to comment.