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

Add vec macros #438

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add vec macros #438

wants to merge 1 commit into from

Conversation

YuhanLiin
Copy link
Contributor

Add vec and vec_with_cap macros
Resolves #344

@YuhanLiin YuhanLiin mentioned this pull request Jan 10, 2024
/// array.
///
/// The length of the provided array must be `N`.
pub fn from_array_same_cap(src: [T; N]) -> Self {
Copy link
Member

Choose a reason for hiding this comment

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

N can be constructed in the macro, so this isn't needed.

@reitermarkus
Copy link
Member

I think instead of vec_with_cap, an API like

vec![1, 2, 3]    // Vec::<_, 3>::from([1, 2, 3])
vec![1; 3]       // Vec::<_, 3>::from([1; 3])
vec![1, 2, 3; 6] // Vec::<_, 6>::from([1, 2, 3])
vec![1; 3; 6]    // Vec::<_, 6>::from([1; 3])

would work.

@birkenfeld
Copy link
Contributor

That's ambiguous though in the case of vec![1; 3].

@reitermarkus
Copy link
Member

Good point. In that case, maybe : as separator would work, e.g. vec![1: 3] vs vec![1; 3: 3]. This also slightly matches the general : syntax for specifying types.

Then again, I think

let vec: Vec<_, 6> = vec![1; 3];

should work when using Vec::from_array, right? So supporting capacity in the macro may not be necessary, to avoid deviating from how the macro works in std.

@YuhanLiin
Copy link
Contributor Author

YuhanLiin commented Jan 26, 2024

If we don't go with the vec![1; 3: 3] version with explicit capacity, then we won't be able to use the macro inline in a lot of places, such as somefunction(vec![0; 3]). It'll need to be in a declaration statement with an explicit type like let v: Vec<_, 3> = vec![...], otherwise the compiler might not know the capacity, leading to ambiguity. I don't know if we care about that.

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.

Feature request create a vec macro
3 participants