Conversation
|
Maybe this should leave under |
|
@bew There is precedence for conditional platform dependant code elsewhere, ex: |
|
@wmoxam from CI there are two files where the formatter is not happy. Error: formatting 'src/lib_c/amd64-unknown-openbsd/c/sysctl.cr' produced changes To clarify @bew comment, #4450 is the first PR trying to move all the platform specifics in it's own namespace and offer a better abstraction. If you want to can take the stab here in this same PR. But if not a refactor will follow probably. |
|
Okay good to know. Thanks @bcardiff |
|
I'll refactor to match #4450 |
|
@wmoxam that PR is now merged. You can base the refactor with that final structure. |
|
Thanks @bcardiff I'll have it ready later this week |
src/crystal/system/system.cr
Outdated
| @@ -0,0 +1,24 @@ | |||
| # :nodoc | |||
There was a problem hiding this comment.
We don't want to :nodoc: Crystal as we loose documenting macros and Crystal::VERSION. The :nodoc: should be placed on Crystal::System only.
src/crystal/system/system.cr
Outdated
| # :nodoc | ||
| module System | ||
| # :nodoc | ||
| module System |
There was a problem hiding this comment.
I think that the methods in this module could go in just Crysatal::System itself.
There was a problem hiding this comment.
Like replace src/system.cr with this file?
There was a problem hiding this comment.
no keep the files the same but place the methods in Crystal::System not Crystal::System::System. It seems redundant and these are "misc" methods which can just go in Crystal::System itself.
There was a problem hiding this comment.
IMO these are not "misc" methods, they are "System" (aka local computer system) utility methods for querying a local system's properties.
My understanding of the structure is that Crystal::System is simply a wrapper namespace for platform specific implementations.
Crystal::System::System is meant to contain platform specific implementations of the "System" methods.
It's confusing for sure since we're using the word 'System" for two different things. Perhaps a there is a different name that is more suitable?
There was a problem hiding this comment.
What about Crystal::System::Inspect?
There was a problem hiding this comment.
@bcardiff If we changed that name we'd have to change the name of System to Inspect which would make little sense. I'd rather have bit of duplication than a nonsensical name.
There was a problem hiding this comment.
It's true there is no much value for some inspection method to be in it's own module for now.
Let's go with @RX14 proposal. So,
src/crystal/system.crwith the if flags to includesrc/crystal/{platform}/system.cr/etc.src/crystal/{platform}/system.cr/etc. with the actual calls to lib C.- Leave
src/system.crwithrequire "crystal/system"and delagation toCrystal::System.hostname.
::System is the public api with documentation.
::Crystal::System is :nodoc:. We can refactor later if many method end up there.
Otherwise it will affect documentation elsewhere
src/crystal/system.cr
Outdated
| # :nodoc | ||
| module System | ||
| # Returns the hostname | ||
| # def.self.hostname |
src/crystal/system.cr
Outdated
| require "./system/unix/sysctl_cpucount" | ||
| {% else %} | ||
| # TODO: restrict on flag?(:unix) after crystal > 0.22.0 is released | ||
| require "./system/unix/sysconf_cpucount" |
There was a problem hiding this comment.
The indentation on these two lines is off.
There was a problem hiding this comment.
Weird that the formatter didn't pick up on this
There was a problem hiding this comment.
@wmoxam it's because the formatter doesn't format macro code
| raise Errno.new("Could not get hostname") | ||
| end | ||
| len = LibC.strlen(buffer) | ||
| {len, len} |
There was a problem hiding this comment.
Are you sure this string doesn't contain utf8?
There was a problem hiding this comment.
I didn't implement this, I only moved it 😅
| if LibC.sysctl(mib, 2, pointerof(ncpus), pointerof(size), nil, 0) == 0 | ||
| ncpus | ||
| else | ||
| -1 |
There was a problem hiding this comment.
Possibly? I was just matching the behaviour of the existing sysconf version
Is that the behaviour of other standard Crystal libs, or do they just rely on return value?
|
|
||
| module Crystal::System | ||
| def self.cpu_count | ||
| LibC.sysconf(LibC::SC_NPROCESSORS_ONLN) |
There was a problem hiding this comment.
Similarly, we should check the return value and raise.
There was a problem hiding this comment.
I think this could be part of a separate PR as the return value isn't checked elsewhere. Ex: Process.times
|
If changes to the API are required it may later in another pull request, but this shouldn't delay this patch which fixes issues on *BSD. |
|
I forgot that this was just a straight copy of the existing behaviour... Returning |
|
Build #4466 errored on osx with xcode8.2, again... Would it be a good time to merge #4528? |
bcardiff
left a comment
There was a problem hiding this comment.
LGTM. Should be merged as a single commit.
System.cpu_countalways returns -1 on BSD since fetching the number of CPUs viasysconfis not supported.Instead use
sysctlto fetch cpu count.