You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This has the problem that it assumes both directions of conversion are infallible, which is not always the case!
We should consider following Rust's conventions for From and TryFrom here.
Ideally, From implementations should always be lossless (never lose information) and should always be infallible (can never revert). Fallible implementations should use a TryFrom alternative that returns a Result so that the user decide whether they want to revert on Err or handle it in some other way.
We can see this convention in Rust's std lib for the primitive types, e.g. it has impl From<u16> for u32, but does not have impl From<u32> for u16. Instead, it offers impl TryFrom<u32> for u16 which checks if the value is out of range.
TODO
Remove the into method from the From<T> trait.
Add a TryFrom<T> trait to support fallible conversions. Note that this requires associated types to ergonomically infer Self::Error type from implementations. Add associated items to impls and traits. #610. In the meantime, we could unblock ourselves from this by returning an Option rather than a Result and use None to imply conversion failure, and then switch to Result in the future once associated types land?
Review From implementations in std to ensure all are infallible (i.e. don't revert). Replace fallible implementations with TryFrom and produce an error instead.
That's fair. I suppose omitting Into for now altogether (until we're able to implement it properly) is better than having the bad implementation that we have today.
## Type of change
<!--Delete points that do not apply-->
- New feature
## Changes
The following changes have been made:
- Added `StorageString` type
- Added `from_raw_slice()` and `as_raw_slice()` to the `String` type
- Updated src README wording, links, and added `StorageString`
- Moved `String` into `sway-libs/libs/strings` folder with the
`StorageString` type
- Updated `String` README and SPECIFICATION to remove outdated info
- Updated `String` to use non-mutable types in `from_utf8()`
## Notes
- The `From<raw_slice>` implementation for the `String` type is
commented out until FuelLabs/sway#3637 is
resolved. The `from_raw_slice()` shall be removed when this is
reimplemented
- Tests for the `from_raw_slice()` and `as_raw_slice()` functions have
been commented out until FuelLabs/sway#4408 is
resolved
- Until FuelLabs/fuels-rs#940 is resolved,
developers must return any `StorageString`s from storage as a `Bytes`
type
- This should unblock NFT URIs, Fuel Name Service, token standards, and
more
## Related Issues
<!--Delete everything after the "#" symbol and replace it with a number.
No spaces between hash and number-->
Closes#40
---------
Co-authored-by: bitzoic <cameron.carstens@fuel.sh>
Currently, our
From
trait looks like this:This has the problem that it assumes both directions of conversion are infallible, which is not always the case!
We should consider following Rust's conventions for
From
andTryFrom
here.Ideally,
From
implementations should always be lossless (never lose information) and should always be infallible (can never revert). Fallible implementations should use aTryFrom
alternative that returns aResult
so that the user decide whether they want to revert onErr
or handle it in some other way.We can see this convention in Rust's std lib for the primitive types, e.g. it has
impl From<u16> for u32
, but does not haveimpl From<u32> for u16
. Instead, it offersimpl TryFrom<u32> for u16
which checks if the value is out of range.TODO
into
method from theFrom<T>
trait.TryFrom<T>
trait to support fallible conversions. Note that this requires associated types to ergonomically inferSelf::Error
type from implementations. Add associated items toimpl
s andtrait
s. #610. In the meantime, we could unblock ourselves from this by returning anOption
rather than aResult
and useNone
to imply conversion failure, and then switch toResult
in the future once associated types land?From
implementations in std to ensure all are infallible (i.e. don't revert). Replace fallible implementations withTryFrom
and produce an error instead.Into<T>
until we support blanket implementations i.e. Implementtrait Into<T>
in lib-std #3399.The text was updated successfully, but these errors were encountered: