aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Programming
  • Software

Swift System Is Now Open Source

  • aster.cloud
  • September 25, 2020
  • 4 minute read

In June, Apple introduced Swift System, a new library for Apple platforms that provides idiomatic interfaces to system calls and low-level currency types. I’m excited to announce that we’re open-sourcing System and adding Linux support! Our vision is for System to eventually act as the single home for low-level system interfaces for all supported Swift platforms.

 


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

Goodbye Imported C Interfaces

Most operating systems today support some flavor of system interfaces written in C that have existed for decades. While it is possible to use these APIs directly from Swift, these weakly-typed system interfaces imported from C can be error-prone and unwieldy. For example, the open system call (available on UNIX-like operating systems such as Linux and Apple platforms) imports as a pair of global functions:

<span class="kd">func</span> <span class="nf">open</span><span class="p">(</span><span class="n">_</span> <span class="nv">path</span><span class="p">:</span> <span class="kt">UnsafePointer</span><span class="o"><</span><span class="kt">CChar</span><span class="o">></span><span class="p">,</span> <span class="n">_</span> <span class="nv">oflag</span><span class="p">:</span> <span class="kt">Int32</span><span class="p">)</span> <span class="o">-></span> <span class="kt">Int32</span>
<span class="kd">func</span> <span class="nf">open</span><span class="p">(</span><span class="n">_</span> <span class="nv">path</span><span class="p">:</span> <span class="kt">UnsafePointer</span><span class="o"><</span><span class="kt">CChar</span><span class="o">></span><span class="p">,</span> <span class="n">_</span> <span class="nv">oflag</span><span class="p">:</span> <span class="kt">Int32</span><span class="p">,</span> <span class="n">_</span> <span class="nv">mode</span><span class="p">:</span> <span class="n">mode_t</span><span class="p">)</span> <span class="o">-></span> <span class="kt">Int32</span>

These weakly-typed functions suffer from several shortcomings and fail to utilize the expressivity and type safety of Swift:

  • File descriptors, alongside options, commands, errno, and other values, are imported as ordinary Int32s.
  • The oflag argument is actually a logical OR-ing of exactly one file access mode and any number of flags, but this is not captured in the type of oflag.
  • Callers of open have to remember to check for a negative return value indicating an error, and if so check the value of the global variable errno to know what error occurred. Additionally, some system calls may be canceled if a signal occurred, requiring callers to remember to write a loop around such calls checking for EINTR errors.
  • File paths are unmanaged pointers, and if they are derived from a managed object (e.g. Array<CChar>), then callers must ensure that array is always null-terminated.
Read More  Introducing Swift Cluster Membership

None of these semantic rules are captured in the API’s signature, preventing the programming language from guiding the user towards correct usage of the API.

 

Hello Idiomatic Swift Interfaces

The System module brings various language features to bear to improve expressivity and eliminate these opportunities for error. For example, System defines the open system call as a static function with defaulted arguments in the FileDescriptor namespace:

