-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 doc aliases for a lot of the C standard library #81988
Conversation
r? @KodrAus (rust-highfive has picked a reviewer for you, use r? to override) |
@@ -279,6 +279,7 @@ impl char { | |||
/// '1'.is_digit(37); | |||
/// ``` | |||
#[stable(feature = "rust1", since = "1.0.0")] | |||
#[doc(alias = "isxdigit")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is moderately iffy because you have to pass a radix of 16, but it's close enough.
#[doc(alias = "delete")] | ||
#[doc(alias = "free")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm very surprised someone added delete
but not free
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This alias does seem very reasonable to add.
#[doc(alias = "atoi")] | ||
#[doc(alias = "atol")] | ||
#[doc(alias = "strtod")] | ||
#[doc(alias = "strtol")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured these were all "close enough" since int
and long
don't have a fixed size.
@@ -1202,6 +1205,7 @@ impl str { | |||
/// | |||
/// [`split_whitespace`]: str::split_whitespace | |||
#[stable(feature = "rust1", since = "1.0.0")] | |||
#[doc(alias = "strtok")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is iffy, but C doesn't really have a split function and I didn't know where else to put strtok.
@@ -873,6 +873,7 @@ impl Command { | |||
/// assert!(output.status.success()); | |||
/// ``` | |||
#[stable(feature = "process", since = "1.0.0")] | |||
#[doc(alias = "system")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could also go under spawn()
. In C system
blocks but returns no value.
Do people really search the Rust standard library using C function names? |
@camelid there are existing aliases for |
Just because there are existing ones doesn't mean we should add more 🙃 |
I think this would be useful if you're learning rust coming from C. |
I can understand that, but that's true of many things. For example, I imagine many people will at least start learning Rust from the Book and Rust by Example (those are some of the ways I started). But perhaps others will jump right in without looking at any language-level docs.
That's true, but I just feel worried that we'll end up littering them all over the place and then not be able to remove them. |
|
A few nitpicks:
Related: if we are going to add aliases for several languages (C, Python, Java...), it would be great if the #[doc(alias = "sprintf", lang = "C, C++, POSIX")] |
#[doc(alias = "null")] | ||
#[doc(alias = "nil")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C does not have nil
, and null
should be NULL
if it is referring to the library side of C.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can separate this into a separate PR if you like, but I think the aliases are useful.
null should be NULL if it is referring to the library side of C.
doc(alias)
is case insensitive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still, writing the original name would be better, no? Otherwise we should change e.g. Map
to map
in the Java one, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, I guess you want to keep null
for other languages that use that one -- again, this depends on how we are looking at this: whether we are trying to keep a mapping or just search terms for any language that matches. If the latter, I agree: null
is better.
library/core/src/primitive.rs
Outdated
@@ -64,4 +64,6 @@ pub use u64; | |||
#[stable(feature = "core_primitive", since = "1.43.0")] | |||
pub use u8; | |||
#[stable(feature = "core_primitive", since = "1.43.0")] | |||
#[doc(alias = "size_t")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are adding these, then we should also add:
_Bool
andbool
forbool
.- The exact-width integer types:
int8_t
,uint8_t
, etc. char32_t
forchar
.ssize_t
forisize
(POSIX, but common enough).- Perhaps
float
anddouble
forf32
andf64
(not guaranteed, but likely).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_Bool and bool for bool.
I have the same question as in #81989 (comment) - what do you see as the point of doc(alias)
? Both of those pull up bool
as the first result in search already: https://doc.rust-lang.org/stable/std/?search=_Bool, https://doc.rust-lang.org/stable/std/?search=bool
The others look useful to add, thanks :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my answer to that comment.
You're welcome! :)
Can you open an issue for this? |
I didn't distinguish very much between "standard C" and "POSIX", I think they're both helpful. Happy to add a bunch more aliases from posix if you think they'd be useful :) |
Of course -- done! #82000 |
At this stage I think I echo @camelid's concern about collecting an unbounded number of aliases that aren't necessarily all useful to have. I think that applies more to the string aliases than the others, which do seem pretty useful. Maybe we should look at landing this without:
aliases. What do you all think? |
This comment has been minimized.
This comment has been minimized.
I took the very scientific approach of "ask some friends what parts of the standard library they use a lot". They suggested https://www.tutorialspoint.com/c_standard_library/ and also the short list `strcpy, strncpy, memcpy, memcpy, printf, scanf, fprintf, sprintf, asprintf, sscanf, strtok, strntok, malloc, calloc, free, strdup, strndup, err, errx `. Many of these already have aliases; those with `*n*` have no analogy in rust; `err` and `errx` have no analogy in Rust. I added most of the rest. This also includes aliases for standard types, like fixed-width integers.
Sure, that seems good to me :) |
We briefly discussed this in the libs meeting, see: #81989 (comment) Some more thoughts related to the kind of aliases in this PR: The Rust standard library reference is probably not the place to explain Aliases can provide no context or explanation, which might lead to confusion or might even come across as somewhat passive aggressive: E.g. someone looking for the raw deallocation api searching for Even for seemingly obvious aliases such as Those of us who were in the meeting agreed that questions like "what's the closest Rust thing to $thing from $language?" are better answered by some kind of cheatsheet or blog post or other resources maintained by the community. We don't know enough about all languages to be able to maintain this as part of the standard library. I personally think some kind of policy for doc aliases in the standard library could be something like "it's okay to add alias $x to $y if someone reasonable expects $y to be named $x in Rust". E.g. the But as I said in the linked comment, we'd first like to see some kind of write down of such a policy proposal and reach a consensus before accepting more doc alias PRs. (I'll put it on my own to-do list to write a draft for it, but I'm probably not going to have time for that any time soon.) |
It seems inconsistent to have an alias for That said, I agree most of the changes here are not super useful. |
I suggested somewhere else to have some extra data for each alias. This was in the context of explaining which language the alias came from (since e.g. same typenames have different meanings in different languages), but having a "reason" or "comment" field overall per alias seems like a good idea to solve the "no context" issue you mention (even if we don't end up with aliases for different languages). |
Agreed. I think |
Remove `delete` alias from `mem::drop`. See rust-lang#81988 (comment) and rust-lang#81988 (comment)
I took the very scientific approach of "ask some friends what parts of
the standard library they use a lot". They suggested
https://www.tutorialspoint.com/c_standard_library/ and also the short
list
strcpy, strncpy, memcpy, memcpy, printf, scanf, fprintf, sprintf, asprintf, sscanf, strtok, strntok, malloc, calloc, free, strdup, strndup, err, errx
. Many of these already have aliases; those with*n*
have no analogy in rust;asprintf
,err
, anderrx
have no analogy in Rust.I added most of the rest.