Commit Graph

13 Commits

Author SHA1 Message Date
Martin Habovstiak e8a42d5851 Unify/reduce usage of `unsafe`
Since the introduction of `Script` `unsafe` started slowly creeping in
as more types with similar semantics were added. The `unsafe` in these
cases is just for trivial conversions between various pointer-like
types. As such, it's possible to move these into a single macro that
takes care of the conversions at one place and avoid repeating the same
`unsafe` code in the codebase. This decreases the cost of audits which
now only need to happen in `internals`, focuses any changes to happen in
that single macro and decreases the chance that we will mess up
similarly to the recent `try_into().expect()` issue (but this time with
UB rather than panic).

The new macro accepts syntax very similar to the already-existing struct
declarations with these differences:

* The struct MUST NOT have `#[repr(transparent)]` - it's added by the
  macro
* If the struct uses `PhantomData` it must be the first field and the
  real data must be the second field (to allow unsized types).
* The struct must be immediately followed by an impl block containing at
  least on conversion function.
* If the struct has generics the impl block has to use the same names of
  generics.
* The conversion functions don't have bodies (similarly to required
  trait methods) and have a fixed set of allowed signatures.
* Underscore (`_`) must be used in place of the inner type in the
  conversion function parameters.

The existing code can simply call the macro with simple changes and get
the same behavior without any direct use of `unsafe`. This change
already calls the macro for all relevant existing types. There are still
some usages left unrelated to the macro, except one additional
conversion in reverse direction on `Script`. It could be moved as well
but since it's on a single place so far it's not really required.
2025-03-27 20:15:37 +01:00
Martin Habovstiak 5680b4e870 Refer to `Script{Buf}` as `Self` where relevant
Using `Self` instead of specific type name can make some refactorings
easier.
2025-02-20 19:30:07 +01:00
Martin Habovstiak 0567e6fe1d Put `#[inline]` on most `Script{Buf}` methods
These methods are either newtype casts that should compile to no-ops or
directly calling into some other function with at most some pointer
adjustments. As such making them `#[inline]` is definitely benefitial.
There are also methods that check length and then call some other
function. These are also worth inlining since the length could be known
at compile time and the check could be eliminated.
2025-02-20 16:18:21 +01:00
Martin Habovstiak b7e2af1b6b Implement `Arbitrary` for `&'a Script`
The `Arbitrary` trait allows zero-copy implementations for borrowed
types, so this adds the implementation for borrowed `Script`.
2025-02-20 16:02:22 +01:00
Martin Habovstiak 3b15e900f0 Add `const` to some `Script` methods
We're confident these methods will never perform syscalls, so it's fine
to add `const` to them.
2025-02-20 15:49:46 +01:00
Martin Habovstiak 277223da6a Make `Script` and `ScriptBuf` obey sanity rules
The newtype sanity rules (a name I came up with):
* Newtypes should have at most one constructor that directly references
  the inner field.
* Newtypes should have at most three accessor methods that directly
  reference the ineer field: one for owned access, the second for
  borrowed and the third for mutably borrowed.
* All other methods should use the methods above to perform operations
  on the newtype and not directly access the fields.

This commit makes `Script` and `ScriptBuf` obey these except for
`reserve` and `reserve_exact` since we don't have `as_mut_vec` method.
As a side effect it also adds `const` to `ScriptBuf::from_bytes`.
2025-02-20 15:35:17 +01:00
jrakibi ab2f709181 Implement Default for Script
Add Default implementation for Script that returns an empty script.

ScriptBuf already has #[derive(Default)] in the code so it's already handled
2025-02-13 03:22:37 +05:30
Jamil Lambert, PhD 566a6e5da6
Add `Script` tests
Add tests to `borrowed` to kill the mutants found by `cargo mutants`.
Tests for `to_bytes` were not added since it is deprecated.
2025-01-28 20:30:31 +00:00
Tobin C. Harding 549be547ac
primitives: Add must_use
Enable lint `clippy::return_self_not_must_use` and add attribute
`must_use` as required.

Also run the linter with `clippy::must_use_candidate` enabled and
manually check every warning site.

While we are at it change the current `must_use` usages to have no
message. We can always add a message later if needed.
2024-12-10 11:02:06 +11:00
Jamil Lambert, PhD 27f94d5540
Replace `creates` with `constructs`
In functions that act like constructors there is a mixture of the usage
of `creates` and `constructs`.

Replace all occurrences of `creates` with `constructs` in the first line
of docs of constructor like functions.
2024-11-05 12:47:28 +00:00
Tobin C. Harding 3af3239ad0
script: Re-order functions
We want our code to be easy to read and our APIs discoverable, for those
of us who read source files the layout matters.

Put the constructors and getters at the top of the impl block.

Code move only, no logic changes.
2024-11-01 07:09:59 +11:00
Tobin C. Harding db40297f87
script: deprecate to_bytes
Deprecate the `Script::to_bytes` function in favour of `to_vec` as we
are doing elsewhere.

Note that `ScriptBuf` has `into_bytes` because it does not copy.
Potentially this should be deprecated in favour of `into_vec`?

Note that in regards to the `to_` prefix this naming as valid according
to convention because the `Script` type is borrowed and `to_vec` copies
the underlying bytes.
2024-11-01 07:09:53 +11:00
Tobin C. Harding d649c06238
Move script types to primitives
Move the `Script` and `ScriptBuf` types to `primitives`, nothing else.
2024-10-14 09:02:39 +11:00