<span class="kd">extension</span> <span class="kt">FileDescriptor</span> <span class="p">{</span>
  <span class="c1">/// Opens or creates a file for reading or writing.</span>
  <span class="c1">///</span>
  <span class="c1">/// - Parameters:</span>
  <span class="c1">///  - path: The location of the file to open.</span>
  <span class="c1">///  - mode: The read and write access to use.</span>
  <span class="c1">///  - options: The behavior for opening the file.</span>
  <span class="c1">///  - permissions: The file permissions to use for created files.</span>
  <span class="c1">///  - retryOnInterrupt: Whether to retry the open operation</span>
  <span class="c1">///    if it throws `Errno.interrupted`.</span>
  <span class="c1">///    The default is `true`.</span>
  <span class="c1">///    Pass `false` to try only once and throw an error upon interruption.</span>
  <span class="c1">/// - Returns: A file descriptor for the open file</span>
  <span class="c1">///</span>
  <span class="c1">/// The corresponding C function is `open`.</span>
  <span class="kd">public</span> <span class="kd">static</span> <span class="kd">func</span> <span class="nf">open</span><span class="p">(</span>
    <span class="n">_</span> <span class="nv">path</span><span class="p">:</span> <span class="kt">FilePath</span><span class="p">,</span>
    <span class="n">_</span> <span class="nv">mode</span><span class="p">:</span> <span class="kt">FileDescriptor</span><span class="o">.</span><span class="kt">AccessMode</span><span class="p">,</span>
     <span class="nv">options</span><span class="p">:</span> <span class="kt">FileDescriptor</span><span class="o">.</span><span class="kt">OpenOptions</span> <span class="o">=</span> <span class="kt">FileDescriptor</span><span class="o">.</span><span class="kt">OpenOptions</span><span class="p">(),</span>
     <span class="nv">permissions</span><span class="p">:</span> <span class="kt">FilePermissions</span><span class="p">?</span> <span class="o">=</span> <span class="kc">nil</span><span class="p">,</span>
     <span class="nv">retryOnInterrupt</span><span class="p">:</span> <span class="kt">Bool</span> <span class="o">=</span> <span class="kc">true</span>
  <span class="p">)</span> <span class="k">throws</span> <span class="o">-></span> <span class="kt">FileDescriptor</span>
<span class="p">}</span>

When one compares this version of open to the original version from C, several significant differences stand out:

  • System pervasively uses raw representable structs and option sets. These strong types help catch mistakes at compile time and are trivial to convert to and from the weaker C types.
  • Errors are thrown using the standard language mechanism and cannot be missed. Further, all system calls interruptible by a signal take a defaulted-true retryOnInterrupt argument, causing them to retry on failure. When combined, these two changes dramatically simplify error and signal handling.
  • FilePath is a managed, null-terminated bag-of-bytes that conforms to ExpressibleByStringLiteral — far safer to work with than a UnsafePointer<CChar>.

The result is code that reads and behaves like idiomatic Swift. For example, this code creates a file path from a string literal and uses it to open and append to a log file:

<span class="k">let</span> <span class="nv">message</span><span class="p">:</span> <span class="kt">String</span> <span class="o">=</span> <span class="s">"Hello, world!"</span> <span class="o">+</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span>
<span class="k">let</span> <span class="nv">path</span><span class="p">:</span> <span class="kt">FilePath</span> <span class="o">=</span> <span class="s">"/tmp/log"</span>
<span class="k">let</span> <span class="nv">fd</span> <span class="o">=</span> <span class="k">try</span> <span class="kt">FileDescriptor</span><span class="o">.</span><span class="nf">open</span><span class="p">(</span>
  <span class="n">path</span><span class="p">,</span> <span class="o">.</span><span class="n">writeOnly</span><span class="p">,</span> <span class="nv">options</span><span class="p">:</span> <span class="p">[</span><span class="o">.</span><span class="n">append</span><span class="p">,</span> <span class="o">.</span><span class="n">create</span><span class="p">],</span> <span class="nv">permissions</span><span class="p">:</span> <span class="o">.</span><span class="n">ownerReadWrite</span><span class="p">)</span>
<span class="k">try</span> <span class="n">fd</span><span class="o">.</span><span class="n">closeAfter</span> <span class="p">{</span>
  <span class="n">_</span> <span class="o">=</span> <span class="k">try</span> <span class="n">fd</span><span class="o">.</span><span class="nf">writeAll</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">utf8</span><span class="p">)</span>
<span class="p">}</span>

 

Read More  Celebrating Black History Month

A Multi-platform Library

System is a multi-platform library, not a cross-platform one. It provides a separate set of APIs and behaviors on every supported platform, closely reflecting the underlying OS interfaces. A single import will pull in the native platform interfaces specific for the targeted OS.

Our immediate goal is to simplify building cross-platform libraries and applications such as SwiftNIO and the Swift Package Manager. System does not eliminate the need for #if os() conditionals to implement cross-platform abstractions, but it does make it safer and more expressive to fill out the platform-specific parts.

 

