Repository: rust-bitcoin/rust-bitcoin (language: rust)

Issue:
Should script types implement `Default`
`ScriptBuf` and `Script` have `new` constructors with empty paramter list - they should probably implement `Default`.


Discussion / hints:
I can't remember if we talked about this, and if so what we said.
If not we could add `ScriptBuf::empty()`
Yeah, I think these should implement `Default`. It's a pretty clear convention that types which can be "empty" have a `Default` impl that gives you the empty one.

I'm also struggling to imagine a situation when this would be dangerous. If you're constructing a script at all without a public key that has a clear chain of custody then your code is broken and no amount of API papercuts is going to change that.
`ScriptBuf` already has `#[derive(Default)]` in the code so it's already handled.

For `Script` not sure if we can implement `Default` since it's unsized slice wrapper and Default require Sized, unless if we do something like:

```rust
impl Default for &'static Script {
    fn default() -> Self {
        Script::new()
    }
}
```
I think just `ScriptBuf` is fine. Super interesting idea to implement `Default` for references to `Script`....I think that's a bit too far-out in terms of API design but I will keep the idea in the back of my mind because it's a pretty neat thought that users could have a reference-containing struct and still be able to default-construct it.
Got it. Since `ScriptBuf` already has `#[derive(Default)]` I think no changes are needed
Thanks for investigating @jrakibi.
`&OsStr` has `Default`, so I think the impl makes sense. Note that we don't need explicit `'static`.
Since `&OsStr` implements `Default`, it makes sense for `&Script` to follow the same pattern. 

Just found out that `str` also does. 
https://doc.rust-lang.org/src/core/str/mod.rs.html#2797

Neat! OK, then we should too.

Fix patch:
diff --git a/primitives/src/script/borrowed.rs b/primitives/src/script/borrowed.rs
index af5c48ebc8..6e68a6ef69 100644
--- a/primitives/src/script/borrowed.rs
+++ b/primitives/src/script/borrowed.rs
@@ -56,6 +56,11 @@ use crate::prelude::{Box, ToOwned, Vec};
 #[repr(transparent)]
 pub struct Script(pub(in crate::script) [u8]);
 
+impl Default for &Script {
+    #[inline]
+    fn default() -> Self { Script::new() }
+}
+
 impl ToOwned for Script {
     type Owned = ScriptBuf;
 


Test patch: