├── .gitignore ├── LICENSE ├── README.md ├── iOS └── Example │ ├── Example.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata │ └── Example │ ├── AppDelegate.swift │ ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Example-Bridging-Header.h │ ├── Info.plist │ ├── Message.swift │ ├── ViewController.swift │ ├── libmessage.a │ └── message.h └── rust └── message ├── .gitignore ├── Cargo.toml ├── message.h └── src ├── lib.rs ├── message.rs └── string.rs /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | .build/ 41 | 42 | # CocoaPods 43 | # 44 | # We recommend against adding the Pods directory to your .gitignore. However 45 | # you should judge for yourself, the pros and cons are mentioned at: 46 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 47 | # 48 | # Pods/ 49 | 50 | # Carthage 51 | # 52 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 53 | # Carthage/Checkouts 54 | 55 | Carthage/Build 56 | 57 | # fastlane 58 | # 59 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 60 | # screenshots whenever they are needed. 61 | # For more information about the recommended setup visit: 62 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 63 | 64 | fastlane/report.xml 65 | fastlane/Preview.html 66 | fastlane/screenshots 67 | fastlane/test_output 68 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Wojtek Lukaszuk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rust-to-ios 2 | [![Swift 5.0](https://img.shields.io/badge/swift-5.0-orange.svg?style=flat)](#) 3 | [![@wojteklu](https://img.shields.io/badge/contact-@wojteklu-blue.svg?style=flat)](https://twitter.com/wojteklu) 4 | 5 | Example project for building a library for iOS. 6 | 7 | * ✓ sending primitives between Rust and iOS 8 | * ✓ sending strings between Rust and iOS 9 | * ✓ giving ownership of a Rust instance to Swift 10 | 11 | Setup 12 | ----- 13 | 14 | 1. Download [rustup](https://www.rustup.rs/) needed to setup Rust for cross-compiling. 15 | 16 | ```sh 17 | curl https://sh.rustup.rs -sSf | sh 18 | ``` 19 | 20 | 2. Download targets for iOS. 21 | 22 | ```sh 23 | rustup target add aarch64-apple-ios armv7-apple-ios armv7s-apple-ios x86_64-apple-ios i386-apple-ios 24 | ``` 25 | 26 | 3. Install cargo-lipo to generate the iOS universal library. 27 | 28 | ```sh 29 | cargo install cargo-lipo 30 | ``` 31 | 32 | Building the library 33 | -------------------- 34 | 35 | 1. Create a new cargo project. 36 | 37 | ```sh 38 | cargo new message 39 | ``` 40 | 41 | 2. Update Cargo.toml by adding the [lib] section. 42 | 43 | ```sh 44 | [lib] 45 | name = "message" 46 | crate-type = ["staticlib"] 47 | ``` 48 | 49 | 3. Write the library and [expose its public interface in a C header](https://doc.rust-lang.org/book/first-edition/ffi.html). 50 | 51 | 4. Build the library. 52 | 53 | ```sh 54 | cd message 55 | 56 | cargo lipo --release 57 | ``` 58 | 59 | Using the library 60 | ----------------- 61 | 62 | 1. Create the iOS project. 63 | 64 | 2. Add the C header to allow using the Rust functions from C. 65 | 66 | 3. Copy `target/universal/release/libmessage.a` to project. 67 | 68 | 4. Add `libresolv.tbd` to `Linked frameworks and libraries`. 69 | 70 | 5. Note that `cargo-lipo` does not support bitcode yet. You must set `ENABLE_BITCODE` to `NO`. 71 | 72 | ## Author 73 | 74 | Wojtek Lukaszuk [@wojteklu](http://twitter.com/wojteklu) 75 | 76 | ## License 77 | 78 | Available under the MIT license. See the LICENSE file for more info. 79 | -------------------------------------------------------------------------------- /iOS/Example/Example.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 2EE45A961F3838330066F889 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EE45A951F3838330066F889 /* AppDelegate.swift */; }; 11 | 2EE45A981F3838330066F889 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EE45A971F3838330066F889 /* ViewController.swift */; }; 12 | 2EE45A9B1F3838330066F889 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2EE45A991F3838330066F889 /* Main.storyboard */; }; 13 | 2EE45A9D1F3838330066F889 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2EE45A9C1F3838330066F889 /* Assets.xcassets */; }; 14 | 2EE45AA01F3838330066F889 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2EE45A9E1F3838330066F889 /* LaunchScreen.storyboard */; }; 15 | 2EE45ABA1F383B310066F889 /* libresolv.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 2EE45AB91F383B310066F889 /* libresolv.tbd */; }; 16 | 2EE45AC01F383C7E0066F889 /* libmessage.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 2EE45ABF1F383C7E0066F889 /* libmessage.a */; }; 17 | 2EE45AC31F383D510066F889 /* Message.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EE45AC21F383D510066F889 /* Message.swift */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXFileReference section */ 21 | 2EE45A921F3838330066F889 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; }; 22 | 2EE45A951F3838330066F889 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 23 | 2EE45A971F3838330066F889 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 24 | 2EE45A9A1F3838330066F889 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 25 | 2EE45A9C1F3838330066F889 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 26 | 2EE45A9F1F3838330066F889 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 27 | 2EE45AA11F3838330066F889 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 28 | 2EE45AA91F3838540066F889 /* message.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = message.h; sourceTree = ""; }; 29 | 2EE45AAA1F3838910066F889 /* Example-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Example-Bridging-Header.h"; sourceTree = ""; }; 30 | 2EE45AB91F383B310066F889 /* libresolv.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libresolv.tbd; path = usr/lib/libresolv.tbd; sourceTree = SDKROOT; }; 31 | 2EE45ABF1F383C7E0066F889 /* libmessage.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libmessage.a; sourceTree = ""; }; 32 | 2EE45AC21F383D510066F889 /* Message.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Message.swift; sourceTree = ""; }; 33 | /* End PBXFileReference section */ 34 | 35 | /* Begin PBXFrameworksBuildPhase section */ 36 | 2EE45A8F1F3838330066F889 /* Frameworks */ = { 37 | isa = PBXFrameworksBuildPhase; 38 | buildActionMask = 2147483647; 39 | files = ( 40 | 2EE45ABA1F383B310066F889 /* libresolv.tbd in Frameworks */, 41 | 2EE45AC01F383C7E0066F889 /* libmessage.a in Frameworks */, 42 | ); 43 | runOnlyForDeploymentPostprocessing = 0; 44 | }; 45 | /* End PBXFrameworksBuildPhase section */ 46 | 47 | /* Begin PBXGroup section */ 48 | 2EE45A891F3838330066F889 = { 49 | isa = PBXGroup; 50 | children = ( 51 | 2EE45A941F3838330066F889 /* Example */, 52 | 2EE45A931F3838330066F889 /* Products */, 53 | 2EE45AB81F383B310066F889 /* Frameworks */, 54 | ); 55 | sourceTree = ""; 56 | }; 57 | 2EE45A931F3838330066F889 /* Products */ = { 58 | isa = PBXGroup; 59 | children = ( 60 | 2EE45A921F3838330066F889 /* Example.app */, 61 | ); 62 | name = Products; 63 | sourceTree = ""; 64 | }; 65 | 2EE45A941F3838330066F889 /* Example */ = { 66 | isa = PBXGroup; 67 | children = ( 68 | 2EE45ABF1F383C7E0066F889 /* libmessage.a */, 69 | 2EE45AA91F3838540066F889 /* message.h */, 70 | 2EE45A951F3838330066F889 /* AppDelegate.swift */, 71 | 2EE45AC21F383D510066F889 /* Message.swift */, 72 | 2EE45AAA1F3838910066F889 /* Example-Bridging-Header.h */, 73 | 2EE45AC11F383CD00066F889 /* Resources */, 74 | ); 75 | path = Example; 76 | sourceTree = ""; 77 | }; 78 | 2EE45AB81F383B310066F889 /* Frameworks */ = { 79 | isa = PBXGroup; 80 | children = ( 81 | 2EE45AB91F383B310066F889 /* libresolv.tbd */, 82 | ); 83 | name = Frameworks; 84 | sourceTree = ""; 85 | }; 86 | 2EE45AC11F383CD00066F889 /* Resources */ = { 87 | isa = PBXGroup; 88 | children = ( 89 | 2EE45A971F3838330066F889 /* ViewController.swift */, 90 | 2EE45A9C1F3838330066F889 /* Assets.xcassets */, 91 | 2EE45AA11F3838330066F889 /* Info.plist */, 92 | 2EE45A9E1F3838330066F889 /* LaunchScreen.storyboard */, 93 | 2EE45A991F3838330066F889 /* Main.storyboard */, 94 | ); 95 | name = Resources; 96 | sourceTree = ""; 97 | }; 98 | /* End PBXGroup section */ 99 | 100 | /* Begin PBXNativeTarget section */ 101 | 2EE45A911F3838330066F889 /* Example */ = { 102 | isa = PBXNativeTarget; 103 | buildConfigurationList = 2EE45AA41F3838330066F889 /* Build configuration list for PBXNativeTarget "Example" */; 104 | buildPhases = ( 105 | 2EE45A8E1F3838330066F889 /* Sources */, 106 | 2EE45A8F1F3838330066F889 /* Frameworks */, 107 | 2EE45A901F3838330066F889 /* Resources */, 108 | ); 109 | buildRules = ( 110 | ); 111 | dependencies = ( 112 | ); 113 | name = Example; 114 | productName = Example; 115 | productReference = 2EE45A921F3838330066F889 /* Example.app */; 116 | productType = "com.apple.product-type.application"; 117 | }; 118 | /* End PBXNativeTarget section */ 119 | 120 | /* Begin PBXProject section */ 121 | 2EE45A8A1F3838330066F889 /* Project object */ = { 122 | isa = PBXProject; 123 | attributes = { 124 | LastSwiftUpdateCheck = 0830; 125 | LastUpgradeCheck = 1000; 126 | ORGANIZATIONNAME = Wojtek; 127 | TargetAttributes = { 128 | 2EE45A911F3838330066F889 = { 129 | CreatedOnToolsVersion = 8.3.3; 130 | LastSwiftMigration = 0830; 131 | ProvisioningStyle = Automatic; 132 | }; 133 | }; 134 | }; 135 | buildConfigurationList = 2EE45A8D1F3838330066F889 /* Build configuration list for PBXProject "Example" */; 136 | compatibilityVersion = "Xcode 3.2"; 137 | developmentRegion = en; 138 | hasScannedForEncodings = 0; 139 | knownRegions = ( 140 | en, 141 | Base, 142 | ); 143 | mainGroup = 2EE45A891F3838330066F889; 144 | productRefGroup = 2EE45A931F3838330066F889 /* Products */; 145 | projectDirPath = ""; 146 | projectRoot = ""; 147 | targets = ( 148 | 2EE45A911F3838330066F889 /* Example */, 149 | ); 150 | }; 151 | /* End PBXProject section */ 152 | 153 | /* Begin PBXResourcesBuildPhase section */ 154 | 2EE45A901F3838330066F889 /* Resources */ = { 155 | isa = PBXResourcesBuildPhase; 156 | buildActionMask = 2147483647; 157 | files = ( 158 | 2EE45AA01F3838330066F889 /* LaunchScreen.storyboard in Resources */, 159 | 2EE45A9D1F3838330066F889 /* Assets.xcassets in Resources */, 160 | 2EE45A9B1F3838330066F889 /* Main.storyboard in Resources */, 161 | ); 162 | runOnlyForDeploymentPostprocessing = 0; 163 | }; 164 | /* End PBXResourcesBuildPhase section */ 165 | 166 | /* Begin PBXSourcesBuildPhase section */ 167 | 2EE45A8E1F3838330066F889 /* Sources */ = { 168 | isa = PBXSourcesBuildPhase; 169 | buildActionMask = 2147483647; 170 | files = ( 171 | 2EE45AC31F383D510066F889 /* Message.swift in Sources */, 172 | 2EE45A981F3838330066F889 /* ViewController.swift in Sources */, 173 | 2EE45A961F3838330066F889 /* AppDelegate.swift in Sources */, 174 | ); 175 | runOnlyForDeploymentPostprocessing = 0; 176 | }; 177 | /* End PBXSourcesBuildPhase section */ 178 | 179 | /* Begin PBXVariantGroup section */ 180 | 2EE45A991F3838330066F889 /* Main.storyboard */ = { 181 | isa = PBXVariantGroup; 182 | children = ( 183 | 2EE45A9A1F3838330066F889 /* Base */, 184 | ); 185 | name = Main.storyboard; 186 | sourceTree = ""; 187 | }; 188 | 2EE45A9E1F3838330066F889 /* LaunchScreen.storyboard */ = { 189 | isa = PBXVariantGroup; 190 | children = ( 191 | 2EE45A9F1F3838330066F889 /* Base */, 192 | ); 193 | name = LaunchScreen.storyboard; 194 | sourceTree = ""; 195 | }; 196 | /* End PBXVariantGroup section */ 197 | 198 | /* Begin XCBuildConfiguration section */ 199 | 2EE45AA21F3838330066F889 /* Debug */ = { 200 | isa = XCBuildConfiguration; 201 | buildSettings = { 202 | ALWAYS_SEARCH_USER_PATHS = NO; 203 | CLANG_ANALYZER_NONNULL = YES; 204 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 205 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 206 | CLANG_CXX_LIBRARY = "libc++"; 207 | CLANG_ENABLE_MODULES = YES; 208 | CLANG_ENABLE_OBJC_ARC = YES; 209 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 210 | CLANG_WARN_BOOL_CONVERSION = YES; 211 | CLANG_WARN_COMMA = YES; 212 | CLANG_WARN_CONSTANT_CONVERSION = YES; 213 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 214 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 215 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 216 | CLANG_WARN_EMPTY_BODY = YES; 217 | CLANG_WARN_ENUM_CONVERSION = YES; 218 | CLANG_WARN_INFINITE_RECURSION = YES; 219 | CLANG_WARN_INT_CONVERSION = YES; 220 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 221 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 222 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 223 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 224 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 225 | CLANG_WARN_STRICT_PROTOTYPES = YES; 226 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 227 | CLANG_WARN_UNREACHABLE_CODE = YES; 228 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 229 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 230 | COPY_PHASE_STRIP = NO; 231 | DEBUG_INFORMATION_FORMAT = dwarf; 232 | ENABLE_STRICT_OBJC_MSGSEND = YES; 233 | ENABLE_TESTABILITY = YES; 234 | GCC_C_LANGUAGE_STANDARD = gnu99; 235 | GCC_DYNAMIC_NO_PIC = NO; 236 | GCC_NO_COMMON_BLOCKS = YES; 237 | GCC_OPTIMIZATION_LEVEL = 0; 238 | GCC_PREPROCESSOR_DEFINITIONS = ( 239 | "DEBUG=1", 240 | "$(inherited)", 241 | ); 242 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 243 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 244 | GCC_WARN_UNDECLARED_SELECTOR = YES; 245 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 246 | GCC_WARN_UNUSED_FUNCTION = YES; 247 | GCC_WARN_UNUSED_VARIABLE = YES; 248 | IPHONEOS_DEPLOYMENT_TARGET = 10.3; 249 | MTL_ENABLE_DEBUG_INFO = YES; 250 | ONLY_ACTIVE_ARCH = YES; 251 | SDKROOT = iphoneos; 252 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 253 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 254 | TARGETED_DEVICE_FAMILY = "1,2"; 255 | }; 256 | name = Debug; 257 | }; 258 | 2EE45AA31F3838330066F889 /* Release */ = { 259 | isa = XCBuildConfiguration; 260 | buildSettings = { 261 | ALWAYS_SEARCH_USER_PATHS = NO; 262 | CLANG_ANALYZER_NONNULL = YES; 263 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 264 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 265 | CLANG_CXX_LIBRARY = "libc++"; 266 | CLANG_ENABLE_MODULES = YES; 267 | CLANG_ENABLE_OBJC_ARC = YES; 268 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 269 | CLANG_WARN_BOOL_CONVERSION = YES; 270 | CLANG_WARN_COMMA = YES; 271 | CLANG_WARN_CONSTANT_CONVERSION = YES; 272 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 273 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 274 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 275 | CLANG_WARN_EMPTY_BODY = YES; 276 | CLANG_WARN_ENUM_CONVERSION = YES; 277 | CLANG_WARN_INFINITE_RECURSION = YES; 278 | CLANG_WARN_INT_CONVERSION = YES; 279 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 280 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 281 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 282 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 283 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 284 | CLANG_WARN_STRICT_PROTOTYPES = YES; 285 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 286 | CLANG_WARN_UNREACHABLE_CODE = YES; 287 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 288 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 289 | COPY_PHASE_STRIP = NO; 290 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 291 | ENABLE_NS_ASSERTIONS = NO; 292 | ENABLE_STRICT_OBJC_MSGSEND = YES; 293 | GCC_C_LANGUAGE_STANDARD = gnu99; 294 | GCC_NO_COMMON_BLOCKS = YES; 295 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 296 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 297 | GCC_WARN_UNDECLARED_SELECTOR = YES; 298 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 299 | GCC_WARN_UNUSED_FUNCTION = YES; 300 | GCC_WARN_UNUSED_VARIABLE = YES; 301 | IPHONEOS_DEPLOYMENT_TARGET = 10.3; 302 | MTL_ENABLE_DEBUG_INFO = NO; 303 | SDKROOT = iphoneos; 304 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 305 | TARGETED_DEVICE_FAMILY = "1,2"; 306 | VALIDATE_PRODUCT = YES; 307 | }; 308 | name = Release; 309 | }; 310 | 2EE45AA51F3838330066F889 /* Debug */ = { 311 | isa = XCBuildConfiguration; 312 | buildSettings = { 313 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 314 | CLANG_ENABLE_MODULES = YES; 315 | INFOPLIST_FILE = Example/Info.plist; 316 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 317 | LIBRARY_SEARCH_PATHS = ( 318 | "$(inherited)", 319 | "$(PROJECT_DIR)/Example", 320 | ); 321 | PRODUCT_BUNDLE_IDENTIFIER = com.rust.Example; 322 | PRODUCT_NAME = "$(TARGET_NAME)"; 323 | SWIFT_OBJC_BRIDGING_HEADER = "Example/Example-Bridging-Header.h"; 324 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 325 | SWIFT_VERSION = 5.0; 326 | }; 327 | name = Debug; 328 | }; 329 | 2EE45AA61F3838330066F889 /* Release */ = { 330 | isa = XCBuildConfiguration; 331 | buildSettings = { 332 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 333 | CLANG_ENABLE_MODULES = YES; 334 | INFOPLIST_FILE = Example/Info.plist; 335 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 336 | LIBRARY_SEARCH_PATHS = ( 337 | "$(inherited)", 338 | "$(PROJECT_DIR)/Example", 339 | ); 340 | PRODUCT_BUNDLE_IDENTIFIER = com.rust.Example; 341 | PRODUCT_NAME = "$(TARGET_NAME)"; 342 | SWIFT_OBJC_BRIDGING_HEADER = "Example/Example-Bridging-Header.h"; 343 | SWIFT_VERSION = 5.0; 344 | }; 345 | name = Release; 346 | }; 347 | /* End XCBuildConfiguration section */ 348 | 349 | /* Begin XCConfigurationList section */ 350 | 2EE45A8D1F3838330066F889 /* Build configuration list for PBXProject "Example" */ = { 351 | isa = XCConfigurationList; 352 | buildConfigurations = ( 353 | 2EE45AA21F3838330066F889 /* Debug */, 354 | 2EE45AA31F3838330066F889 /* Release */, 355 | ); 356 | defaultConfigurationIsVisible = 0; 357 | defaultConfigurationName = Release; 358 | }; 359 | 2EE45AA41F3838330066F889 /* Build configuration list for PBXNativeTarget "Example" */ = { 360 | isa = XCConfigurationList; 361 | buildConfigurations = ( 362 | 2EE45AA51F3838330066F889 /* Debug */, 363 | 2EE45AA61F3838330066F889 /* Release */, 364 | ); 365 | defaultConfigurationIsVisible = 0; 366 | defaultConfigurationName = Release; 367 | }; 368 | /* End XCConfigurationList section */ 369 | }; 370 | rootObject = 2EE45A8A1F3838330066F889 /* Project object */; 371 | } 372 | -------------------------------------------------------------------------------- /iOS/Example/Example.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /iOS/Example/Example/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | @UIApplicationMain 4 | class AppDelegate: UIResponder, UIApplicationDelegate { 5 | var window: UIWindow? 6 | 7 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { 8 | 9 | let message = Message(text: "some text") 10 | print(message.name) 11 | 12 | return true 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /iOS/Example/Example/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /iOS/Example/Example/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /iOS/Example/Example/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /iOS/Example/Example/Example-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | // 2 | // Use this file to import your target's public headers that you would like to expose to Swift. 3 | // 4 | 5 | #import "message.h" 6 | -------------------------------------------------------------------------------- /iOS/Example/Example/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /iOS/Example/Example/Message.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | class Message { 4 | private let raw: OpaquePointer 5 | 6 | init(text: String) { 7 | raw = message_new(text) 8 | } 9 | 10 | deinit { 11 | message_destroy(raw) 12 | } 13 | 14 | var name: String { 15 | let byteSlice = message_get_text(raw) 16 | return byteSlice.toString()! 17 | } 18 | } 19 | 20 | extension ByteSlice { 21 | 22 | func toString(encoding: String.Encoding = String.Encoding.utf8) -> String? { 23 | let pointer = UnsafeBufferPointer(start: bytes, count: length) 24 | return String(bytes: pointer, encoding: encoding) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /iOS/Example/Example/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // Example 4 | // 5 | // Created by Wojtek on 07/08/2017. 6 | // Copyright © 2017 Wojtek. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | override func viewDidLoad() { 14 | super.viewDidLoad() 15 | // Do any additional setup after loading the view, typically from a nib. 16 | } 17 | 18 | override func didReceiveMemoryWarning() { 19 | super.didReceiveMemoryWarning() 20 | // Dispose of any resources that can be recreated. 21 | } 22 | 23 | 24 | } 25 | 26 | -------------------------------------------------------------------------------- /iOS/Example/Example/libmessage.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wojteklu/rust-to-ios/4e1cfc7c63eda21a9af2f6e4aac0a64e9b2cfb53/iOS/Example/Example/libmessage.a -------------------------------------------------------------------------------- /iOS/Example/Example/message.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | struct ByteSlice { 5 | const uint8_t *bytes; 6 | size_t length; 7 | }; 8 | 9 | struct message; 10 | 11 | struct message *message_new(const char *text); 12 | void message_destroy(struct message *message); 13 | struct ByteSlice message_get_text(const struct message *message); 14 | -------------------------------------------------------------------------------- /rust/message/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /rust/message/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "message" 3 | version = "0.1.0" 4 | authors = ["Wojtek Lukaszuk "] 5 | 6 | [lib] 7 | name = "message" 8 | crate-type = ["staticlib"] 9 | 10 | [dependencies] 11 | libc = "0.1" -------------------------------------------------------------------------------- /rust/message/message.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | struct ByteSlice { 5 | const uint8_t *bytes; 6 | size_t length; 7 | }; 8 | 9 | struct message; 10 | 11 | struct message *message_new(const char *text); 12 | void message_destroy(struct message *message); 13 | struct ByteSlice message_get_text(const struct message *message); 14 | -------------------------------------------------------------------------------- /rust/message/src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate libc; 2 | 3 | pub mod message; 4 | pub mod string; -------------------------------------------------------------------------------- /rust/message/src/message.rs: -------------------------------------------------------------------------------- 1 | use ::libc::c_char; 2 | use ::string::FromChar; 3 | use ::string::ByteSlice; 4 | 5 | pub struct Message { 6 | text: String, 7 | } 8 | 9 | #[no_mangle] 10 | pub extern fn message_new(string: *const c_char) -> *mut Message { 11 | let message = Message{ 12 | text: String::from_char(string).clone(), 13 | }; 14 | 15 | let boxed_data = Box::new(message); 16 | Box::into_raw(boxed_data) 17 | } 18 | 19 | #[no_mangle] 20 | pub unsafe extern fn message_destroy(data: *mut Message) { 21 | let _ = Box::from_raw(data); 22 | } 23 | 24 | #[no_mangle] 25 | pub unsafe extern fn message_get_text(message: *const Message) -> ByteSlice { 26 | let message = &*message; 27 | ByteSlice::from(message.text.as_ref()) 28 | } 29 | -------------------------------------------------------------------------------- /rust/message/src/string.rs: -------------------------------------------------------------------------------- 1 | use std::ffi::CStr; 2 | use libc::{c_char, size_t}; 3 | use std::string::String; 4 | 5 | pub trait FromChar { 6 | fn from_char(char: *const c_char) -> String; 7 | } 8 | 9 | impl FromChar for String { 10 | fn from_char(char: *const c_char) -> String { 11 | let c_str: &CStr = unsafe { CStr::from_ptr(char) }; 12 | let byte_slice = c_str.to_bytes().to_vec(); 13 | 14 | match String::from_utf8(byte_slice) { 15 | Ok(s) => return s, 16 | Err(_) => return String::from(""), 17 | } 18 | } 19 | } 20 | 21 | #[repr(C)] 22 | pub struct ByteSlice { 23 | pub bytes: *const u8, 24 | pub length: size_t, 25 | } 26 | 27 | impl<'a> From<&'a str> for ByteSlice { 28 | fn from(s: &'a str) -> Self { 29 | ByteSlice{ 30 | bytes: s.as_ptr(), 31 | length: s.len() as size_t, 32 | } 33 | } 34 | } 35 | --------------------------------------------------------------------------------