What’s Next?

System is only in its infancy—it currently includes a small number of system calls, currency types, and convenience functionality. As part of the effort to increase the API coverage, we’ll be working to adopt System in the Swift Package Manager. This will include enhancements to FilePath and adding support for the recently announced Swift on Windows.

There’s a ton of exciting work left to do. System (especially the forthcoming Windows support!) is a fantastic opportunity to get involved in the Swift project and help it grow into a strong, vibrant, cross-platform ecosystem.

 

Get Involved

Your experience, feedback, and contributions are greatly encouraged!

  • Get started by trying out the System package on GitHub,
  • Discuss the library and get help in the Swift System forum,
  • Open an issue with problems you find or ideas you have for improvements,
  • And as always, pull requests are welcome!

 

Questions?

Please feel free to ask questions about this post in the associated thread on the Swift forums.

Read More  How Open Source Software Is Fighting COVID-19

 

Michael Ilseman is an engineer on the Swift Standard Library team at Apple.

For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

aster.cloud

Related Topics
  • GitHub
  • Open Source
  • Swift
  • Swift System
  • SwiftNIO
You May Also Like
View Post
  • Software
  • Technology

Canonical Releases Ubuntu 25.04 Plucky Puffin

  • April 17, 2025
View Post
  • Software
  • Technology

IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management

  • March 27, 2025
Vehicle manufacturing
View Post
  • Software

IBM Study: Vehicles Believed to be Software Defined and AI Powered by 2035

  • December 12, 2024
aster-cloud-tux-gaming
View Post
  • Computing
  • Gears
  • Software

5 best Linux distributions for gamers in 2024

  • September 11, 2024
Crab
View Post
  • Gears
  • Learning
  • Software

The Best Friends for a Rustacean. Top Books in Learning Rust.

  • August 25, 2024
Coffee | Laptop | Notebook | Work
View Post
  • Software

The Hidden Economy Of Open Source Software

  • April 28, 2024
Redis logo
View Post
  • Platforms
  • Software

Redis Moves To Source-Available Licenses

  • April 2, 2024
View Post
  • Software
  • Technology

Charmed MongoDB Enters General Availability

  • March 26, 2024

Stay Connected!
LATEST
  • college-of-cardinals-2025 1
    The Definitive Who’s Who of the 2025 Papal Conclave
    • May 7, 2025
  • conclave-poster-black-smoke 2
    The World Is Revalidating Itself
    • May 6, 2025
  • 3
    Conclave: How A New Pope Is Chosen
    • April 25, 2025
  • Getting things done makes her feel amazing 4
    Nurturing Minds in the Digital Revolution
    • April 25, 2025
  • 5
    AI is automating our jobs – but values need to change if we are to be liberated by it
    • April 17, 2025
  • 6
    Canonical Releases Ubuntu 25.04 Plucky Puffin
    • April 17, 2025
  • 7
    United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services
    • April 15, 2025
  • 8
    Tokyo Electron and IBM Renew Collaboration for Advanced Semiconductor Technology
    • April 2, 2025
  • 9
    IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management
    • March 27, 2025
  • 10
    Tariffs, Trump, and Other Things That Start With T – They’re Not The Problem, It’s How We Use Them
    • March 25, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    IBM contributes key open-source projects to Linux Foundation to advance AI community participation
    • March 22, 2025
  • 2
    Co-op mode: New partners driving the future of gaming with AI
    • March 22, 2025
  • 3
    Mitsubishi Motors Canada Launches AI-Powered “Intelligent Companion” to Transform the 2025 Outlander Buying Experience
    • March 10, 2025
  • PiPiPi 4
    The Unexpected Pi-Fect Deals This March 14
    • March 13, 2025
  • Nintendo Switch Deals on Amazon 5
    10 Physical Nintendo Switch Game Deals on MAR10 Day!
    • March 9, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.