mirror of
https://github.com/FuelLabs/sway.git
synced 2025-08-10 05:39:05 +00:00
Standardized module structure (#4232)
## Description Implement RFC 0006, fix #4191. Remove the `dep` reseved keyword in favor of `mod`. Change path resolution for submodules to use an adjascent file at the root and a folder named after the current module in other cases. Remove the need for an argument to `library`, the LSP now points to empty spans at the top of module files. The library names are now determined by the file name, or the package name at the top level. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
This commit is contained in:
parent
0848a887b7
commit
0eaa3a6da9
430 changed files with 994 additions and 1171 deletions
|
@ -1,4 +1,4 @@
|
|||
library fizzbuzz;
|
||||
library;
|
||||
|
||||
// ANCHOR: state
|
||||
enum State {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library arrays;
|
||||
library;
|
||||
|
||||
// ANCHOR: syntax
|
||||
fn syntax() {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library booleans;
|
||||
library;
|
||||
|
||||
// ANCHOR: syntax
|
||||
fn returns_true() -> bool {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library bytes;
|
||||
library;
|
||||
|
||||
fn syntax() {
|
||||
// ANCHOR: syntax
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library basic_enum;
|
||||
library;
|
||||
|
||||
// ANCHOR: definition
|
||||
enum Color {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library enum_of_enums;
|
||||
library;
|
||||
|
||||
// ANCHOR: content
|
||||
enum UserError {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library enum_of_structs;
|
||||
library;
|
||||
|
||||
// ANCHOR: content
|
||||
struct Item {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library enums;
|
||||
library;
|
||||
|
||||
dep basic_enum;
|
||||
dep enum_of_structs;
|
||||
mod basic_enum;
|
||||
mod enum_of_structs;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library numerics;
|
||||
library;
|
||||
|
||||
fn syntax() {
|
||||
// ANCHOR: syntax
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library strings;
|
||||
library;
|
||||
|
||||
fn explicit() {
|
||||
// ANCHOR: explicit
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library structs;
|
||||
library;
|
||||
|
||||
// ANCHOR: definition
|
||||
struct Foo {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library tuples;
|
||||
library;
|
||||
|
||||
fn syntax() {
|
||||
// ANCHOR: declare
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library comments;
|
||||
library;
|
||||
|
||||
fn comment() {
|
||||
// ANCHOR: comment
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library control_flow;
|
||||
library;
|
||||
|
||||
fn conditional() {
|
||||
// ANCHOR: conditional
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library explicit;
|
||||
library;
|
||||
|
||||
// ANCHOR: main
|
||||
fn main() -> bool {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library implicit;
|
||||
library;
|
||||
|
||||
// ANCHOR: main
|
||||
fn main() -> bool {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
library functions;
|
||||
library;
|
||||
|
||||
dep explicit;
|
||||
dep implicit;
|
||||
mod explicit;
|
||||
mod implicit;
|
||||
|
||||
// ANCHOR: definition
|
||||
fn my_function(my_parameter: u64 /* ... */) -> u64 {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library interface;
|
||||
library;
|
||||
|
||||
abi Wallet {
|
||||
/// When the BASE_ASSET is sent to this function the internal contract balance is incremented
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library my_library;
|
||||
library;
|
||||
|
||||
use my_other_library::quix;
|
||||
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
library my_other_library;
|
||||
library;
|
||||
|
||||
pub fn quix() {}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
library lib;
|
||||
library;
|
||||
|
||||
dep my_library;
|
||||
mod my_library;
|
||||
|
||||
use my_library::bar;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// ANCHOR: library
|
||||
// ANCHOR: module
|
||||
library my_library;
|
||||
library;
|
||||
// ANCHOR_END: module
|
||||
|
||||
// Cannot import because the `pub` keyword is missing
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library annotations;
|
||||
library;
|
||||
|
||||
// ANCHOR: type_annotation
|
||||
fn execute() {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library enums;
|
||||
library;
|
||||
|
||||
pub enum Error {
|
||||
StateError: StateError,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library getters;
|
||||
library;
|
||||
|
||||
// ANCHOR: avoid
|
||||
fn get_maximum_deposit() -> u64 {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// ANCHOR: module
|
||||
library letter_casing;
|
||||
library;
|
||||
// ANCHOR_END: module
|
||||
// ANCHOR: const
|
||||
const MAXIMUM_DEPOSIT = 10;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library struct_shorthand;
|
||||
library;
|
||||
|
||||
// ANCHOR: struct_shorthand_definition
|
||||
struct Structure {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library variables;
|
||||
library;
|
||||
|
||||
fn mutable() {
|
||||
// ANCHOR: mutable
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library enums;
|
||||
library;
|
||||
|
||||
// ANCHOR: u64_example
|
||||
pub enum T {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library arrays;
|
||||
library;
|
||||
|
||||
// ANCHOR: syntax
|
||||
fn syntax() {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library strings;
|
||||
library;
|
||||
|
||||
fn single_quotes() {
|
||||
// ANCHOR: single_quotes
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
library assertions;
|
||||
library;
|
||||
|
||||
dep req;
|
||||
mod req;
|
||||
|
||||
// ANCHOR: assert
|
||||
fn subtract(a: u64, b: u64) -> u64 {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library req;
|
||||
library;
|
||||
|
||||
// ANCHOR: require
|
||||
fn subtract(a: u64, b: u64) -> u64 {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library asset_operations;
|
||||
library;
|
||||
|
||||
// ANCHOR: mint_import
|
||||
use std::token::mint;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library call_data;
|
||||
library;
|
||||
|
||||
// ANCHOR: import_sender
|
||||
use std::auth::msg_sender;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library interface;
|
||||
library;
|
||||
|
||||
abi Vault {
|
||||
fn deposit();
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library hashing;
|
||||
library;
|
||||
|
||||
// ANCHOR: import_sha256
|
||||
use std::hash::sha256;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library logging;
|
||||
library;
|
||||
|
||||
// ANCHOR: logging
|
||||
fn log_data(number: u64) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library namespace;
|
||||
library;
|
||||
|
||||
// ANCHOR: address
|
||||
pub struct Address {
|
||||
|
|
|
@ -4,7 +4,7 @@ A library is used to contain code that performs common operations in order to pr
|
|||
|
||||
## Definition
|
||||
|
||||
Libraries are defined using the `library` keyword at the beginning of a file followed by a name so that they can be identified and imported.
|
||||
Libraries are defined using the `library` keyword at the beginning of a file.
|
||||
|
||||
```sway
|
||||
{{#include ../../../../code/language/program-types/libraries/internal/my_library/src/my_library.sw:module}}
|
||||
|
|
|
@ -14,7 +14,7 @@ $ tree
|
|||
|
||||
To be able to use our library `my_library.sw` in `lib.sw` there are two steps to take:
|
||||
|
||||
1. Bring our library into scope by using the `dep` keyword followed by the library name
|
||||
1. Bring our library into scope by using the `mod` keyword followed by the library name
|
||||
2. Use the `use` keyword to selectively import various items from the library
|
||||
|
||||
```sway
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue