Skip to content

Introduce a strong-type template#3165

Merged
reneme merged 1 commit intomasterfrom
chore/strong_types
Jan 12, 2023
Merged

Introduce a strong-type template#3165
reneme merged 1 commit intomasterfrom
chore/strong_types

Conversation

@reneme
Copy link
Copy Markdown
Collaborator

@reneme reneme commented Jan 9, 2023

Usage:

// declare a strong type (`struct SessionID_` is needed to make this
// a unique type among all the other byte vector based types)
using SessionID = Strong<std::vector<uint8_t>, struct SessionID_>

SessionID sid{unlock(rng.random_vec(32))};
// I'm hoping this will eventually be:
// auto sid = rng.random_range<SessionID>(32);

// Strong Types try hard to act like their underlying type without
// doing implicit conversion between each other
std::cout << hex_encode(sid); // assumg `hex_encode` has a `std::span<>` overload

With that, constructors that take two byte vectors are much more ergonomic. No more mixing up parameters. Overloading constructors (despite both overloads being byte vectors):

using SessionID = Strong<std::vector<uint8_t>, struct SessionID_>
using SessionTicket = Strong<std::vector<uint8_t>, struct SessionTicket_>

// A Session_Handle must be constructed with an ID, a Ticket or both
// but it cannot be constructed without either of them.
struct Session_Handle {
  Session_Handle(SessionID id);
  Session_Handle(SessionTicket ticket);
  Session_Handle(SessionID id, SessionTicket ticket);
};

@reneme reneme force-pushed the chore/strong_types branch 2 times, most recently from 833ed42 to a99defc Compare January 9, 2023 14:28
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jan 9, 2023

Codecov Report

Base: 88.03% // Head: 88.04% // Increases project coverage by +0.01% 🎉

Coverage data is based on head (846807c) compared to base (8fa6793).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3165      +/-   ##
==========================================
+ Coverage   88.03%   88.04%   +0.01%     
==========================================
  Files         604      605       +1     
  Lines       67228    67292      +64     
  Branches     6736     6737       +1     
==========================================
+ Hits        59181    59247      +66     
+ Misses       5221     5219       -2     
  Partials     2826     2826              
Impacted Files Coverage Δ
src/tests/test_strong_type.cpp 100.00% <100.00%> (ø)
src/fuzzer/uri.cpp 75.00% <0.00%> (-5.00%) ⬇️
src/lib/pubkey/mce/gf2m_rootfind_dcmp.cpp 95.38% <0.00%> (-2.31%) ⬇️
src/cli/tls_proxy.cpp 81.81% <0.00%> (-0.54%) ⬇️
src/lib/tls/tls12/tls_record.cpp 91.80% <0.00%> (-0.41%) ⬇️
src/tests/test_bigint.cpp 90.83% <0.00%> (+0.04%) ⬆️
src/fuzzer/barrett.cpp 72.22% <0.00%> (+5.55%) ⬆️
src/lib/utils/thread_utils/semaphore.cpp 100.00% <0.00%> (+54.54%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@reneme reneme force-pushed the chore/strong_types branch from a99defc to 7100e56 Compare January 9, 2023 15:11
@randombit
Copy link
Copy Markdown
Owner

Windows CI failure is relevant. We could (IMO) consider dropping VC2019 support for 3.0; in the long run it's very likely to hold us back in terms of what 2020 features can be used.

@reneme
Copy link
Copy Markdown
Collaborator Author

reneme commented Jan 9, 2023

Ugh... I already tried to work around that. Apparently without much luck. I'll look into it once more. So far 2019 did keep better pace than other compilers. But I agree, it's likely that it'll become painful.

@reneme reneme force-pushed the chore/strong_types branch from 7100e56 to 846807c Compare January 10, 2023 07:53
@reneme
Copy link
Copy Markdown
Collaborator Author

reneme commented Jan 10, 2023

I'll look into it once more.

I found another (IMO more elegant) way to get 2019 to do what I want. For now, it may stay. ;)

@randombit
Copy link
Copy Markdown
Owner

randombit commented Jan 10, 2023

OK. Should we in the supported platform list for 3.0 say instead of "Visual C++ 2019 or higher" something like "Visual C++ 2022, with VC 2019 supported on an interim basis"?

Toolchains tend to be sticky in projects (more so on the Windows side of things, in my experience) and I don't want to bait and switch someone, where they start a project using Botan 3 plus VC 2019, and everything works great until without warning in some minor release we drop VC2019 support because it got inconvenient.

Copy link
Copy Markdown
Owner

@randombit randombit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine to merge, we can sort out the VC2019 support thing in a ticket.

@reneme reneme merged commit a548f1f into master Jan 12, 2023
@randombit randombit deleted the chore/strong_types branch January 12, 2023 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants