Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion to/from std types #263

Open
4 of 11 tasks
danakj opened this issue Jun 14, 2023 · 0 comments
Open
4 of 11 tasks

Conversion to/from std types #263

danakj opened this issue Jun 14, 2023 · 0 comments
Labels
design Design of the library systems as a whole, such as concepts std-compat Compatibility with std library types or concepts

Comments

@danakj
Copy link
Collaborator

danakj commented Jun 14, 2023

  • integers <-> native
  • floats<-> native
  • Option <-> std::optional
  • Result <-> std::expected (c++23...so far away)
  • Tuple <-> std::tuple
  • input_range <-> Iterator
  • Vec <-> std::vector
  • Array <-> std::array
  • Slice/SliceMut <-> std::span
  • Box <-> std::unique_ptr
    • Can we convert errors to unique_ptr<DynError>? And strings?

References?

The tricky thing is when we have a reference type in the sus type.

  • Option<T&> can convert to optional<T*>, but optional<T*> will convert to Option<T*>.
  • Or should Option<T&> convert to optional<T>?
  • Same question for Result<T&, E>.

Vec

For Vec, we can't construct a std::vector with a pointer of our choosing, or rip the pointer out of it. So proposing that instead of Vec<T, Allocator> we have Vec<T, Driver>.

The Driver will provide the Allocator but it will also provide an abstract API that can choose how to store the Vec's data. Then we provide two Drivers:

  • VecDriver which uses the Slice pointers in the Vec in-place to manage the memory (this is how the code is written today). The VecDriver is an empty class so it does not contribute to Vec's size at all.
  • StdDriver which stores a std::vector inside it, and then mirrors the std::vector's pointers into the Vec's Slice pointers.

What this means is we can construct a Vec<T, StdDriver<T>> from a std::vector with just a (cheap) move of the std::vector, and we can unwrap the Vec<T, StdDriver<T>> back into a std::vector<T> as well. The Vec with a StdDriver is necessarily larger, unless we commit ABI crimes and just read/write from the std::vector as a char array. That is another possible Driver option though.

Converting between Vec<T, StdDriver<T>> and std::vector<T> would be ~the same as a move of std::vector<T>. Whereas converting between Vec<T> and std::vector<T> will require an allocation and a move of each item between buffers.

Because allocators are also a thing, the Driver would have an optional Allocator type parameter, so technically its Vec<T, Driver<Allocator>>.

@danakj danakj added the design Design of the library systems as a whole, such as concepts label Jun 14, 2023
@danakj danakj added this to the stable-numerics milestone Jun 14, 2023
@danakj danakj modified the milestones: stable-numerics, containers Jul 31, 2023
@danakj danakj added the std-compat Compatibility with std library types or concepts label Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design of the library systems as a whole, such as concepts std-compat Compatibility with std library types or concepts
Projects
None yet
Development

No branches or pull requests

1 participant