Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9decf4c78a | ||
|
|
e2463a5589 | ||
|
|
a825f5a0c8 | ||
|
|
3c0d26cb74 | ||
|
|
f7a31ac804 | ||
|
|
31aebcdc59 | ||
|
|
afbac81918 | ||
|
|
0b78f86f08 | ||
|
|
7180b98f47 | ||
|
|
021cca436d | ||
|
|
a3d88dc406 | ||
|
|
58a6291f17 | ||
|
|
81884247d7 | ||
|
|
c07a34f9a8 | ||
|
|
e05bfc78cb | ||
|
|
cb430702c9 | ||
|
|
6aaa4bb06d | ||
|
|
a6b80a19c5 | ||
|
|
8be0ad0499 | ||
|
|
8da4936ac3 | ||
|
|
bcc92ccc40 | ||
|
|
d64d4b009c |
@@ -2,11 +2,6 @@ language: erlang
|
||||
|
||||
otp_release:
|
||||
- 18.0
|
||||
- 18.1
|
||||
- 18.2
|
||||
- 18.3
|
||||
- 19.0
|
||||
- 19.1
|
||||
|
||||
sudo: false
|
||||
|
||||
|
||||
+163
-230
@@ -1,292 +1,225 @@
|
||||
# Changelog for Elixir v1.4
|
||||
# Changelog for Elixir v1.2
|
||||
|
||||
Elixir v1.4 brings new features, enhancements and bug fixes into Elixir. The most notable changes are the addition of the `Registry` module and the `Task.async_stream/3` and `Task.async_stream/5` which aid developers in writing concurrent software. Those two features and a couple other improvements are described in detail below followed by the complete list of changes.
|
||||
v1.2 brings enhancements, bug fixes, performance improvements and more
|
||||
into Elixir. Elixir v1.2 relies on many features in Erlang 18, requiring
|
||||
at least Erlang 18+. Upgrading to Erlang 18 is therefore necessary before
|
||||
upgrading Elixir.
|
||||
|
||||
## Registry
|
||||
## Erlang 18 support
|
||||
|
||||
The registry is a local, decentralized and scalable key-value process storage:
|
||||
We have brought many features specific to Erlang 18. Here are the highlights:
|
||||
|
||||
* Local because keys and values are only accessible to the current node (opposite to distributed)
|
||||
* Decentralized because there is no single entity responsible for managing the registry
|
||||
* Scalable because performance scales linearly with the addition of more cores upon partitioning
|
||||
* Maps can now scale from dozens to millions of keys. Therefore, usage of
|
||||
the modules `Dict` and `HashDict` is now discouraged and will be
|
||||
deprecated in future releases, instead use `Map`. Similarly, `Set` and
|
||||
`HashSet` will be deprecated in favor of `MapSet`
|
||||
* Compilation times are faster due to improvements in both the Elixir and
|
||||
Erlang compilers
|
||||
* Dialyzer now emits less false negative warnings thanks to new annotations
|
||||
available in the Erlang compiler
|
||||
|
||||
A registry is chosen upon start to have unique or duplicate keys. Every key-value pair is associated to the process registering the key. Keys are automatically removed once the owner process terminates.
|
||||
## Language improvements
|
||||
|
||||
iex> Registry.start_link(:unique, MyRegistry)
|
||||
iex> {:ok, _} = Registry.register(MyRegistry, "hello", 1)
|
||||
iex> Registry.lookup(MyRegistry, "hello")
|
||||
[{self(), 1}]
|
||||
This release includes four notable language improvements:
|
||||
|
||||
With the registry, developers can provide dynamic process names, module-function dispatch or even a local pubsub system. See the `Registry` documentation for more information.
|
||||
* The addition of multi aliases/imports/require:
|
||||
|
||||
## Syntax coloring
|
||||
alias MyApp.{Foo, Bar, Baz}
|
||||
|
||||
Elixir v1.4 introduces the ability to syntax color inspected data structures:
|
||||
* Support for variables in map keys:
|
||||
|
||||
iex> IO.puts inspect([hello: 1, world: "!"], syntax_colors: [atom: :cyan])
|
||||
[hello: 1, world: "!"]
|
||||
%{key => value}
|
||||
|
||||
Coloring is done with ANSI colors as specified in the `IO.ANSI` module.
|
||||
* Support for the pin operator in map keys and function clauses:
|
||||
|
||||
IEx automatically relies on this feature to provide syntax coloring for evaluated shell results. This behaviour can be configured via the `:syntax_colors` coloring option:
|
||||
%{^key => value} = %{key => value}
|
||||
fn ^key -> :ok end
|
||||
|
||||
IEx.configure [colors: [syntax_colors: [atom: :cyan, string: :green]]]
|
||||
* Addition of the `with` special form to match on multiple expressions:
|
||||
|
||||
To disable coloring altogether, pass an empty list to `:syntax_colors`.
|
||||
with {:ok, contents} <- File.read("my_file.ex"),
|
||||
{res, binding} <- Code.eval_string(contents),
|
||||
do: {:ok, res}
|
||||
|
||||
## Calendar
|
||||
These improvements aim to make the language more consistent and expressive.
|
||||
|
||||
Elixir v1.3 introduced new calendar types. This release continues evolving the Calendar APIs by adding functions for comparing, adding and calculating the difference between types, retrieve the `day_of_week/1`, check if the current date is a `leap_year?/1` and more.
|
||||
## Getting started experience
|
||||
|
||||
## Task.async_stream
|
||||
While we were improving the language, we also improved both the parser and
|
||||
compiler to be even more aware of language constructs, emitting warnings
|
||||
on common pitfalls like when piping to expressions without parentheses or
|
||||
when defining unsafe variables.
|
||||
|
||||
When there is a need to traverse a collection of items concurrently, Elixir developers often resort to tasks:
|
||||
We have also introduced the `i/1` helper in IEx, which allows developers
|
||||
to retrieve information about any data type. This will help newcomers
|
||||
explore the language values while providing experienced developers with
|
||||
crucial information about the value they are introspecting.
|
||||
|
||||
collection
|
||||
|> Enum.map(&Task.async(SomeMod, :function, [&1]))
|
||||
|> Enum.map(&Task.await/1)
|
||||
## Workflow improvements
|
||||
|
||||
While the snippet above works fine in many occasions, for large collections it will spawn and run concurrently as many tasks as there are items in the collection.
|
||||
Umbrella applications are now able to share both build and configuration files.
|
||||
This aims to drastically reduce compilation times in umbrella projects by
|
||||
adding the following configuration to each umbrella app's `mix.exs` file:
|
||||
|
||||
`Task.async_stream/3` and `Task.async_stream/5` allows developers to process collections concurrently while controlling the maximum amount of concurrent tasks:
|
||||
build_path: "../../_build",
|
||||
config_path: "../../config/config.exs",
|
||||
|
||||
collection
|
||||
|> Task.async_stream(SomeMod, :function, [], max_concurrency: System.schedulers_online)
|
||||
Finally, Mix will now consolidate protocols by default as we are now able to
|
||||
consolidate in parallel and cache the consolidation results, providing the
|
||||
best performance across all environments without affecting compilation times.
|
||||
The only downside of this change is that, if you have been implementing
|
||||
protocols exclusively as part of your test suite, inside the `test` directory,
|
||||
those won't be picked up as it happens after compilation. For such cases,
|
||||
consolidation can be disabled by setting `consolidate_protocols: false` in
|
||||
the project config.
|
||||
|
||||
The `Task.async_stream` functions are also lazy, allowing developers to partially consume the stream until a condition is reached. Furthermore, `Task.Supervisor.async_stream/4` and `Task.Supervisor.async_stream/6` can be used to ensure the concurrent tasks are spawned under a given supervisor.
|
||||
These are great additions on top of the faster compilation times we have
|
||||
achieved when migrating to Erlang 18.
|
||||
|
||||
## Application inference
|
||||
## Rebar 3 support
|
||||
|
||||
Mix v1.4 now automatically infers the list of applications that are required on runtime from your dependencies list.
|
||||
With Rebar 3 gaining more adoption in the Erlang community, Mix is
|
||||
now able to fetch and compile Rebar 3 dependencies. This feature is currently
|
||||
experimental and therefore opt-in: if you have a Rebar 3 dependency, you can
|
||||
ask Mix to use Rebar 3 to compile it by passing the `manager: :rebar3` option.
|
||||
Once configured, Mix will prompt you to install Rebar 3 if it is not yet
|
||||
available.
|
||||
|
||||
In previous Mix versions, most of your dependencies had to be added both to your dependencies list and applications list. Here is how a `mix.exs` would look like:
|
||||
## v1.2.2
|
||||
|
||||
def application do
|
||||
[applications: [:logger, :plug, :postgrex]]
|
||||
end
|
||||
### 1. Enhancements
|
||||
|
||||
def deps do
|
||||
[{:plug, "~> 1.2"},
|
||||
{:postgrex, "~> 1.0"}]
|
||||
end
|
||||
* [Kernel] Support `@compile {:autoload, false}` to disable automatic loading after compilation
|
||||
|
||||
This was error prone as many developers would not list their dependencies in their applications list.
|
||||
### 2. Bug fixes
|
||||
|
||||
Mix v1.4 now automatically infers your applications list as long as you leave the `:applications` key empty. The `mix.exs` above can be rewritten to:
|
||||
* [ExUnit] Raise if trying to override reserved tag in `setup` blocks
|
||||
* [Mix] Ensure retrieve compile manifests do fail if some compilers are not yet available
|
||||
* [Mix] Automatically merge managers according to the mix > rebar3 > rebar > make order
|
||||
* [Mix] Force recompilation if dependency was recently fetched
|
||||
|
||||
def application do
|
||||
[extra_applications: [:logger]]
|
||||
end
|
||||
## v1.2.1 (2016-01-14)
|
||||
|
||||
def deps do
|
||||
[{:plug, "~> 1.2"},
|
||||
{:postgrex, "~> 1.0"}]
|
||||
end
|
||||
### 1. Enhancements
|
||||
|
||||
With the above, Mix will automatically build your application list based on your dependencies. Applications that are part of Erlang or Elixir that are required at runtime, such as `:logger`, must be added to the `:extra_applications` list. All extra applications will be included in the application list.
|
||||
* [IEx] Support remote pids/ports with IEx helper `i/1`
|
||||
* [Protocol] Warn when `defimpl` is called for a consolidated protocol
|
||||
|
||||
Finally, if there is a dependency you don't want to include in the application runtime list, you can do so by specifying the `runtime: false` option:
|
||||
### 2. Bug fixes
|
||||
|
||||
{:distillery, "> 0.0.0", runtime: false}
|
||||
* [ExUnit] Ensure `assert` macros can be used from quoted code
|
||||
* [ExUnit] Do not warn in match assertion if variable is reused in pattern
|
||||
* [Macro] Fix a bug in `Macro.to_string/1` where a remote function could be accidentally interpreted as a sigil
|
||||
* [Mix] Ensure dependencies are properly skipped when `--only` option is given to `mix deps.get`
|
||||
|
||||
We hope this feature provides a more streamlined workflow for developers who are building releases for their Elixir projects.
|
||||
|
||||
## Mix install from SCM
|
||||
|
||||
Mix v1.4 can now install escripts and archives from both Git and Hex, providing you with even more options for distributing Elixir code.
|
||||
|
||||
This makes it possible to distribute CLI applications written in Elixir by publishing a package which builds an escript to Hex. [`ex_doc`](https://hex.pm/packages/ex_doc) has been updated to serve as an example of how to use this new functionality.
|
||||
|
||||
Simply running:
|
||||
|
||||
mix escript.install hex ex_doc
|
||||
|
||||
will fetch `ex_doc` and its dependencies, build them, and then install `ex_doc` to `~/.mix/escripts` (by default). After adding `~/.mix/escripts` to your `PATH`, running `ex_doc` is as simple as:
|
||||
|
||||
ex_doc
|
||||
|
||||
You can now also install archives from Hex in this way. Since they are fetched and built on the user's machine, they do not have the same limitations as pre-built archives. However, keep in mind archives run alongside every Mix project, which may lead to conflicts. For this reason, escripts is the preferred format.
|
||||
|
||||
It is also possible to install escripts and archives by providing a Git/GitHub repo. See `mix help escript.install` and `mix help archive.install` for more details.
|
||||
|
||||
## v1.4.2 (2017-02-16)
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### EEx
|
||||
|
||||
* [EEx] Support middle expressions on trim mode
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Calendar] Correct typo on Calendar types
|
||||
* [Kernel] Ensure redefined functions point to the proper source
|
||||
* [OptionParser] Add `:allow_inexistent_atoms` to support unsafe behaviour prior to v1.4
|
||||
* [Stream] Allow consuming multiple items from suspended enumerable in `Stream.transform/3`
|
||||
* [String] Incorporate new grapheme rules in Unicode 9
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx.Autocomplete] Do not crash on aliases which are not known at compile time
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix.Umbrella] Ensure umbrella projects can depend on other umbrella projects
|
||||
* [Mix.Archive] Ensure previous archives with `.ez` extension are deleted
|
||||
|
||||
## v1.4.1 (2017-01-26)
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Kernel] Remove warning when making private functions overridable
|
||||
* [Path] Ensure `Path.join/1` returns strings for lists of one element
|
||||
* [Regex] Ensure `Regex.escape/1` also escapes `-`
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx] Disable ANSI detection for powershell to avoid false positives
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix.Make] Run `make clean` for `erlang.mk`
|
||||
* [Mix.Rebar] Support all of rebar3 dependency package declaration
|
||||
* [Mix.Rebar] Only pass overrides from parent to child in Rebar dep
|
||||
|
||||
## v1.4.0 (2017-01-05)
|
||||
## v1.2.0 (2016-01-01)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Calendar] Add `Date.compare/2`, `Time.compare/2`, `NaiveDateTime.compare/2` and `DateTime.compare/2`
|
||||
* [Calendar] Support `NaiveDateTime.add/3` and `NaiveDateTime.diff/3` for adding seconds (up to microseconds) as well as the difference between two NaiveDateTimes in seconds (up to microseconds)
|
||||
* [Calendar] Add `Date.leap_year?/1` and `Date.day_of_week/1`
|
||||
* [Calendar] Ensure `Date`, `Time` and `NaiveDateTime` APIs work with any struct that provides the same set of fields as their respective struct. For example, a `NaiveDateTime` can be given to `Date` since it contains a superset of the fields in the `Date` struct
|
||||
* [Calendar] Add `Time.utc_now/0` and `NaiveDateTime.utc_now/0`
|
||||
* [Enum] Add `Enum.map_every/2` that invokes the given function with every nth item
|
||||
* [Enum] Add `min/2`, `max/2`, `min_max/2`, `min_by/3`, `max_by/3`, and `min_max_by/3` that allow a function specifying the default value when the enumerable is empty
|
||||
* [Enum] Introduce `Enum.zip/1` to zip multiple entries at once
|
||||
* [Float] Introduce `Float.ratio/1` that returns a tuple with the numerator and denominator as integers to retrieve the given float
|
||||
* [GenServer] Log warn on default `handle_info/2` implementation
|
||||
* [Inspect] Support syntax coloring via the `:syntax_color` option
|
||||
* [Integer] `Integer.digits/2` now accepts negative integers
|
||||
* [Integer] Add `Integer.mod/2` and `Integer.floor_div/2`
|
||||
* [IO] Add `:label` option to `IO.inspect/2` to help distinguish multiple `IO.inspect/2` calls.
|
||||
* [Kernel] Recognize merge conflict markers in source and provide a readable error message
|
||||
* [Kernel] Warn on unused module attributes
|
||||
* [Kernel] Improve compiler message on unexpected end of line
|
||||
* [Kernel] Raise `BadBooleanError` when a non-boolean is given on the left-hand side of `and`/`or`
|
||||
* [List] Add `List.pop_at/3`
|
||||
* [List] Add `List.myers_difference/2`
|
||||
* [OptionParser] Expand multi-letter aliases in `OptionParser`
|
||||
* [Process] Add `Process.send_after/4`
|
||||
* [Process] Improve error messages on `Process.register/2` errors
|
||||
* [Registry] Add a local, decentralized and scalable key-value process storage
|
||||
* [Stream] Add `Stream.map_every/2` that invokes the given function with every nth item
|
||||
* [Stream] Introduce `Stream.zip/1` to lazily zip multiple entries at once
|
||||
* [String] Update to Unicode 9.0.0
|
||||
* [Task] Add `Task.async_stream/3` and `Task.async_stream/5` as well as the supervised versions `Task.Supervisor.async_stream/4` and `Task.Supervisor.async_stream/6`
|
||||
* [URI] Allow 0 as URI scheme default port
|
||||
* [Application] Add `spec/1` and `spec/2` to retrieve application specification
|
||||
* [Application] Add `get_application/1` to retrieve the application a given module belongs to
|
||||
* [Base] Optimize encode and decode operations about 10 times
|
||||
* [Enum] Use the faster and auto-seeding `:rand` instead of `:random` in `Enum.shuffle/1` and `Enum.random/1` and `Enum.take_random/2`
|
||||
* [Enum] Add `Enum.with_index/2`
|
||||
* [GenServer] Add `GenServer.stop/1` for shutting down servers reliably
|
||||
* [IO] Add `color` related functions to `IO.ANSI`
|
||||
* [Kernel] Support multiple aliases in `alias`, `import`, `require` and `use`. For example, `alias MyApp.{Foo, Bar, Baz}`
|
||||
* [Kernel] Add `struct!/2`. Similar to `struct/2` but raises on invalid keys
|
||||
* [Kernel] Warn if `@doc/@typedoc/@moduledoc` attributes are redefined
|
||||
* [Kernel] Warn if non-variables are used in `defdelegate/2` (as they have no effect)
|
||||
* [Kernel] Mark quoted expressions as generated, avoiding false positives on dialyzer
|
||||
* [Kernel] Allow variables as map keys on creation `%{key => value}` and on matches `%{^key => value}`
|
||||
* [Kernel] Allow the pin operator `^` in `fn` clauses and on the left side of `<-` in `for` comprehensions
|
||||
* [Kernel] Introduce `with` as a special form that allows matching on right side parameters
|
||||
* [Kernel] Warn when right hand side of `->` does not provide any expression
|
||||
* [Kernel] Warn if the Elixir was compiled with a different endianness than the one currently available at runtime
|
||||
* [Kernel] Warn if a variable is used after being defined exclusively in a nested context
|
||||
* [Kernel] Warn if piping into an expression without parentheses
|
||||
* [Macro] Add `Macro.traverse/4` that performs pre and post-walk at once
|
||||
* [Macro] Add `Macro.camelize/1` and `Macro.underscore/1`
|
||||
* [Process] Add `Process.get_keys/0`
|
||||
* [Stream] Add `Stream.with_index/2`
|
||||
* [String] Introduce `String.replace_{prefix,suffix,leading,trailing}/2`. The first two will replace only the first occurrence of the given match in string. The last two will replace all occurrences of the given match
|
||||
* [String] Support `String.normalize/2` and `String.equivalent?/2` that perform NFD and NFC normalization
|
||||
* [System] Add `System.time_offset`, `System.monotonic_time`, `System.system_time`, `System.convert_time_unit` and `System.unique_integer`
|
||||
* [System] Allow `System.cmd/3` to remove variables by specifying nil values
|
||||
* [Task] Add `Task.Supervisor.async_nolink/1/3` that spawns a supervised task without linking to the caller process
|
||||
* [Task] Introduce `Task.yield_many/2`
|
||||
* [Task] Raise an error when a task is queried from a non-owning process (instead of waiting forever)
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit.Diff] Use red or green background for whitespace-only diffs
|
||||
* [ExUnit.Doctest] Allow inspected structures with multiples lines and unicode characters in the doctest result
|
||||
* [ExUnit.Formatter] Replace lhs/rhs with left/right in the formatter for clarity
|
||||
* [ExUnit] Allow one test to raise multiple errors. The goal is to enable tools in the ecosystem to emit multiple failure reports from the same test
|
||||
* [ExUnit] Support `@tag report: [:foo, :bar]` which will include the values for tags `:foo` and `:bar` whenever a test fails
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx.Autocomplete] Stop appending a trailing dot when autocompleting modules in IEx
|
||||
* [IEx.Autocomplete] Support autocompletion for structs
|
||||
* [IEx.Autocomplete] Improve IEx autocomplete to support navigating map atom keys
|
||||
* [IEx.Helpers] `c/1` now compiles in memory by default to avoid common issue where `.beam` files remain at projects root directory
|
||||
* [IEx.Helpers] Add info about protocols in `i/1`
|
||||
* [IEx.Server] Support interrupting IEx evaluation through the Ctrl+G prompt
|
||||
* [IEx] Allow `IEX_WITH_WERL` to be set on Windows to always run on WERL mode
|
||||
* [IEx] Display type docs for `t(Module.type)` and `t(Module.type/arity)`
|
||||
* [IEx] Add `i/1` helper that prints information about any data type
|
||||
* [IEx] Show source code snippet whenever there is a request to pry a given process
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Add file to logger metadata
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix archive] Compress archive files built by `mix archive` as they are now unzipped during installation
|
||||
* [mix archive] Install from SCM
|
||||
* [mix compile] Automatically infer the list of applications for Mix projects
|
||||
* [mix cmd] Add the ability to specify one or more apps in `mix cmd`
|
||||
* [mix deps] Warn if there are non-applications in the `apps` directory for umbrella projects
|
||||
* [mix deps] Add warning for invalid paths on `mix deps.clean`
|
||||
* [mix deps] Add `Mix.Project.apps_paths` that returns the paths to children applications in umbrella projects
|
||||
* [mix deps] Add `MIX_REBAR` environment variable for overriding local rebar
|
||||
* [mix escript] Install from SCM
|
||||
* [mix new] Check directory existence in `mix new` and ask how to proceed if one exists
|
||||
* [mix new] Applications built with the `--sup` flag now have an individual module to work as application callback
|
||||
* [mix test] Add `--formatter` option to `mix test`
|
||||
* [mix xref] Provide "did you mean?" suggestions for `mix xref`
|
||||
* [Mix] Cache and always consolidate protocols
|
||||
* [Mix] Add `warn_test_pattern` to `mix test` that will warn on potentially misconfigured test files
|
||||
* [Mix] Introduce `MIX_QUIET` environment variable that configures the underlying Mix task to output only error messages
|
||||
* [Mix] Introduce `MIX_DEBUG` environment variable that prints information about the task being run
|
||||
* [Mix] Validate git options and warn on conflicting ref, branch or tags
|
||||
* [Mix] New umbrella applications will now share configuration and build files
|
||||
* [Mix] Add experimental support for Rebar 3
|
||||
* [Mix] Do not warn when an optional dependency has a conflicting `:only` option with another dependency
|
||||
* [Mix] Raise readable error message when parsertools is not available
|
||||
* [Mix] Add `--build` flag to `mix deps.clean DEP` to only remove artifacts from `_build`
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
#### Kernel
|
||||
|
||||
* [Access] Do not accept nils in `Access.key/1` and `Access.key/2` in favor of explicit default values (or Access.key!/1 if you expect the key to always be available)
|
||||
* [Float] Avoid multiple roundings in `Float.ceil/2`, `Float.floor/2` and `Float.round/2`
|
||||
* [Kernel] Don't crash in `macro_exported?/3` when dealing with Erlang modules
|
||||
* [Kernel] Ensure locals calls are rewritten when calling a local function or macro from inside a module
|
||||
* [Kernel] Annotate the context for variables as zero-arity funs in quotes
|
||||
* [Kernel.SpecialForms] Ensure comprehensions with guards and filters keep proper ordering,
|
||||
* [Kernel.SpecialForms] Produce meaningful warning when with's else clauses have no effect
|
||||
* [Macro] Wrap fn calls in parens in `Macro.to_string/2`
|
||||
* [Macro] Do not print aliases as keys inside keyword lists in `Macro.to_string/2`
|
||||
* [OptionParser] Support options in `OptionParser.to_argv/2` to ensure `:count` switches are correctly encoded
|
||||
* [Stream] Ensure `Stream.take/2` does not consume next element on `:suspend`
|
||||
* [String] Fix infinite recursion in `String.replace_leading/3` and `String.replace_trailing/3` when given an empty string
|
||||
* [Task] Fix `Task.shutdown/1,2` infinite block when task has no monitor
|
||||
* [Task] Ensure task cannot link after parents unlinks
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit] Fix a race condition in `assert_receive` where we would assert a message was not received but show it in the list of messages when the message is delivered right after the timeout value
|
||||
|
||||
### IEx
|
||||
|
||||
* [IEx.Helpers] Purge consolidated protocols before and after `recompile/0`
|
||||
|
||||
### Mix
|
||||
|
||||
* [Mix.Dep] Use `gmake` on FreeBSD instead of `make` when compiling make dependencies
|
||||
* [Mix.Project] Only copy files from source when they're newer than destination (for Windows machines)
|
||||
* [Mix.Task] Ensure non-recursive tasks inside umbrella are reenabled
|
||||
|
||||
### 3. Soft deprecations (no warnings emitted)
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Enum] `Enum.partition/2` has been deprecated in favor of `Enum.split_with/2`
|
||||
* [System] Deprecate plural time units in favor of singular ones to align with future Erlang releases
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit] Using GenEvent to implement ExUnit formatters is deprecated. Please use the new `GenServer` based formatters instead
|
||||
|
||||
### 4. Deprecations
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Access] `Access.key/1` is deprecated due to erratic behaviour for missing keys, please use Access.key/2 instead with proper default values
|
||||
* [Behaviour] The `Behaviour` module is deprecated. Callbacks may now be defined directly via the `@callback` attribute
|
||||
* [Enum] Deprecate `Enum.uniq/2` in favor of `Enum.uniq_by/2`
|
||||
* [Float] `Float.to_char_list/2` and `Float.to_string/2` are deprecated (use the :erlang functions if such conversions are desired)
|
||||
* [Kernel] Deprecate support for making private functions overridable. Overridable functions must always be public as they must be contracts
|
||||
* [Kernel] Warn if variable is used as a function call
|
||||
* [OptionParser] Deprecate aliases with multiple letters, such as `-abc`
|
||||
* [Set] Deprecate the `Set` module
|
||||
* [Stream] Deprecate `Stream.uniq/2` in favor of `Stream.uniq_by/2`
|
||||
* [Access] Improve error messages when using Access on non-valid key-value structures
|
||||
* [Kernel] Raise when conflicting `:only` and `:except` are given to import
|
||||
* [Kernel] Change `__ENV__.file` if `@file` is set for the given function
|
||||
* [Kernel] Make `Kernel.ParallelRequire` aware of `:warning_as_errors`
|
||||
* [Kernel] Improve error message for invalid `do`/`do:`
|
||||
* [Macro] Ensure `Macro.to_string/2` respects operator precedence when using the access operator
|
||||
* [Path] Do not crash when expanding paths that go beyond the root, for example, `Path.expand("/../..")`
|
||||
* [String] Ensure `UnicodeConversionError` does not contain invalid string in its error message
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx.Helpers] `import_file/2` is deprecated in favor of `import_file_if_available/1`
|
||||
* [IEx] Do not start apps on `recompile` helper if `--no-start` was given
|
||||
* [IEx] Avoid copying of data when evaluating every expression in IEx
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix.Utils] `underscore/1` and `camelize/1` are deprecated
|
||||
* [Mix] Always run non-recursive tasks at the umbrella root
|
||||
* [Mix] Ensure rebar projects work on directory names that contain non-latin characters
|
||||
* [Mix] Ignore directories inside `apps` in umbrellas that do not have a `mix.exs` file
|
||||
* [Mix] Ensure Mix can be used with path dependencies where the app name is different than the path basename
|
||||
* [Mix] Ensure dependencies won't crash when updating from a git repository to a hex repository and the git version did not respect SemVer
|
||||
* [Mix] Do not run remote converger if dependencies have diverged
|
||||
* [Mix] Ensure umbrella dependencies across all environments are loaded on parent deps.get/deps.update
|
||||
|
||||
## v1.3
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit] Include file and line in all compilation errors for doctests
|
||||
|
||||
### 3. Soft deprecations (no warnings emitted)
|
||||
|
||||
#### Kernel
|
||||
|
||||
* [Dict] `Dict` and `HashDict` are soft deprecated in favor of `Map`
|
||||
* [Keyword] `Keyword.size/1` is deprecated in favor of `length/1`
|
||||
* [Map] `Map.size/1` is deprecated in favor of `map_size/1`
|
||||
* [Set] `Set` and `HashSet` are soft deprecated in favor of `MapSet`
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] `Mix.Utils.camelize/1` and `Mix.Utils.underscore/1` are soft deprecated in favor of `Macro.camelize/1` and `Macro.underscore/1`
|
||||
|
||||
The CHANGELOG for v1.3 releases can be found [in the v1.3 branch](https://github.com/elixir-lang/elixir/blob/v1.3/CHANGELOG.md).
|
||||
|
||||
+14
-48
@@ -1,56 +1,22 @@
|
||||
# Code of Conduct
|
||||
# Contributor Code of Conduct
|
||||
|
||||
Contact: elixir-lang-conduct@googlegroups.com
|
||||
As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
||||
|
||||
## Why have a Code of Conduct?
|
||||
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality.
|
||||
|
||||
As contributors and maintainers of this project, we are committed to providing a friendly, safe and welcoming environment for all, regardless of age, disability, gender, nationality, race, religion, sexuality, or similar personal characteristic.
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
The goal of the Code of Conduct is to specify a baseline standard of behavior so that people with different social values and communication styles can talk about Elixir effectively, productively, and respectfully, even in face of disagreements. The Code of Conduct also provides a mechanism for resolving conflicts in the community when they arise.
|
||||
* The use of sexualized language or imagery
|
||||
* Personal attacks
|
||||
* Trolling or insulting/derogatory comments
|
||||
* Public or private harassment
|
||||
* Publishing other's private information, such as physical or electronic addresses, without explicit permission
|
||||
* Other unethical or unprofessional conduct.
|
||||
|
||||
## Our Values
|
||||
The Elixir Core Team has the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, we commit ourselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team.
|
||||
|
||||
These are the values Elixir developers should aspire to:
|
||||
This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community.
|
||||
|
||||
* Be friendly and welcoming
|
||||
* Be patient
|
||||
* Remember that people have varying communication styles and that not everyone is using their native language. (Meaning and tone can be lost in translation.)
|
||||
* Be thoughtful
|
||||
* Productive communication requires effort. Think about how your words will be interpreted.
|
||||
* Remember that sometimes it is best to refrain entirely from commenting.
|
||||
* Be respectful
|
||||
* In particular, respect differences of opinion. It is important that we resolve disagreements and differing views constructively.
|
||||
* Avoid destructive behavior
|
||||
* Derailing: stay on topic; if you want to talk about something else, start a new conversation.
|
||||
* Unconstructive criticism: don't merely decry the current state of affairs; offer (or at least solicit) suggestions as to how things may be improved.
|
||||
* Snarking (pithy, unproductive, sniping comments).
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by sending an e-mail to elixir-lang-conduct@googlegroups.com.
|
||||
|
||||
The following actions are explicitly forbidden:
|
||||
|
||||
* Insulting, demeaning, hateful, or threatening remarks.
|
||||
* Discrimination based on age, disability, gender, nationality, race, religion, sexuality, or similar personal characteristic.
|
||||
* Bullying or systematic harassment.
|
||||
* Unwelcome sexual advances.
|
||||
* Incitement to any of these.
|
||||
|
||||
## Where does the Code of Conduct apply?
|
||||
|
||||
If you participate in or contribute to the Elixir ecosystem in any way, you are encouraged to follow the Code of Conduct while doing so.
|
||||
|
||||
Explicit enforcement of the Code of Conduct applies to the official mediums operated by the Elixir project:
|
||||
|
||||
* The official GitHub projects and code reviews.
|
||||
* The official elixir-lang mailing lists.
|
||||
* The #elixir-lang IRC channel on Freenode.
|
||||
|
||||
Other Elixir activities (such as conferences, meetups, and other unofficial forums) are encouraged to adopt this Code of Conduct. Such groups must provide their own contact information.
|
||||
|
||||
Project maintainers may remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct.
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by emailing: elixir-lang-conduct@googlegroups.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. **All reports will be kept confidential**.
|
||||
|
||||
**The goal of the Code of Conduct is to resolve conflicts in the most harmonious way possible**. We hope that in most cases issues may be resolved through polite discussion and mutual agreement. Bannings and other forceful measures are to be employed only as a last resort. **Do not** post about the issue publicly or try to rally sentiment against a particular individual or group.
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
This document was based on the Code of Conduct from the Go project with parts derived from Django's Code of Conduct, Rust's Code of Conduct and the Contributor Covenant.
|
||||
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/)
|
||||
+275
@@ -0,0 +1,275 @@
|
||||
# Contributing to Elixir
|
||||
|
||||
Please take a moment to review this document in order to make the contribution
|
||||
process easy and effective for everyone involved!
|
||||
|
||||
Also make sure you read our [Code of Conduct](CODE_OF_CONDUCT.md) that
|
||||
outlines our commitment towards an open and welcoming environment.
|
||||
|
||||
## Using the issue tracker
|
||||
|
||||
Use the issues tracker for:
|
||||
|
||||
* [bug reports](#bug-reports)
|
||||
* [submitting pull requests](#pull-requests)
|
||||
|
||||
Please **do not** use the issue tracker for personal support requests nor feature requests. Support requests should be sent to:
|
||||
|
||||
* [the elixir-talk mailing list](https://groups.google.com/group/elixir-lang-talk)
|
||||
* [Stack Overflow](http://stackoverflow.com/questions/ask?tags=elixir)
|
||||
* **[#elixir-lang](irc://chat.freenode.net/elixir-lang)** IRC channel on [chat.freenode.net](http://www.freenode.net/)
|
||||
|
||||
Feature requests can be discussed on [the elixir-core mailing list](https://groups.google.com/group/elixir-lang-core).
|
||||
|
||||
We do our best to keep the issue tracker tidy and organized, making it useful
|
||||
for everyone. For example, we classify open issues per application and perceived
|
||||
difficulty of the issue, making it easier for developers to
|
||||
[contribute to Elixir](#contributing).
|
||||
|
||||
## Bug reports
|
||||
|
||||
A bug is a _demonstrable problem_ that is caused by the code in the repository.
|
||||
Good bug reports are extremely helpful - thank you!
|
||||
|
||||
Guidelines for bug reports:
|
||||
|
||||
1. **Use the GitHub issue search** — [check if the issue has already been
|
||||
reported](https://github.com/elixir-lang/elixir/search?type=Issues).
|
||||
|
||||
2. **Check if the issue has been fixed** — try to reproduce it using the
|
||||
`master` branch in the repository.
|
||||
|
||||
3. **Isolate and report the problem** — ideally create a reduced test
|
||||
case.
|
||||
|
||||
Please try to be as detailed as possible in your report. Include information about
|
||||
your Operating System, your Erlang and Elixir versions. Please provide steps to
|
||||
reproduce the issue as well as the outcome you were expecting! All these details
|
||||
will help developers to fix any potential bugs.
|
||||
|
||||
Example:
|
||||
|
||||
> Short and descriptive example bug report title
|
||||
>
|
||||
> A summary of the issue and the environment in which it occurs. If suitable,
|
||||
> include the steps required to reproduce the bug.
|
||||
>
|
||||
> 1. This is the first step
|
||||
> 2. This is the second step
|
||||
> 3. Further steps, etc.
|
||||
>
|
||||
> `<url>` - a link to the reduced test case (e.g. a GitHub Gist)
|
||||
>
|
||||
> Any other information you want to share that is relevant to the issue being
|
||||
> reported. This might include the lines of code that you have identified as
|
||||
> causing the bug, and potential solutions (and your opinions on their
|
||||
> merits).
|
||||
|
||||
## Feature requests
|
||||
|
||||
Feature requests are welcome and should be discussed on [the elixir-core mailing list](https://groups.google.com/group/elixir-lang-core). But take a moment to find
|
||||
out whether your idea fits with the scope and aims of the project. It's up to *you*
|
||||
to make a strong case to convince the community of the merits of this feature.
|
||||
Please provide as much detail and context as possible.
|
||||
|
||||
## Contributing
|
||||
|
||||
We incentivize everyone to contribute to Elixir and help us tackle
|
||||
existing issues! To do so, there are a few things you need to know
|
||||
about the code. First, Elixir code is divided in applications inside
|
||||
the `lib` folder:
|
||||
|
||||
* `elixir` - Contains Elixir's kernel and stdlib
|
||||
|
||||
* `eex` - Template engine that allows you to embed Elixir
|
||||
|
||||
* `ex_unit` - Simple test framework that ships with Elixir
|
||||
|
||||
* `iex` - IEx, Elixir's interactive shell
|
||||
|
||||
* `mix` - Elixir's build tool
|
||||
|
||||
You can run all tests in the root directory with `make test` and you can
|
||||
also run tests for a specific framework `make test_#{NAME}`, for example,
|
||||
`make test_ex_unit`.
|
||||
|
||||
In case you are changing a single file, you can compile and run tests only
|
||||
for that particular file for fast development cycles. For example, if you
|
||||
are changing the String module, you can compile it and run its tests as:
|
||||
|
||||
```sh
|
||||
bin/elixirc lib/elixir/lib/string.ex -o lib/elixir/ebin
|
||||
bin/elixir lib/elixir/test/elixir/string_test.exs
|
||||
```
|
||||
|
||||
After your changes are done, please remember to run the full suite with
|
||||
`make test`.
|
||||
|
||||
From time to time, your tests may fail in an existing Elixir checkout and
|
||||
may require a clean start by running `make clean compile`. You can always
|
||||
check [the official build status on Travis-CI](https://travis-ci.org/elixir-lang/elixir).
|
||||
|
||||
With tests running and passing, you are ready to contribute to Elixir and
|
||||
send your pull requests.
|
||||
|
||||
## Contributing Documentation
|
||||
|
||||
Code documentation (`@doc`, `@moduledoc`, `@typedoc`) has a special convention:
|
||||
the first paragraph is considered to be a short summary.
|
||||
|
||||
For functions, macros and callbacks say what it will do. For example write
|
||||
something like:
|
||||
|
||||
```elixir
|
||||
@doc """
|
||||
Returns only those elements for which `fun` is `true`.
|
||||
|
||||
...
|
||||
"""
|
||||
def filter(collection, fun) ...
|
||||
```
|
||||
|
||||
For modules, protocols and types say what it is. For example write
|
||||
something like:
|
||||
|
||||
```elixir
|
||||
defmodule File.Stat do
|
||||
@moduledoc """
|
||||
Information about a file.
|
||||
|
||||
...
|
||||
"""
|
||||
|
||||
defstruct [...]
|
||||
end
|
||||
```
|
||||
|
||||
Keep in mind that the first paragraph might show up in a summary somewhere, long
|
||||
texts in the first paragraph create very ugly summaries. As a rule of thumb
|
||||
anything longer than 80 characters is too long.
|
||||
|
||||
Try to keep unnecessary details out of the first paragraph, it's only there to
|
||||
give a user a quick idea of what the documented "thing" does/is. The rest of the
|
||||
documentation string can contain the details, for example when a value and when
|
||||
`nil` is returned.
|
||||
|
||||
If possible include examples, preferably in a form that works with doctests. For
|
||||
example:
|
||||
|
||||
```elixir
|
||||
@doc """
|
||||
Returns only those elements for which `fun` is `true`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Enum.filter([1, 2, 3], fn(x) -> rem(x, 2) == 0 end)
|
||||
[2]
|
||||
|
||||
"""
|
||||
def filter(collection, fun) ...
|
||||
```
|
||||
|
||||
This makes it easy to test the examples so that they don't go stale and examples
|
||||
are often a great help in explaining what a function does.
|
||||
|
||||
## Pull requests
|
||||
|
||||
Good pull requests - patches, improvements, new features - are a fantastic
|
||||
help. They should remain focused in scope and avoid containing unrelated
|
||||
commits.
|
||||
|
||||
**NOTE**: Do not send code style changes as pull requests like changing
|
||||
the indentation of some particular code snippet or how a function is called.
|
||||
Those will not be accepted as they pollute the repository history with non
|
||||
functional changes and are often based on personal preferences.
|
||||
|
||||
**IMPORTANT**: By submitting a patch, you agree that your work will be
|
||||
licensed under the license used by the project.
|
||||
|
||||
If you have any large pull request in mind (e.g. implementing features,
|
||||
refactoring code, etc), **please ask first** otherwise you risk spending
|
||||
a lot of time working on something that the project's developers might
|
||||
not want to merge into the project.
|
||||
|
||||
Please adhere to the coding conventions in the project (indentation,
|
||||
accurate comments, etc.) and don't forget to add your own tests and
|
||||
documentation. When working with Git, we recommend the following process
|
||||
in order to craft an excellent pull request:
|
||||
|
||||
1. [Fork](https://help.github.com/fork-a-repo/) the project, clone your fork,
|
||||
and configure the remotes:
|
||||
|
||||
```sh
|
||||
# Clone your fork of the repo into the current directory
|
||||
git clone https://github.com/<your-username>/elixir
|
||||
# Navigate to the newly cloned directory
|
||||
cd elixir
|
||||
# Assign the original repo to a remote called "upstream"
|
||||
git remote add upstream https://github.com/elixir-lang/elixir
|
||||
```
|
||||
|
||||
2. If you cloned a while ago, get the latest changes from upstream:
|
||||
|
||||
```sh
|
||||
git checkout master
|
||||
git pull upstream master
|
||||
```
|
||||
|
||||
3. Create a new topic branch (off of `master`) to contain your feature, change,
|
||||
or fix.
|
||||
|
||||
**IMPORTANT**: Making changes in `master` is discouraged. You should always
|
||||
keep your local `master` in sync with upstream `master` and make your
|
||||
changes in topic branches.
|
||||
|
||||
```sh
|
||||
git checkout -b <topic-branch-name>
|
||||
```
|
||||
|
||||
4. Commit your changes in logical chunks. Keep your commit messages organized,
|
||||
with a short description in the first line and more detailed information on
|
||||
the following lines. Feel free to use Git's
|
||||
[interactive rebase](https://help.github.com/articles/interactive-rebase)
|
||||
feature to tidy up your commits before making them public.
|
||||
|
||||
5. Make sure all the tests are still passing.
|
||||
|
||||
```sh
|
||||
make test
|
||||
```
|
||||
|
||||
This command will compile the code in your branch and use that
|
||||
version of Elixir to run the tests. This is needed to ensure your changes can
|
||||
pass all the tests.
|
||||
|
||||
6. Push your topic branch up to your fork:
|
||||
|
||||
```sh
|
||||
git push origin <topic-branch-name>
|
||||
```
|
||||
|
||||
7. [Open a Pull Request](https://help.github.com/articles/using-pull-requests/)
|
||||
with a clear title and description.
|
||||
|
||||
8. If you haven't updated your pull request for a while, you should consider
|
||||
rebasing on master and resolving any conflicts.
|
||||
|
||||
**IMPORTANT**: _Never ever_ merge upstream `master` into your branches. You
|
||||
should always `git rebase` on `master` to bring your changes up to date when
|
||||
necessary.
|
||||
|
||||
```sh
|
||||
git checkout master
|
||||
git pull upstream master
|
||||
git checkout <your-topic-branch>
|
||||
git rebase master
|
||||
```
|
||||
|
||||
We have saved some excellent pull requests we have received in the past in case
|
||||
you are looking for some examples:
|
||||
|
||||
* [Implement Enum.member? – Pull Request](https://github.com/elixir-lang/elixir/pull/992)
|
||||
* [Add String.valid? – Pull Request](https://github.com/elixir-lang/elixir/pull/1058)
|
||||
* [Implement capture_io for ExUnit – Pull Request](https://github.com/elixir-lang/elixir/pull/1059)
|
||||
|
||||
Thank you for your contributions!
|
||||
@@ -1,18 +0,0 @@
|
||||
### Precheck
|
||||
|
||||
* Do not use the issues tracker for help or support (try Elixir Forum, Stack Overflow, IRC, etc.)
|
||||
* For proposing a new feature, please start a discussion on the Elixir Core mailing list
|
||||
* For bugs, do a quick search and make sure the bug has not yet been reported
|
||||
* Finally, be nice and have fun!
|
||||
|
||||
### Environment
|
||||
|
||||
* Elixir version (elixir -v):
|
||||
* Operating system:
|
||||
|
||||
### Current behavior
|
||||
|
||||
Include code samples, errors and stacktraces if appropriate.
|
||||
|
||||
### Expected behavior
|
||||
|
||||
@@ -1,31 +1,27 @@
|
||||
REBAR ?= "$(CURDIR)/rebar"
|
||||
PREFIX ?= /usr/local
|
||||
SHARE_PREFIX ?= $(PREFIX)/share
|
||||
CANONICAL :=
|
||||
DOCS := v1.2
|
||||
ELIXIRC := bin/elixirc --verbose --ignore-module-conflict
|
||||
ERLC := erlc -I lib/elixir/include
|
||||
ERL := erl -I lib/elixir/include -noshell -pa lib/elixir/ebin
|
||||
VERSION := $(strip $(shell cat VERSION))
|
||||
Q := @
|
||||
LIBDIR := lib
|
||||
BINDIR := bin
|
||||
INSTALL = install
|
||||
INSTALL_DIR = $(INSTALL) -m755 -d
|
||||
INSTALL_DATA = $(INSTALL) -m644
|
||||
INSTALL_PROGRAM = $(INSTALL) -m755
|
||||
GIT_REVISION = $(strip $(shell git rev-parse HEAD 2> /dev/null ))
|
||||
GIT_TAG = $(strip $(shell head="$(call GIT_REVISION)"; git tag --points-at $$head 2> /dev/null | tail -1) )
|
||||
|
||||
.PHONY: install compile erlang elixir build_plt clean_plt dialyze test clean install_man clean_man docs Docs.zip Precompiled.zip zips
|
||||
.PHONY: install compile erlang elixir build_plt clean_plt dialyze test clean install_man clean_man docs Docs.zip Precompiled.zip publish_zips publish_docs publish_mix
|
||||
.NOTPARALLEL: compile
|
||||
|
||||
#==> Functions
|
||||
|
||||
define CHECK_ERLANG_RELEASE
|
||||
$(Q) erl -noshell -eval '{V,_} = string:to_integer(erlang:system_info(otp_release)), io:fwrite("~s", [is_integer(V) and (V >= 18)])' -s erlang halt | grep -q '^true'; \
|
||||
if [ $$? != 0 ]; then \
|
||||
echo "At least Erlang 18.0 is required to build Elixir"; \
|
||||
exit 1; \
|
||||
$(Q) erl -noshell -eval 'io:fwrite("~s", [erlang:system_info(otp_release) >= "18"])' -s erlang halt | grep -q '^true'; \
|
||||
if [ $$? != 0 ]; then \
|
||||
echo "At least Erlang 18.0 is required to build Elixir"; \
|
||||
exit 1; \
|
||||
fi;
|
||||
endef
|
||||
|
||||
@@ -75,7 +71,7 @@ elixir: stdlib lib/eex/ebin/Elixir.EEx.beam mix ex_unit logger eex iex
|
||||
|
||||
stdlib: $(KERNEL) VERSION
|
||||
$(KERNEL): lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex lib/elixir/lib/*/*/*.ex
|
||||
$(Q) if [ ! -f $(KERNEL) ]; then \
|
||||
$(Q) if [ ! -f $(KERNEL) ]; then \
|
||||
echo "==> bootstrap (compile)"; \
|
||||
$(ERL) -s elixir_compiler core -s erlang halt; \
|
||||
fi
|
||||
@@ -89,6 +85,7 @@ $(KERNEL): lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex lib/elixir/lib/*/*/*.ex
|
||||
unicode: $(UNICODE)
|
||||
$(UNICODE): lib/elixir/unicode/*
|
||||
@ echo "==> unicode (compile)";
|
||||
@ echo "Embedding the Unicode database... (this may take a while)"
|
||||
$(Q) cd lib/elixir && ../../$(ELIXIRC) unicode/unicode.ex -o ebin;
|
||||
|
||||
$(eval $(call APP_TEMPLATE,ex_unit,ExUnit))
|
||||
@@ -106,9 +103,9 @@ install: compile
|
||||
done
|
||||
$(Q) $(INSTALL_DIR) "$(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/bin"
|
||||
$(Q) $(INSTALL_PROGRAM) $(filter-out %.ps1, $(filter-out %.bat, $(wildcard bin/*))) "$(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/bin"
|
||||
$(Q) $(INSTALL_DIR) "$(DESTDIR)$(PREFIX)/$(BINDIR)"
|
||||
$(Q) $(INSTALL_DIR) "$(DESTDIR)$(PREFIX)/bin"
|
||||
$(Q) for file in "$(DESTDIR)$(PREFIX)"/$(LIBDIR)/elixir/bin/* ; do \
|
||||
ln -sf "../$(LIBDIR)/elixir/bin/$${file##*/}" "$(DESTDIR)$(PREFIX)/$(BINDIR)/" ; \
|
||||
ln -sf "../$(LIBDIR)/elixir/bin/$${file##*/}" "$(DESTDIR)$(PREFIX)/bin/" ; \
|
||||
done
|
||||
$(MAKE) install_man
|
||||
|
||||
@@ -127,19 +124,18 @@ clean:
|
||||
clean_exbeam:
|
||||
$(Q) rm -f lib/*/ebin/Elixir.*.beam
|
||||
|
||||
#==> Create Documentation
|
||||
#==> Create Documentation
|
||||
|
||||
LOGO_PATH = $(shell test -f ../docs/logo.png && echo "--logo ../docs/logo.png")
|
||||
SOURCE_REF = $(shell tag="$(call GIT_TAG)" revision="$(call GIT_REVISION)"; echo "$${tag:-$$revision}\c")
|
||||
DOCS_FORMAT = html
|
||||
COMPILE_DOCS = bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" -m "$(3)" -u "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" $(call LOGO_PATH) -o doc/$(2) -n https://hexdocs.pm/$(2)/$(CANONICAL) -p http://elixir-lang.org/docs.html -f "$(DOCS_FORMAT)" $(4)
|
||||
SOURCE_REF = $(shell head="$$(git rev-parse HEAD)" tag="$$(git tag --points-at $$head | tail -1)" ; echo "$${tag:-$$head}\c")
|
||||
COMPILE_DOCS = bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" -m "$(3)" -u "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" $(call LOGO_PATH) -o doc/$(2) -p http://elixir-lang.org/docs.html $(4)
|
||||
|
||||
docs: compile ../ex_doc/bin/ex_doc docs_elixir docs_eex docs_mix docs_iex docs_ex_unit docs_logger
|
||||
|
||||
docs_elixir: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (elixir)"
|
||||
$(Q) rm -rf doc/elixir
|
||||
$(call COMPILE_DOCS,Elixir,elixir,Kernel,-e "lib/elixir/pages/Behaviours.md" -e "lib/elixir/pages/Guards.md" -e "lib/elixir/pages/Naming Conventions.md" -e "lib/elixir/pages/Operators.md" -e "lib/elixir/pages/Typespecs.md" -e "lib/elixir/pages/Writing Documentation.md")
|
||||
$(call COMPILE_DOCS,Elixir,elixir,Kernel,-e "lib/elixir/pages/Typespecs.md" -e "lib/elixir/pages/Writing Documentation.md")
|
||||
|
||||
docs_eex: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (eex)"
|
||||
@@ -182,7 +178,13 @@ Precompiled.zip: build_man compile
|
||||
zip -9 -r Precompiled-v$(VERSION).zip bin CHANGELOG.md lib/*/ebin LICENSE man NOTICE README.md VERSION
|
||||
@ echo "Precompiled file created $(CURDIR)/Precompiled-v$(VERSION).zip"
|
||||
|
||||
zips: Precompiled.zip Docs.zip
|
||||
#==> Publish
|
||||
|
||||
publish_zips: Precompiled.zip Docs.zip
|
||||
|
||||
publish_docs: docs
|
||||
rm -rf ../docs/$(DOCS)/*/
|
||||
cp -R doc/* ../docs/$(DOCS)
|
||||
|
||||
#==> Tests tasks
|
||||
|
||||
@@ -251,9 +253,9 @@ clean_man:
|
||||
rm -f man/iex.1
|
||||
|
||||
install_man: build_man
|
||||
$(Q) mkdir -p $(DESTDIR)$(SHARE_PREFIX)/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/elixir.1 $(DESTDIR)$(SHARE_PREFIX)/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/elixirc.1 $(DESTDIR)$(SHARE_PREFIX)/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/iex.1 $(DESTDIR)$(SHARE_PREFIX)/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/mix.1 $(DESTDIR)$(SHARE_PREFIX)/man/man1
|
||||
$(Q) mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/elixir.1 $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/elixirc.1 $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/iex.1 $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/mix.1 $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(MAKE) clean_man
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||

|
||||
=========
|
||||
[](https://travis-ci.org/elixir-lang/elixir)
|
||||
|
||||
For more about Elixir, installation and documentation,
|
||||
[check Elixir's website](http://elixir-lang.org/).
|
||||
|
||||
## Compiling from source
|
||||
## Usage
|
||||
|
||||
To run Elixir from source, clone this repository to your machine, compile and test it:
|
||||
If you want to contribute to Elixir or run it from source, clone this
|
||||
repository to your machine, compile and test it:
|
||||
|
||||
```sh
|
||||
git clone https://github.com/elixir-lang/elixir.git
|
||||
@@ -24,98 +25,26 @@ If Elixir fails to build (specifically when pulling in a new version via
|
||||
`git`), be sure to remove any previous build artifacts by running
|
||||
`make clean`, then `make test`.
|
||||
|
||||
If tests pass, you are ready to move on to the [Getting Started guide][1]
|
||||
or to try Interactive Elixir by running `bin/iex` in your terminal.
|
||||
If tests pass, you are ready to move on to the
|
||||
[Getting Started guide][1] or to try Interactive Elixir by running:
|
||||
`bin/iex` in your terminal.
|
||||
|
||||
However, if tests fail, it is likely you have an outdated Erlang version
|
||||
(Elixir requires Erlang 18.0 or later). You can check your Erlang version
|
||||
by calling `erl` in the command line. You will see some information as follows:
|
||||
(Elixir requires Erlang 18.0 or later).
|
||||
You can check your Erlang version by calling `erl` in the command line.
|
||||
You will see some information as follows:
|
||||
|
||||
Erlang/OTP 18 [erts-7.0] [source] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]
|
||||
`Erlang/OTP 18 [erts-7.0] [source] [smp:2:2] [async-threads:10] [hipe]
|
||||
[kernel-poll:false]`
|
||||
|
||||
If you have properly set up your dependencies and tests still fail,
|
||||
you may want to open up a bug report, as explained next.
|
||||
|
||||
## Bug reports
|
||||
|
||||
For reporting bugs, [visit our issues tracker][2] and follow the steps
|
||||
for reporting a new issue. Please disclose security vulnerabilities
|
||||
privately at elixir-security@googlegroups.com.
|
||||
|
||||
## Contributing
|
||||
|
||||
We welcome everyone to contribute to Elixir and help us tackle existing issues!
|
||||
To do so, there are a few things you need to know about the code. First, Elixir
|
||||
code is divided in applications inside the `lib` folder:
|
||||
|
||||
* `elixir` - Contains Elixir's kernel and stdlib
|
||||
|
||||
* `eex` - Template engine that allows you to embed Elixir
|
||||
|
||||
* `ex_unit` - Simple test framework that ships with Elixir
|
||||
|
||||
* `iex` - IEx, Elixir's interactive shell
|
||||
|
||||
* `logger` - The built-in logger
|
||||
|
||||
* `mix` - Elixir's build tool
|
||||
|
||||
You can run all tests in the root directory with `make test` and you can
|
||||
also run tests for a specific framework `make test_#{NAME}`, for example,
|
||||
`make test_ex_unit`.
|
||||
|
||||
In case you are changing a single file, you can compile and run tests only
|
||||
for that particular file for fast development cycles. For example, if you
|
||||
are changing the String module, you can compile it and run its tests as:
|
||||
|
||||
```sh
|
||||
bin/elixirc lib/elixir/lib/string.ex -o lib/elixir/ebin
|
||||
bin/elixir lib/elixir/test/elixir/string_test.exs
|
||||
```
|
||||
|
||||
After your changes are done, please remember to run the full suite with
|
||||
`make test`.
|
||||
|
||||
From time to time, your tests may fail in an existing Elixir checkout and
|
||||
may require a clean start by running `make clean compile`. You can always
|
||||
check [the official build status on Travis-CI](https://travis-ci.org/elixir-lang/elixir).
|
||||
|
||||
With tests running and passing, you are ready to contribute to Elixir and
|
||||
[send a pull request](https://help.github.com/articles/using-pull-requests/).
|
||||
We have saved some excellent pull requests we have received in the past in
|
||||
case you are looking for some examples:
|
||||
|
||||
* [Implement Enum.member? – Pull Request](https://github.com/elixir-lang/elixir/pull/992)
|
||||
* [Add String.valid? – Pull Request](https://github.com/elixir-lang/elixir/pull/1058)
|
||||
* [Implement capture_io for ExUnit – Pull Request](https://github.com/elixir-lang/elixir/pull/1059)
|
||||
|
||||
We usually keep a list of enhancements and bugs [in the issue tracker][2].
|
||||
For proposing new features, please start a discussion in the
|
||||
[Elixir Core mailing list][3]. Keep in mind that it is your responsibility
|
||||
to argue and explain why a feature is useful and how it will impact the
|
||||
codebase and the community. Finally, remember all interactions in our official
|
||||
spaces follow our [Code of Conduct][7].
|
||||
|
||||
### Reviewing changes
|
||||
|
||||
Once a pull request is sent, the Elixir team will review your changes.
|
||||
We outline our process below to clarify the roles of everyone involved.
|
||||
|
||||
All pull requests must be approved by two committers before being merged into
|
||||
the repository. In case any changes are necessary, the team will leave
|
||||
appropriate comments requesting changes to the code.
|
||||
|
||||
The Elixir team may optionally assign someone to review a pull request.
|
||||
In case someone is assigned, they must explicitly approve the code before
|
||||
another team member can merge it.
|
||||
|
||||
When review is completed, your pull request will be squashed and merged
|
||||
into the repository.
|
||||
If you have the correct version and tests still fail, feel free to
|
||||
[open an issue][2].
|
||||
|
||||
## Building documentation
|
||||
|
||||
Building the documentation requires [ExDoc](https://github.com/elixir-lang/ex_doc)
|
||||
to be installed and built alongside Elixir:
|
||||
Building the documentation requires
|
||||
[ExDoc](https://github.com/elixir-lang/ex_doc) to be installed and built
|
||||
alongside Elixir.
|
||||
|
||||
```sh
|
||||
# After cloning and compiling Elixir, in its parent directory:
|
||||
@@ -124,26 +53,31 @@ cd ex_doc && ../elixir/bin/mix do deps.get, compile
|
||||
cd ../elixir && make docs
|
||||
```
|
||||
|
||||
This will produce documentation sets for `elixir`, `mix`, etc., under
|
||||
the `doc` directory. If you are planning to contribute documentation,
|
||||
[please check our best practices for writing documentation](http://elixir-lang.org/docs/stable/elixir/writing-documentation.html).
|
||||
This will produce documentation sets for `elixir`, `mix`, etc., under the `doc` directory.
|
||||
|
||||
## Development links
|
||||
## Contributing
|
||||
|
||||
* [Elixir Website][1]
|
||||
* [Elixir Documentation][6]
|
||||
* [Elixir Core Mailing list (development)][3]
|
||||
* [Issues tracker][2]
|
||||
* [Code of Conduct][7]
|
||||
* **[#elixir-lang][4]** on [Freenode][5] IRC
|
||||
We appreciate any contribution to Elixir.
|
||||
Check our [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) and
|
||||
[CONTRIBUTING.md](CONTRIBUTING.md) guides for more information.
|
||||
We usually keep a list of features and bugs [in the issue tracker][2].
|
||||
|
||||
## Important links
|
||||
|
||||
* [Elixir Website][1]
|
||||
* [Elixir Documentation][7]
|
||||
* **[#elixir-lang][5]** on [Freenode][6] IRC
|
||||
* [Issue tracker][2]
|
||||
* [elixir-talk Mailing list (questions)][3]
|
||||
* [elixir-core Mailing list (development)][4]
|
||||
|
||||
[1]: http://elixir-lang.org
|
||||
[2]: https://github.com/elixir-lang/elixir/issues
|
||||
[3]: https://groups.google.com/group/elixir-lang-core
|
||||
[4]: https://webchat.freenode.net/?channels=#elixir-lang
|
||||
[5]: http://www.freenode.net
|
||||
[6]: http://elixir-lang.org/docs.html
|
||||
[7]: CODE_OF_CONDUCT.md
|
||||
[3]: https://groups.google.com/group/elixir-lang-talk
|
||||
[4]: https://groups.google.com/group/elixir-lang-core
|
||||
[5]: https://webchat.freenode.net/?channels=#elixir-lang
|
||||
[6]: http://www.freenode.net
|
||||
[7]: http://elixir-lang.org/docs.html
|
||||
|
||||
## License
|
||||
|
||||
|
||||
+13
-15
@@ -18,26 +18,24 @@ This document simply outlines the release process:
|
||||
|
||||
7. Push branch and the new tag
|
||||
|
||||
8. If a new `vMAJOR.MINOR`, create a new branch "vMAJOR.MINOR" and set `CANONICAL=` in Makefile before building docs
|
||||
8. Publish new docs with `make publish_docs`, copy docs to `docs/stable` if appropriate, and push to GitHub Pages
|
||||
|
||||
9. Publish new zips with `make zips`, upload `Precompiled.zip` and `Docs.zip` to GitHub Releases
|
||||
9. Publish new zips with `make publish_zips`, upload `Precompiled.zip` and `Docs.zip` to GitHub Releases
|
||||
|
||||
10. Add the release to `elixir.csv` and `_data/elixir-versions.yml` files in `elixir-lang/elixir-lang.github.com`
|
||||
10. Add the release to `elixir.csv` file in `elixir-lang/elixir-lang.github.com`
|
||||
|
||||
11. After a new `vMAJOR.MINOR`, move back to master, bump versions, start new CHANGELOG, add `-dev` back and commit "Start vMAJOR.MINOR+1"
|
||||
11. Build and push standalone Mix with `make publish_mix` (requires AWS credentials)
|
||||
|
||||
## New vMAJOR.MINOR releases
|
||||
|
||||
12. Create a new branch "vMAJOR.MINOR"
|
||||
|
||||
13. Move docs generation to `docs/vMAJOR.MINOR` in Makefile and copy them from `docs/stable` (change index.html accordingly)
|
||||
|
||||
14. In master, bump versions, start new CHANGELOG, add `-dev` back and commit "Start vVERSION+1"
|
||||
|
||||
## Places where version is mentioned
|
||||
|
||||
* VERSION
|
||||
* VERSION (make sure there is no newline in this file)
|
||||
* CHANGELOG.md
|
||||
* src/elixir.app.src (not lib/elixir/src/elixir.app.src)
|
||||
|
||||
## Deprecation policy
|
||||
|
||||
Elixir deprecations happens in 3 steps:
|
||||
|
||||
1. The feature is soft-deprecated. It means both CHANGELOG and documentation must list the feature as deprecated but no warning is effectively emitted by running the code. There is no requirement to soft-deprecate a feature.
|
||||
|
||||
2. The feature is effectively deprecated by emitting warnings on usage. In order to deprecate a feature, the proposed alternative MUST exist for AT LEAST two versions. For example, `Enum.uniq/2` was soft-deprecated in favor of `Enum.uniq_by/2` in Elixir v1.1. This means a deprecation warning may only be emitted by Elixir v1.3 or later.
|
||||
|
||||
3. The feature is removed. This can only happen on major releases. This means deprecated features in Elixir v1.x shall only be removed by Elixir v2.x.
|
||||
|
||||
+16
-33
@@ -2,25 +2,22 @@
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
echo "Usage: `basename $0` [options] [.exs file] [data]
|
||||
|
||||
-v Prints version and exits
|
||||
-e COMMAND Evaluates the given command (*)
|
||||
-r FILE Requires the given files/patterns (*)
|
||||
-S SCRIPT Finds and executes the given script in PATH
|
||||
-pr FILE Requires the given files/patterns in parallel (*)
|
||||
-pa PATH Prepends the given path to Erlang code path (*)
|
||||
-pz PATH Appends the given path to Erlang code path (*)
|
||||
|
||||
--app APP Starts the given app and its dependencies (*)
|
||||
--cookie COOKIE Sets a cookie for this distributed node
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
--hidden Makes a hidden node
|
||||
--logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
--logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
--name NAME Makes and assigns a name to the distributed node
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
--sname NAME Makes and assigns a short name to the distributed node
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
-v Prints version and exits
|
||||
-e \"command\" Evaluates the given command (*)
|
||||
-r \"file\" Requires the given files/patterns (*)
|
||||
-S \"script\" Finds and executes the given script
|
||||
-pr \"file\" Requires the given files/patterns in parallel (*)
|
||||
-pa \"path\" Prepends the given path to Erlang code path (*)
|
||||
-pz \"path\" Appends the given path to Erlang code path (*)
|
||||
--app \"app\" Start the given app and its dependencies (*)
|
||||
--erl \"switches\" Switches to be passed down to Erlang (*)
|
||||
--name \"name\" Makes and assigns a name to the distributed node
|
||||
--sname \"name\" Makes and assigns a short name to the distributed node
|
||||
--cookie \"cookie\" Sets a cookie for this distributed node
|
||||
--hidden Makes a hidden node
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
|
||||
** Options marked with (*) can be given more than once
|
||||
** Options given after the .exs file or -- are passed down to the executed code
|
||||
@@ -71,20 +68,6 @@ while [ $I -le $# ]; do
|
||||
eval "VAL=\${$I}"
|
||||
ERL="$ERL `echo $PEEK | cut -c 2-` "$VAL""
|
||||
;;
|
||||
--logger-otp-reports)
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
if [ "$VAL" == 'true' ] || [ "$VAL" == 'false' ]; then
|
||||
ERL="$ERL -logger handle_otp_reports "$VAL""
|
||||
fi
|
||||
;;
|
||||
--logger-sasl-reports)
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
if [ "$VAL" == 'true' ] || [ "$VAL" == 'false' ]; then
|
||||
ERL="$ERL -logger handle_sasl_reports "$VAL""
|
||||
fi
|
||||
;;
|
||||
--erl)
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
|
||||
+45
-52
@@ -1,38 +1,34 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
setlocal
|
||||
if ""%1""=="""" goto documentation
|
||||
if /I ""%1""==""--help"" goto documentation
|
||||
if /I ""%1""==""-h"" goto documentation
|
||||
if /I ""%1""==""/h"" goto documentation
|
||||
if ""%1""==""/?"" goto documentation
|
||||
if ""%1""=="""" goto :documentation
|
||||
if ""%1""==""--help"" goto :documentation
|
||||
if ""%1""==""-h"" goto :documentation
|
||||
if ""%1""==""/h"" goto :documentation
|
||||
goto parseopts
|
||||
|
||||
:documentation
|
||||
echo Usage: %~nx0 [options] [.exs file] [data]
|
||||
echo.
|
||||
echo -v Prints version and exits
|
||||
echo -e COMMAND Evaluates the given command (*)
|
||||
echo -r FILE Requires the given files/patterns (*)
|
||||
echo -S SCRIPT Finds and executes the given script in PATH
|
||||
echo -pr FILE Requires the given files/patterns in parallel (*)
|
||||
echo -pa PATH Prepends the given path to Erlang code path (*)
|
||||
echo -pz PATH Appends the given path to Erlang code path (*)
|
||||
echo.
|
||||
echo --app APP Starts the given app and its dependencies (*)
|
||||
echo --cookie COOKIE Sets a cookie for this distributed node
|
||||
echo --detached Starts the Erlang VM detached from console
|
||||
echo --erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
echo --hidden Makes a hidden node
|
||||
echo --logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
echo --logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
echo --name NAME Makes and assigns a name to the distributed node
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo --sname NAME Makes and assigns a short name to the distributed node
|
||||
echo --werl Uses Erlang's Windows shell GUI
|
||||
echo -v Prints version and exits
|
||||
echo -e command Evaluates the given command (*)
|
||||
echo -r file Requires the given files/patterns (*)
|
||||
echo -S script Finds and executes the given script
|
||||
echo -pr file Requires the given files/patterns in parallel (*)
|
||||
echo -pa path Prepends the given path to Erlang code path (*)
|
||||
echo -pz path Appends the given path to Erlang code path (*)
|
||||
echo --app app Start the given app and its dependencies (*)
|
||||
echo --erl switches Switches to be passed down to erlang (*)
|
||||
echo --name name Makes and assigns a name to the distributed node
|
||||
echo --sname name Makes and assigns a short name to the distributed node
|
||||
echo --cookie cookie Sets a cookie for this distributed node
|
||||
echo --hidden Makes a hidden node
|
||||
echo --detached Starts the Erlang VM detached from console
|
||||
echo --werl Uses Erlang's Windows shell GUI
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo.
|
||||
echo ** Options marked with (*) can be given more than once
|
||||
echo ** Options given after the .exs file or -- are passed down to the executed code
|
||||
echo ** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS or --erl
|
||||
echo ** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTIONS or --erl
|
||||
goto end
|
||||
|
||||
:parseopts
|
||||
@@ -61,52 +57,49 @@ set par="%1"
|
||||
shift
|
||||
if "%par%"=="" (
|
||||
rem if no parameters defined
|
||||
goto expand_erl_libs
|
||||
goto :expand_erl_libs
|
||||
)
|
||||
if "%par%"=="""" (
|
||||
rem if no parameters defined - special case for parameter that is already quoted
|
||||
goto expand_erl_libs
|
||||
goto :expand_erl_libs
|
||||
)
|
||||
rem ******* EXECUTION OPTIONS **********************
|
||||
if "%par%"==""--werl"" (set useWerl=1)
|
||||
if "%par%"==""+iex"" (set runMode="iex")
|
||||
rem ******* ELIXIR PARAMETERS **********************
|
||||
IF "%par%"==""--werl"" (Set useWerl=1)
|
||||
IF "%par%"==""+iex"" (Set runMode="iex")
|
||||
rem ******* elixir parameters **********************
|
||||
rem Note: we don't have to do anything with options that don't take an argument
|
||||
if """"=="%par:-e=%" (shift)
|
||||
if """"=="%par:-r=%" (shift)
|
||||
if """"=="%par:-pr=%" (shift)
|
||||
if """"=="%par:-pa=%" (shift)
|
||||
if """"=="%par:-pz=%" (shift)
|
||||
if """"=="%par:--app=%" (shift)
|
||||
if """"=="%par:--remsh=%" (shift)
|
||||
IF """"=="%par:-e=%" (shift)
|
||||
IF """"=="%par:-r=%" (shift)
|
||||
IF """"=="%par:-pr=%" (shift)
|
||||
IF """"=="%par:-pa=%" (shift)
|
||||
IF """"=="%par:-pz=%" (shift)
|
||||
IF """"=="%par:--app=%" (shift)
|
||||
IF """"=="%par:--remsh=%" (shift)
|
||||
rem ******* ERLANG PARAMETERS **********************
|
||||
if """"=="%par:--detached=%" (set parsErlang=%parsErlang% -detached)
|
||||
if """"=="%par:--hidden=%" (set parsErlang=%parsErlang% -hidden)
|
||||
if """"=="%par:--cookie=%" (set parsErlang=%parsErlang% -setcookie %1 && shift)
|
||||
if """"=="%par:--sname=%" (set parsErlang=%parsErlang% -sname %1 && shift)
|
||||
if """"=="%par:--name=%" (set parsErlang=%parsErlang% -name %1 && shift)
|
||||
if """"=="%par:--logger-otp-reports=%" (set parsErlang=%parsErlang% -logger handle_otp_reports %1 && shift)
|
||||
if """"=="%par:--logger-sasl-reports=%" (set parsErlang=%parsErlang% -logger handle_sasl_reports %1 && shift)
|
||||
if """"=="%par:--erl=%" (set beforeExtra=%beforeExtra% %~1 && shift)
|
||||
IF """"=="%par:--detached=%" (Set parsErlang=%parsErlang% -detached)
|
||||
IF """"=="%par:--hidden=%" (Set parsErlang=%parsErlang% -hidden)
|
||||
IF """"=="%par:--cookie=%" (Set parsErlang=%parsErlang% -setcookie %1 && shift)
|
||||
IF """"=="%par:--sname=%" (Set parsErlang=%parsErlang% -sname %1 && shift)
|
||||
IF """"=="%par:--name=%" (Set parsErlang=%parsErlang% -name %1 && shift)
|
||||
IF """"=="%par:--erl=%" (Set beforeExtra=%beforeExtra% %~1 && shift)
|
||||
goto:startloop
|
||||
|
||||
rem ******* assume all pre-params are parsed ********************
|
||||
:expand_erl_libs
|
||||
rem ******* expand all ebin paths as Windows does not support the ..\*\ebin wildcard ********************
|
||||
setlocal enabledelayedexpansion
|
||||
SETLOCAL enabledelayedexpansion
|
||||
set ext_libs=
|
||||
for /d %%d in ("%originPath%..\lib\*.") do (
|
||||
set ext_libs=!ext_libs! -pa "%%~fd\ebin"
|
||||
)
|
||||
setlocal disabledelayedexpansion
|
||||
|
||||
SETLOCAL disabledelayedexpansion
|
||||
:run
|
||||
if not %runMode% == "iex" (
|
||||
IF NOT %runMode% == "iex" (
|
||||
set beforeExtra=-noshell -s elixir start_cli %beforeExtra%
|
||||
)
|
||||
if %useWerl% equ 1 (
|
||||
IF %useWerl% EQU 1 (
|
||||
start werl.exe %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
) else (
|
||||
) ELSE (
|
||||
erl.exe %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
)
|
||||
:end
|
||||
|
||||
+7
-7
@@ -2,16 +2,16 @@
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
echo "Usage: `basename $0` [elixir switches] [compiler switches] [.ex files]
|
||||
|
||||
-o The directory to output compiled files
|
||||
--no-docs Do not attach documentation to compiled modules
|
||||
--no-debug-info Do not attach debug info to compiled modules
|
||||
--verbose Print compilation status
|
||||
--warnings-as-errors Treat warnings as errors and return non-zero exit code
|
||||
-o The directory to output compiled files
|
||||
--no-docs Do not attach documentation to compiled modules
|
||||
--no-debug-info Do not attach debug info to compiled modules
|
||||
--ignore-module-conflict
|
||||
--warnings-as-errors Treat warnings as errors and return non-zero exit code
|
||||
--verbose Print informational messages.
|
||||
|
||||
** Options given after -- are passed down to the executed code
|
||||
** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS
|
||||
** Options can be passed to the Erlang compiler using ERL_COMPILER_OPTIONS" >&2
|
||||
** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTIONS
|
||||
** Options can be passed to the erlang compiler using ERL_COMPILER_OPTIONS" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
+11
-18
@@ -1,12 +1,10 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
setlocal
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
set argc=0
|
||||
for %%A in (%*) do (
|
||||
if /I "%%A"=="--help" goto documentation
|
||||
if /I "%%A"=="-h" goto documentation
|
||||
if /I "%%A"=="/h" goto documentation
|
||||
if "%%A"=="/?" goto documentation
|
||||
set /A argc+=1
|
||||
if "%%A"=="--help" goto documentation
|
||||
if "%%A"=="-h" goto documentation
|
||||
if "%%A"=="/h" goto documentation
|
||||
set /A argc+=1
|
||||
)
|
||||
if %argc%==0 goto documentation
|
||||
goto run
|
||||
@@ -14,20 +12,15 @@ goto run
|
||||
:documentation
|
||||
echo Usage: %~nx0 [elixir switches] [compiler switches] [.ex files]
|
||||
echo.
|
||||
echo -o The directory to output compiled files
|
||||
echo --no-docs Do not attach documentation to compiled modules
|
||||
echo --no-debug-info Do not attach debug info to compiled modules
|
||||
echo --verbose Print compilation status
|
||||
echo --warnings-as-errors Treat warnings as errors and return non-zero exit code
|
||||
echo -o The directory to output compiled files
|
||||
echo --no-docs Do not attach documentation to compiled modules
|
||||
echo --no-debug-info Do not attach debug info to compiled modules
|
||||
echo --ignore-module-conflict
|
||||
echo --warnings-as-errors Treat warnings as errors and return non-zero exit code
|
||||
echo --verbose Print informational messages.
|
||||
echo.
|
||||
echo ** Options given after -- are passed down to the executed code
|
||||
echo ** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS
|
||||
echo ** Options can be passed to the Erlang compiler using ERL_COMPILER_OPTIONS
|
||||
goto end
|
||||
|
||||
echo ** Options can be passed to the Erlang compiler using ERL_COMPILER_OPTIONS >&2
|
||||
:run
|
||||
call "%~dp0\elixir.bat" +elixirc %*
|
||||
|
||||
:end
|
||||
endlocal
|
||||
|
||||
@@ -2,29 +2,24 @@
|
||||
if [ $# -gt 0 ] && ([ "$1" = "--help" ] || [ "$1" = "-h" ]); then
|
||||
echo "Usage: `basename $0` [options] [.exs file] [data]
|
||||
|
||||
-v Prints version and exits
|
||||
-e COMMAND Evaluates the given command (*)
|
||||
-r FILE Requires the given files/patterns (*)
|
||||
-S SCRIPT Finds and executes the given script in PATH
|
||||
-pr FILE Requires the given files/patterns in parallel (*)
|
||||
-pa PATH Prepends the given path to Erlang code path (*)
|
||||
-pz PATH Appends the given path to Erlang code path (*)
|
||||
|
||||
--app APP Starts the given app and its dependencies (*)
|
||||
--cookie COOKIE Sets a cookie for this distributed node
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
--hidden Makes a hidden node
|
||||
--logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
--logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
--name NAME Makes and assigns a name to the distributed node
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
--sname NAME Makes and assigns a short name to the distributed node
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
|
||||
--dot-iex PATH Overrides default .iex.exs file and uses path instead;
|
||||
path can be empty, then no file will be loaded
|
||||
--remsh NAME Connects to a node using a remote shell
|
||||
-v Prints version
|
||||
-e \"command\" Evaluates the given command (*)
|
||||
-r \"file\" Requires the given files/patterns (*)
|
||||
-S \"script\" Finds and executes the given script
|
||||
-pr \"file\" Requires the given files/patterns in parallel (*)
|
||||
-pa \"path\" Prepends the given path to Erlang code path (*)
|
||||
-pz \"path\" Appends the given path to Erlang code path (*)
|
||||
--app \"app\" Start the given app and its dependencies (*)
|
||||
--erl \"switches\" Switches to be passed down to Erlang (*)
|
||||
--name \"name\" Makes and assigns a name to the distributed node
|
||||
--sname \"name\" Makes and assigns a short name to the distributed node
|
||||
--cookie \"cookie\" Sets a cookie for this distributed node
|
||||
--hidden Makes a hidden node
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--remsh \"name\" Connects to a node using a remote shell
|
||||
--dot-iex \"path\" Overrides default .iex.exs file and uses path instead;
|
||||
path can be empty, then no file will be loaded
|
||||
|
||||
** Options marked with (*) can be given more than once
|
||||
** Options given after the .exs file or -- are passed down to the executed code
|
||||
|
||||
+1
-42
@@ -1,45 +1,4 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
setlocal
|
||||
if /I ""%1""==""--help"" goto documentation
|
||||
if /I ""%1""==""-h"" goto documentation
|
||||
if /I ""%1""==""/h"" goto documentation
|
||||
if ""%1""==""/?"" goto documentation
|
||||
goto run
|
||||
|
||||
:documentation
|
||||
echo Usage: %~nx0 [options] [.exs file] [data]
|
||||
echo.
|
||||
echo -v Prints version and exits
|
||||
echo -e COMMAND Evaluates the given command (*)
|
||||
echo -r FILE Requires the given files/patterns (*)
|
||||
echo -S SCRIPT Finds and executes the given script in PATH
|
||||
echo -pr FILE Requires the given files/patterns in parallel (*)
|
||||
echo -pa PATH Prepends the given path to Erlang code path (*)
|
||||
echo -pz PATH Appends the given path to Erlang code path (*)
|
||||
echo.
|
||||
echo --app APP Starts the given app and its dependencies (*)
|
||||
echo --cookie COOKIE Sets a cookie for this distributed node
|
||||
echo --detached Starts the Erlang VM detached from console
|
||||
echo --erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
echo --hidden Makes a hidden node
|
||||
echo --logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
echo --logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
echo --name NAME Makes and assigns a name to the distributed node
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo --sname NAME Makes and assigns a short name to the distributed node
|
||||
echo --werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
echo.
|
||||
echo --dot-iex PATH Overrides default .iex.exs file and uses path instead;
|
||||
echo path can be empty, then no file will be loaded
|
||||
echo --remsh NAME Connects to a node using a remote shell
|
||||
echo.
|
||||
echo ** Options marked with (*) can be given more than once
|
||||
echo ** Options given after the .exs file or -- are passed down to the executed code
|
||||
echo ** Options can be passed to the Erlang VM using ELIXIR_ERL_OPTIONS or --erl
|
||||
goto end
|
||||
|
||||
:run
|
||||
@if defined IEX_WITH_WERL (@set __ELIXIR_IEX_FLAGS=--werl) else (set __ELIXIR_IEX_FLAGS=)
|
||||
call "%~dp0\elixir.bat" +iex --erl "-user Elixir.IEx.CLI" --no-halt %__ELIXIR_IEX_FLAGS% %*
|
||||
:end
|
||||
endlocal
|
||||
@set __ELIXIR_IEX_FLAGS=
|
||||
|
||||
+2
-2
@@ -1,2 +1,2 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
call "%~dp0\elixir.bat" "%~dp0\mix" %*
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
call "%~dp0\elixir.bat" "%~dp0\mix" %*
|
||||
+9
-16
@@ -35,14 +35,13 @@ defmodule EEx do
|
||||
|
||||
## Options
|
||||
|
||||
All functions in this module accept EEx-related options.
|
||||
All functions in this module accepts EEx-related options.
|
||||
They are:
|
||||
|
||||
* `:line` - the line to be used as the template start. Defaults to 1.
|
||||
* `:file` - the file to be used in the template. Defaults to the given
|
||||
file the template is read from or to "nofile" when compiling from a string.
|
||||
* `:engine` - the EEx engine to be used for compilation.
|
||||
* `:trim` - trims whitespace left/right of quotation tags
|
||||
|
||||
## Engine
|
||||
|
||||
@@ -65,7 +64,7 @@ defmodule EEx do
|
||||
**must** use the equals sign (`=`). Since everything in
|
||||
Elixir is an expression, there are no exceptions for this rule.
|
||||
For example, while some template languages would special-case
|
||||
`if/2` clauses, they are treated the same in EEx and
|
||||
`if` clauses, they are treated the same in EEx and
|
||||
also require `=` in order to have their result printed:
|
||||
|
||||
<%= if true do %>
|
||||
@@ -90,7 +89,7 @@ defmodule EEx do
|
||||
|
||||
<%= {:ok, v} = Access.fetch(assigns, :foo); v %>
|
||||
|
||||
The `assigns` extension is useful when the number of variables
|
||||
The assigns extension is useful when the number of variables
|
||||
required by the template is not specified at compilation time.
|
||||
"""
|
||||
|
||||
@@ -111,7 +110,7 @@ defmodule EEx do
|
||||
|
||||
"""
|
||||
defmacro function_from_string(kind, name, source, args \\ [], options \\ []) do
|
||||
quote bind_quoted: binding() do
|
||||
quote bind_quoted: binding do
|
||||
info = Keyword.merge [file: __ENV__.file, line: __ENV__.line], options
|
||||
args = Enum.map args, fn arg -> {arg, [line: info[:line]], nil} end
|
||||
compiled = EEx.compile_string(source, info)
|
||||
@@ -148,7 +147,7 @@ defmodule EEx do
|
||||
|
||||
"""
|
||||
defmacro function_from_file(kind, name, file, args \\ [], options \\ []) do
|
||||
quote bind_quoted: binding() do
|
||||
quote bind_quoted: binding do
|
||||
info = Keyword.merge options, [file: file, line: 1]
|
||||
args = Enum.map args, fn arg -> {arg, [line: 1], nil} end
|
||||
compiled = EEx.compile_file(file, info)
|
||||
@@ -166,8 +165,7 @@ defmodule EEx do
|
||||
Gets a string `source` and generate a quoted expression
|
||||
that can be evaluated by Elixir or compiled to a function.
|
||||
"""
|
||||
@spec compile_string(String.t, Keyword.t) :: Macro.t | no_return
|
||||
def compile_string(source, options \\ []) when is_binary(source) and is_list(options) do
|
||||
def compile_string(source, options \\ []) do
|
||||
EEx.Compiler.compile(source, options)
|
||||
end
|
||||
|
||||
@@ -175,8 +173,7 @@ defmodule EEx do
|
||||
Gets a `filename` and generate a quoted expression
|
||||
that can be evaluated by Elixir or compiled to a function.
|
||||
"""
|
||||
@spec compile_file(String.t, Keyword.t) :: Macro.t | no_return
|
||||
def compile_file(filename, options \\ []) when is_binary(filename) and is_list(options) do
|
||||
def compile_file(filename, options \\ []) do
|
||||
options = Keyword.merge options, [file: filename, line: 1]
|
||||
compile_string(File.read!(filename), options)
|
||||
end
|
||||
@@ -190,9 +187,7 @@ defmodule EEx do
|
||||
"foo baz"
|
||||
|
||||
"""
|
||||
@spec eval_string(String.t, Keyword.t, Keyword.t) :: any
|
||||
def eval_string(source, bindings \\ [], options \\ [])
|
||||
when is_binary(source) and is_list(bindings) and is_list(options) do
|
||||
def eval_string(source, bindings \\ [], options \\ []) do
|
||||
compiled = compile_string(source, options)
|
||||
do_eval(compiled, bindings, options)
|
||||
end
|
||||
@@ -209,9 +204,7 @@ defmodule EEx do
|
||||
EEx.eval_file "sample.eex", [bar: "baz"] #=> "foo baz"
|
||||
|
||||
"""
|
||||
@spec eval_file(String.t, Keyword.t, Keyword.t) :: any
|
||||
def eval_file(filename, bindings \\ [], options \\ [])
|
||||
when is_binary(filename) and is_list(bindings) and is_list(options) do
|
||||
def eval_file(filename, bindings \\ [], options \\ []) do
|
||||
options = Keyword.put options, :file, filename
|
||||
compiled = compile_file(filename, options)
|
||||
do_eval(compiled, bindings, options)
|
||||
|
||||
+15
-19
@@ -9,8 +9,7 @@ defmodule EEx.Compiler do
|
||||
and the engine together by handling the tokens and invoking
|
||||
the engine every time a full expression or text is received.
|
||||
"""
|
||||
@spec compile(String.t, Keyword.t) :: Macro.t | no_return
|
||||
def compile(source, opts) when is_binary(source) and is_list(opts) do
|
||||
def compile(source, opts) do
|
||||
file = opts[:file] || "nofile"
|
||||
line = opts[:line] || 1
|
||||
trim = opts[:trim] || false
|
||||
@@ -18,47 +17,46 @@ defmodule EEx.Compiler do
|
||||
{:ok, tokens} ->
|
||||
state = %{engine: opts[:engine] || @default_engine,
|
||||
file: file, line: line, quoted: [], start_line: nil}
|
||||
generate_buffer(tokens, state.engine.init(opts), [], state)
|
||||
generate_buffer(tokens, "", [], state)
|
||||
{:error, line, message} ->
|
||||
raise EEx.SyntaxError, line: line, file: file, message: message
|
||||
end
|
||||
end
|
||||
|
||||
# Generates the buffers by handling each expression from the tokenizer.
|
||||
# It returns Macro.t/0 or it raises.
|
||||
# Generates the buffers by handling each expression from the tokenizer
|
||||
|
||||
defp generate_buffer([{:text, chars} | t], buffer, scope, state) do
|
||||
defp generate_buffer([{:text, chars}|t], buffer, scope, state) do
|
||||
buffer = state.engine.handle_text(buffer, IO.chardata_to_string(chars))
|
||||
generate_buffer(t, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:expr, line, mark, chars} | t], buffer, scope, state) do
|
||||
defp generate_buffer([{:expr, line, mark, chars}|t], buffer, scope, state) do
|
||||
expr = Code.string_to_quoted!(chars, [line: line, file: state.file])
|
||||
buffer = state.engine.handle_expr(buffer, IO.chardata_to_string(mark), expr)
|
||||
generate_buffer(t, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:start_expr, start_line, mark, chars} | t], buffer, scope, state) do
|
||||
defp generate_buffer([{:start_expr, start_line, mark, chars}|t], buffer, scope, state) do
|
||||
{contents, line, t} = look_ahead_text(t, start_line, chars)
|
||||
{contents, t} = generate_buffer(t, "", [contents | scope],
|
||||
{contents, t} = generate_buffer(t, "", [contents|scope],
|
||||
%{state | quoted: [], line: line, start_line: start_line})
|
||||
buffer = state.engine.handle_expr(buffer, IO.chardata_to_string(mark), contents)
|
||||
generate_buffer(t, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:middle_expr, line, _, chars} | t], buffer, [current | scope], state) do
|
||||
defp generate_buffer([{:middle_expr, line, _, chars}|t], buffer, [current|scope], state) do
|
||||
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
|
||||
generate_buffer(t, "", [wrapped | scope], %{state | line: line})
|
||||
generate_buffer(t, "", [wrapped|scope], %{state | line: line})
|
||||
end
|
||||
|
||||
defp generate_buffer([{:end_expr, line, _, chars} | t], buffer, [current | _], state) do
|
||||
defp generate_buffer([{:end_expr, line, _, chars}|t], buffer, [current|_], state) do
|
||||
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
|
||||
tuples = Code.string_to_quoted!(wrapped, [line: state.start_line, file: state.file])
|
||||
buffer = insert_quoted(tuples, state.quoted)
|
||||
{buffer, t}
|
||||
end
|
||||
|
||||
defp generate_buffer([{:end_expr, line, _, chars} | _], _buffer, [], state) do
|
||||
defp generate_buffer([{:end_expr, line, _, chars}|_], _buffer, [], state) do
|
||||
raise EEx.SyntaxError, message: "unexpected token #{inspect chars}", file: state.file, line: line
|
||||
end
|
||||
|
||||
@@ -76,23 +74,21 @@ defmodule EEx.Compiler do
|
||||
defp wrap_expr(current, line, buffer, chars, state) do
|
||||
new_lines = List.duplicate(?\n, line - state.line)
|
||||
key = length(state.quoted)
|
||||
placeholder = '__EEX__(' ++ Integer.to_charlist(key) ++ ');'
|
||||
placeholder = '__EEX__(' ++ Integer.to_char_list(key) ++ ');'
|
||||
{current ++ placeholder ++ new_lines ++ chars,
|
||||
%{state | quoted: [{key, buffer} | state.quoted]}}
|
||||
%{state | quoted: [{key, buffer}|state.quoted]}}
|
||||
end
|
||||
|
||||
# Look text ahead on expressions
|
||||
|
||||
defp look_ahead_text([{:text, text}, {:middle_expr, line, _, chars} | t]=list, start, contents) do
|
||||
defp look_ahead_text([{:text, text}, {:middle_expr, line, _, chars}|t]=list, start, contents) do
|
||||
if only_spaces?(text) do
|
||||
{contents ++ text ++ chars, line, t}
|
||||
else
|
||||
{contents, start, list}
|
||||
end
|
||||
end
|
||||
defp look_ahead_text([{:middle_expr, line, _, chars} | t], _start, contents) do
|
||||
{contents ++ chars, line, t}
|
||||
end
|
||||
|
||||
defp look_ahead_text(t, start, contents) do
|
||||
{contents, start, t}
|
||||
end
|
||||
|
||||
+13
-27
@@ -2,9 +2,7 @@ defmodule EEx.Engine do
|
||||
@moduledoc ~S"""
|
||||
Basic EEx engine that ships with Elixir.
|
||||
|
||||
An engine needs to implement four functions:
|
||||
|
||||
* `init(opts)` - returns the initial buffer
|
||||
An engine needs to implement three functions:
|
||||
|
||||
* `handle_body(quoted)` - receives the final built quoted
|
||||
expression, should do final post-processing and return a
|
||||
@@ -27,22 +25,17 @@ defmodule EEx.Engine do
|
||||
default implementations for the functions above.
|
||||
"""
|
||||
|
||||
@callback init(opts :: Keyword.t) :: Macro.t
|
||||
@callback handle_body(quoted :: Macro.t) :: Macro.t
|
||||
@callback handle_text(buffer :: Macro.t, text :: String.t) :: Macro.t
|
||||
@callback handle_expr(buffer :: Macro.t, marker :: String.t, expr :: Macro.t) :: Macro.t
|
||||
@callback handle_body(Macro.t) :: Macro.t
|
||||
@callback handle_text(Macro.t, String.t) :: Macro.t
|
||||
@callback handle_expr(Macro.t, String.t, Macro.t) :: Macro.t
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote do
|
||||
@behaviour EEx.Engine
|
||||
|
||||
def init(opts) do
|
||||
EEx.Engine.init(opts)
|
||||
end
|
||||
|
||||
def handle_body(quoted) do
|
||||
EEx.Engine.handle_body(quoted)
|
||||
def handle_body(body) do
|
||||
EEx.Engine.handle_body(body)
|
||||
end
|
||||
|
||||
def handle_text(buffer, text) do
|
||||
@@ -53,7 +46,7 @@ defmodule EEx.Engine do
|
||||
EEx.Engine.handle_expr(buffer, marker, expr)
|
||||
end
|
||||
|
||||
defoverridable [handle_body: 1, handle_expr: 3, handle_text: 2, init: 1]
|
||||
defoverridable [handle_body: 1, handle_expr: 3, handle_text: 2]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -72,7 +65,6 @@ defmodule EEx.Engine do
|
||||
end
|
||||
|
||||
"""
|
||||
@spec handle_assign(Macro.t) :: Macro.t
|
||||
def handle_assign({:@, meta, [{name, _, atom}]}) when is_atom(name) and is_atom(atom) do
|
||||
line = meta[:line] || 0
|
||||
quote line: line, do: EEx.Engine.fetch_assign!(var!(assigns), unquote(name))
|
||||
@@ -82,28 +74,21 @@ defmodule EEx.Engine do
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Raise on 2.0
|
||||
@spec fetch_assign!(map, Map.key) :: term | nil
|
||||
# TODO: raise on 1.3 or 1.4
|
||||
def fetch_assign!(assigns, key) do
|
||||
case Access.fetch(assigns, key) do
|
||||
{:ok, val} ->
|
||||
val
|
||||
:error ->
|
||||
keys = Enum.map(assigns, &elem(&1, 0))
|
||||
IO.warn "assign @#{key} not available in EEx template. " <>
|
||||
"Please ensure all assigns are given as options. " <>
|
||||
"Available assigns: #{inspect keys}"
|
||||
IO.write :stderr, "warning: assign @#{key} not available in eex template. " <>
|
||||
"Please ensure all assigns are given as options. " <>
|
||||
"Available assigns: #{inspect keys}\n" <>
|
||||
Exception.format_stacktrace
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an empty string as initial buffer.
|
||||
"""
|
||||
def init(_opts) do
|
||||
""
|
||||
end
|
||||
|
||||
@doc """
|
||||
The default implementation simply returns the given expression.
|
||||
"""
|
||||
@@ -126,6 +111,7 @@ defmodule EEx.Engine do
|
||||
|
||||
All other markers are not implemented by this engine.
|
||||
"""
|
||||
@spec handle_expr(Macro.t, String.t, Macro.t) :: Macro.t
|
||||
def handle_expr(buffer, "=", expr) do
|
||||
quote do
|
||||
tmp1 = unquote(buffer)
|
||||
|
||||
@@ -1,39 +1,31 @@
|
||||
defmodule EEx.Tokenizer do
|
||||
@moduledoc false
|
||||
|
||||
@type content :: IO.chardata
|
||||
@type line :: non_neg_integer
|
||||
@type token :: {:text, content} |
|
||||
{:expr | :start_expr | :middle_expr | :end_expr, line, '=' | '', content}
|
||||
|
||||
@doc """
|
||||
Tokenizes the given charlist or binary.
|
||||
Tokenizes the given char list or binary.
|
||||
|
||||
It returns {:ok, list} with the following tokens:
|
||||
|
||||
* `{:text, content}`
|
||||
* `{:expr, line, marker, content}`
|
||||
* `{:start_expr, line, marker, content}`
|
||||
* `{:middle_expr, line, marker, content}`
|
||||
* `{:end_expr, line, marker, content}`
|
||||
* `{:text, contents}`
|
||||
* `{:expr, line, marker, contents}`
|
||||
* `{:start_expr, line, marker, contents}`
|
||||
* `{:middle_expr, line, marker, contents}`
|
||||
* `{:end_expr, line, marker, contents}`
|
||||
|
||||
Or `{:error, line, error}` in case of errors.
|
||||
"""
|
||||
@spec tokenize(binary | charlist, line, Keyword.t) :: {:ok, [token]} | {:error, line, String.t}
|
||||
def tokenize(bin, line, opts \\ [])
|
||||
|
||||
def tokenize(bin, line, opts)
|
||||
when is_binary(bin) and is_integer(line) and line >= 0 and is_list(opts) do
|
||||
tokenize(String.to_charlist(bin), line, opts)
|
||||
def tokenize(bin, line, opts) when is_binary(bin) do
|
||||
tokenize(String.to_char_list(bin), line, opts)
|
||||
end
|
||||
|
||||
def tokenize(list, line, opts)
|
||||
when is_list(list) and is_integer(line) and line >= 0 and is_list(opts) do
|
||||
def tokenize(list, line, opts) do
|
||||
tokenize(list, line, opts, [], [])
|
||||
end
|
||||
|
||||
defp tokenize('<%%' ++ t, line, opts, buffer, acc) do
|
||||
tokenize t, line, opts, [?%, ?< | buffer], acc
|
||||
tokenize t, line, opts, [?%, ?<|buffer], acc
|
||||
end
|
||||
|
||||
defp tokenize('<%#' ++ t, line, opts, buffer, acc) do
|
||||
@@ -60,11 +52,11 @@ defmodule EEx.Tokenizer do
|
||||
end
|
||||
|
||||
defp tokenize('\n' ++ t, line, opts, buffer, acc) do
|
||||
tokenize t, line + 1, opts, [?\n | buffer], acc
|
||||
tokenize t, line + 1, opts, [?\n|buffer], acc
|
||||
end
|
||||
|
||||
defp tokenize([h | t], line, opts, buffer, acc) do
|
||||
tokenize t, line, opts, [h | buffer], acc
|
||||
defp tokenize([h|t], line, opts, buffer, acc) do
|
||||
tokenize t, line, opts, [h|buffer], acc
|
||||
end
|
||||
|
||||
defp tokenize([], _line, _opts, buffer, acc) do
|
||||
@@ -83,16 +75,16 @@ defmodule EEx.Tokenizer do
|
||||
|
||||
# Tokenize an expression until we find %>
|
||||
|
||||
defp expr([?%, ?> | t], line, buffer) do
|
||||
defp expr([?%, ?>|t], line, buffer) do
|
||||
{:ok, buffer, line, t}
|
||||
end
|
||||
|
||||
defp expr('\n' ++ t, line, buffer) do
|
||||
expr t, line + 1, [?\n | buffer]
|
||||
expr t, line + 1, [?\n|buffer]
|
||||
end
|
||||
|
||||
defp expr([h | t], line, buffer) do
|
||||
expr t, line, [h | buffer]
|
||||
defp expr([h|t], line, buffer) do
|
||||
expr t, line, [h|buffer]
|
||||
end
|
||||
|
||||
defp expr([], line, _buffer) do
|
||||
@@ -106,11 +98,11 @@ defmodule EEx.Tokenizer do
|
||||
# Middle tokens are marked with "->" or keywords
|
||||
# End tokens contain only the end word and optionally ")"
|
||||
|
||||
defp token_name([h | t]) when h in [?\s, ?\t, ?)] do
|
||||
defp token_name([h|t]) when h in [?\s, ?\t, ?)] do
|
||||
token_name(t)
|
||||
end
|
||||
|
||||
defp token_name('od' ++ [h | _]) when h in [?\s, ?\t, ?)] do
|
||||
defp token_name('od' ++ [h|_]) when h in [?\s, ?\t, ?)] do
|
||||
:start_expr
|
||||
end
|
||||
|
||||
@@ -151,7 +143,7 @@ defmodule EEx.Tokenizer do
|
||||
Enum.find_index tokens, fn
|
||||
{:fn_paren, _} -> true
|
||||
{:fn, _} -> true
|
||||
_ -> false
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -181,6 +173,7 @@ defmodule EEx.Tokenizer do
|
||||
# If trim mode is enabled and the token is on a line with
|
||||
# only itself and whitespace, trim the whitespace around it,
|
||||
# including the line break following it if there is one.
|
||||
|
||||
defp trim_if_needed(rest, line, opts, buffer, acc) do
|
||||
original = {rest, line, buffer}
|
||||
if opts[:trim] do
|
||||
@@ -197,7 +190,7 @@ defmodule EEx.Tokenizer do
|
||||
|
||||
defp trim_left(buffer, acc) do
|
||||
case {trim_whitespace(buffer), acc} do
|
||||
{[?\n | _] = trimmed_buffer, _} -> {true, trimmed_buffer}
|
||||
{[?\n|_] = trimmed_buffer, _} -> {true, trimmed_buffer}
|
||||
{[], []} -> {true, []}
|
||||
_ -> {false, buffer}
|
||||
end
|
||||
@@ -205,14 +198,14 @@ defmodule EEx.Tokenizer do
|
||||
|
||||
defp trim_right(rest, line) do
|
||||
case trim_whitespace(rest) do
|
||||
[?\r, ?\n | trimmed_rest] -> {true, trimmed_rest, line + 1}
|
||||
[?\n | trimmed_rest] -> {true, trimmed_rest, line + 1}
|
||||
[?\r, ?\n|trimmed_rest] -> {true, trimmed_rest, line + 1}
|
||||
[?\n|trimmed_rest] -> {true, trimmed_rest, line + 1}
|
||||
[] -> {true, [], line}
|
||||
_ -> {false, rest, line}
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_whitespace([h | t]) when h == ?\s or h == ?\t do
|
||||
defp trim_whitespace([h|t]) when h == ?\s or h == ?\t do
|
||||
trim_whitespace(t)
|
||||
end
|
||||
|
||||
|
||||
@@ -3,8 +3,6 @@ Code.require_file "../test_helper.exs", __DIR__
|
||||
defmodule EEx.SmartEngineTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
import ExUnit.CaptureIO
|
||||
|
||||
test "evaluates simple string" do
|
||||
assert_eval "foo bar", "foo bar"
|
||||
end
|
||||
@@ -17,13 +15,6 @@ defmodule EEx.SmartEngineTest do
|
||||
assert_eval "1", "<%= @foo %>", assigns: %{foo: 1}
|
||||
end
|
||||
|
||||
test "error with missing assigns" do
|
||||
stderr = capture_io(:stderr, fn ->
|
||||
assert_eval "", "<%= @foo %>", assigns: %{}
|
||||
end)
|
||||
assert stderr =~ "assign @foo not available in EEx template"
|
||||
end
|
||||
|
||||
test "evaluates with loops" do
|
||||
assert_eval "1\n2\n3\n", "<%= for x <- [1, 2, 3] do %><%= x %>\n<% end %>"
|
||||
end
|
||||
|
||||
@@ -158,6 +158,5 @@ baz %>
|
||||
|
||||
test "raise syntax error when there is start mark and no end mark" do
|
||||
assert T.tokenize('foo <% :bar', 1) == {:error, 1, "missing token '%>'"}
|
||||
assert T.tokenize('<%# true ', 1) == {:error, 1, "missing token '%>'"}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ require EEx
|
||||
|
||||
defmodule EExTest.Compiled do
|
||||
def before_compile do
|
||||
fill_in_stacktrace()
|
||||
fill_in_stacktrace
|
||||
{__ENV__.line, hd(tl(System.stacktrace))}
|
||||
end
|
||||
|
||||
@@ -19,13 +19,13 @@ defmodule EExTest.Compiled do
|
||||
def file_sample(arg), do: private_file_sample(arg)
|
||||
|
||||
def after_compile do
|
||||
fill_in_stacktrace()
|
||||
fill_in_stacktrace
|
||||
{__ENV__.line, hd(tl(System.stacktrace))}
|
||||
end
|
||||
|
||||
@file "unknown"
|
||||
def unknown do
|
||||
fill_in_stacktrace()
|
||||
fill_in_stacktrace
|
||||
{__ENV__.line, hd(tl(System.stacktrace))}
|
||||
end
|
||||
|
||||
@@ -99,7 +99,7 @@ defmodule EExTest do
|
||||
end
|
||||
|
||||
test "evaluates with parentheses after end in end token" do
|
||||
assert_eval " 101 102 103 ", "<%= Enum.map([1, 2, 3], (fn x -> %> <%= 100 + x %> <% end) ) %>"
|
||||
assert_eval " 101 102 103 ", "<%= Enum.map([1,2,3], (fn x -> %> <%= 100 + x %> <% end) ) %>"
|
||||
end
|
||||
|
||||
test "evaluates with defined variable" do
|
||||
@@ -316,7 +316,7 @@ foo
|
||||
assert_eval "\ndone\n", string, packages: nil, all: nil
|
||||
end
|
||||
|
||||
test "Unicode" do
|
||||
test "unicode" do
|
||||
template = """
|
||||
• <%= "•" %> •
|
||||
<%= "Jößé Vâlìm" %> Jößé Vâlìm
|
||||
@@ -331,19 +331,6 @@ foo
|
||||
assert_eval expected, string, [], trim: true
|
||||
end
|
||||
|
||||
test "trim mode with middle expression" do
|
||||
string = """
|
||||
<%= cond do %>
|
||||
<% false -> %>
|
||||
this
|
||||
<% true -> %>
|
||||
that
|
||||
<% end %>
|
||||
"""
|
||||
expected = " that\n"
|
||||
assert_eval expected, string, [], trim: true
|
||||
end
|
||||
|
||||
test "evaluates the source from a given file" do
|
||||
filename = Path.join(__DIR__, "fixtures/eex_template.eex")
|
||||
result = EEx.eval_file(filename)
|
||||
@@ -357,7 +344,7 @@ foo
|
||||
end
|
||||
|
||||
test "raises an Exception when there's an error with the given file" do
|
||||
assert_raise File.Error, "could not read file \"non-existent.eex\": no such file or directory", fn ->
|
||||
assert_raise File.Error, "could not read file non-existent.eex: no such file or directory", fn ->
|
||||
filename = "non-existent.eex"
|
||||
EEx.compile_file(filename)
|
||||
end
|
||||
@@ -383,7 +370,7 @@ foo
|
||||
{EExTest.Compiled,
|
||||
:before_compile,
|
||||
0,
|
||||
[file: to_charlist(Path.relative_to_cwd(__ENV__.file)), line: 7]
|
||||
[file: to_char_list(Path.relative_to_cwd(__ENV__.file)), line: 7]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -392,7 +379,7 @@ foo
|
||||
{EExTest.Compiled,
|
||||
:after_compile,
|
||||
0,
|
||||
[file: to_charlist(Path.relative_to_cwd(__ENV__.file)), line: 22]
|
||||
[file: to_char_list(Path.relative_to_cwd(__ENV__.file)), line: 22]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -409,10 +396,6 @@ foo
|
||||
defmodule TestEngine do
|
||||
@behaviour EEx.Engine
|
||||
|
||||
def init(_opts) do
|
||||
""
|
||||
end
|
||||
|
||||
def handle_body(body) do
|
||||
{:wrapped, body}
|
||||
end
|
||||
|
||||
@@ -1 +1 @@
|
||||
ExUnit.start [trace: "--trace" in System.argv]
|
||||
ExUnit.start [trace: "--trace" in System.argv]
|
||||
+54
-516
@@ -1,6 +1,6 @@
|
||||
defmodule Access do
|
||||
@moduledoc """
|
||||
Key-based access to data structures using the `data[key]` syntax.
|
||||
Key-based access to data structures via the `foo[bar]` syntax.
|
||||
|
||||
Elixir provides two syntaxes for accessing values. `user[:name]`
|
||||
is used by dynamic structures, like maps and keywords, while
|
||||
@@ -8,16 +8,9 @@ defmodule Access do
|
||||
`user[:name]` won't raise if the key `:name` is missing but
|
||||
`user.name` will raise if there is no `:name` key.
|
||||
|
||||
Besides the cases above, this module provides convenience
|
||||
functions for accessing other structures, like `at/1` for
|
||||
lists and `elem/1` for tuples. Those functions can be used
|
||||
by the nested update functions in `Kernel`, such as
|
||||
`Kernel.get_in/2`, `Kernel.put_in/3`, `Kernel.update_in/3`,
|
||||
`Kernel.get_and_update_in/3` and friends.
|
||||
## Key-based lookups
|
||||
|
||||
## Dynamic lookups
|
||||
|
||||
Out of the box, `Access` works with `Keyword` and `Map`:
|
||||
Out of the box, Access works with `Keyword` and `Map`:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:a]
|
||||
@@ -31,10 +24,7 @@ defmodule Access do
|
||||
iex> star_ratings[1.5]
|
||||
"★☆"
|
||||
|
||||
Note that the dynamic lookup syntax (`term[key]`) roughly translates to
|
||||
`Access.get(term, key, nil)`.
|
||||
|
||||
`Access` can be combined with `Kernel.put_in/3` to put a value
|
||||
Access can be combined with `Kernel.put_in/3` to put a value
|
||||
in a given key:
|
||||
|
||||
iex> map = %{a: 1, b: 2}
|
||||
@@ -47,51 +37,46 @@ defmodule Access do
|
||||
iex> put_in users["john"][:age], 28
|
||||
%{"john" => %{age: 28}, "meg" => %{age: 23}}
|
||||
|
||||
Furthermore, `Access` transparently ignores `nil` values:
|
||||
Furthermore, Access transparently ignores `nil` values:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:c][:unknown]
|
||||
nil
|
||||
|
||||
Since `Access` is a behaviour, it can be implemented for key-value
|
||||
data structures. The implementation should be added to the
|
||||
module that defines the struct being accessed. `Access` requires the
|
||||
key comparison to be implemented using the `===` operator.
|
||||
Since Access is a behaviour, it can be implemented to key-value
|
||||
data structures. Access requires the key comparison to be
|
||||
implemented using the `===` operator.
|
||||
|
||||
## Static lookups
|
||||
## Field-based lookups
|
||||
|
||||
The `Access` syntax (`foo[bar]`) cannot be used to access fields in
|
||||
structs, since structs do not implement the `Access` behaviour by
|
||||
default. It is also a design decision: the dynamic access lookup
|
||||
is meant to be used for dynamic key-value structures, like maps
|
||||
and keywords, and not by static ones like structs (where fields are
|
||||
known and not dynamic).
|
||||
The Access syntax (`foo[bar]`) cannot be used to access fields in
|
||||
structs. That's by design, as Access is meant to be used for
|
||||
dynamic key-value structures, like maps and keywords, and not
|
||||
by static ones like structs.
|
||||
|
||||
Therefore Elixir provides a static lookup for struct fields and for atom
|
||||
fields in maps. Imagine a struct named `User` with a `:name` field.
|
||||
The following would raise:
|
||||
However Elixir already provides a field-based lookup for structs.
|
||||
Imagine a struct named `User` with name and age fields. The
|
||||
following would raise:
|
||||
|
||||
user = %User{name: "John"}
|
||||
user = %User{name: "john"}
|
||||
user[:name]
|
||||
# ** (UndefinedFunctionError) undefined function User.fetch/2
|
||||
# (User does not implement the Access behaviour)
|
||||
** (UndefinedFunctionError) undefined function User.fetch/2
|
||||
(User does not implement the Access behaviour)
|
||||
|
||||
Structs instead use the `user.name` syntax to access fields:
|
||||
Structs instead use the `user.name` syntax:
|
||||
|
||||
user.name
|
||||
#=> "John"
|
||||
#=> "john"
|
||||
|
||||
The same `user.name` syntax can also be used by `Kernel.put_in/2`
|
||||
to for updating structs fields:
|
||||
|
||||
put_in user.name, "Mary"
|
||||
#=> %User{name: "Mary"}
|
||||
put_in user.name, "mary"
|
||||
%User{name: "mary"}
|
||||
|
||||
Differently from `user[:name]`, `user.name` is not extensible via
|
||||
a behaviour and is restricted only to structs and atom keys in maps.
|
||||
|
||||
As mentioned above, this works for atom keys in maps as well. Refer to the
|
||||
`Map` module for more information on this.
|
||||
Differently from `user[:name]`, `user.name` cannot be extended by
|
||||
the developers, and will be always restricted to only maps and
|
||||
structs.
|
||||
|
||||
Summing up:
|
||||
|
||||
@@ -100,127 +85,21 @@ defmodule Access do
|
||||
* `user.name` is used by static structures, it is not extensible
|
||||
and it will raise on missing keys
|
||||
|
||||
## Accessors
|
||||
|
||||
While Elixir provides built-in syntax only for traversing dynamic
|
||||
and static key-value structures, this module provides convenience
|
||||
functions for traversing other structures, like tuples and lists,
|
||||
to be used alongside `Kernel.put_in/2` in others.
|
||||
|
||||
For instance, given a user with a list of languages, here is how to
|
||||
deeply traverse the map and convert all language names to uppercase:
|
||||
|
||||
iex> user = %{name: "john",
|
||||
...> languages: [%{name: "elixir", type: :functional},
|
||||
...> %{name: "c", type: :procedural}]}
|
||||
iex> update_in user, [:languages, Access.all(), :name], &String.upcase/1
|
||||
%{name: "john",
|
||||
languages: [%{name: "ELIXIR", type: :functional},
|
||||
%{name: "C", type: :procedural}]}
|
||||
|
||||
See the functions `key/1`, `key!/1`, `elem/1`, and `all/0` for some of the
|
||||
available accessors.
|
||||
|
||||
## Implementing the Access behaviour for custom data structures
|
||||
|
||||
In order to be able to use the `Access` protocol with custom data structures
|
||||
(which have to be structs), such structures have to implement the `Access`
|
||||
behaviour. For example, for a `User` struct, this would have to be done:
|
||||
|
||||
defmodule User do
|
||||
defstruct [:name, :email]
|
||||
|
||||
@behaviour Access
|
||||
# Implementation of the Access callbacks...
|
||||
end
|
||||
|
||||
"""
|
||||
|
||||
@type t :: list | map | nil | any
|
||||
@type t :: list | map | nil
|
||||
@type key :: any
|
||||
@type value :: any
|
||||
|
||||
@doc """
|
||||
Invoked in order to access the value stored under `key` in the given term `term`.
|
||||
|
||||
This function should return `{:ok, value}` where `value` is the value under
|
||||
`key` if it succeeded, or `:error` if the key does not exist in the structure.
|
||||
|
||||
Many of the functions defined in the `Access` module internally call this
|
||||
function. This function is also used when the square-brackets access syntax
|
||||
(`structure[key]`) is used: the `fetch/2` callback implemented by the module
|
||||
that defines the `structure` struct is invoked and if it returns `{:ok,
|
||||
value}` then `value` is returned, or if it returns `:error` then `nil` is
|
||||
returned.
|
||||
|
||||
|
||||
See the `Map.fetch/2` and `Keyword.fetch/2` implementations for examples of
|
||||
how to implement this callback.
|
||||
"""
|
||||
@callback fetch(term :: t, key) :: {:ok, value} | :error
|
||||
|
||||
@doc """
|
||||
Invoked in order to access the value stored under `key` in the given term `term`,
|
||||
defaulting to `default` if not present.
|
||||
|
||||
This function should return the value under the key `key` in `term` if there's
|
||||
such key, otherwise `default`.
|
||||
|
||||
For most data structures, this can be implemented using `fetch/2` internally;
|
||||
for example:
|
||||
|
||||
def get(structure, key, default) do
|
||||
case fetch(structure, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> default
|
||||
end
|
||||
end
|
||||
|
||||
See the `Map.get/3` and `Keyword.get/3` implementations for more examples.
|
||||
"""
|
||||
@callback get(term :: t, key, default :: value) :: value
|
||||
|
||||
@doc """
|
||||
Invoked in order to access the value under `key` and update it at the same time.
|
||||
|
||||
The implementation of this callback should invoke the passed function with the
|
||||
value under key `key` in the passed structure, or `nil` if the key is not
|
||||
present. This function should return either `{value_to_return, new_value}` or
|
||||
`:pop`.
|
||||
|
||||
If it returns `{value_to_return, new_value}`, the return value of this
|
||||
callback should be `{value_to_return, new_term}` where `new_term` is `term`
|
||||
after updating the value of `key` with `new_value`.
|
||||
|
||||
If it returns `:pop`, the return value of this callback should be `{value,
|
||||
new_term}` where `value` is the value under `key` or `nil` if not present, and
|
||||
`new_term` is `term` without the key `key`.
|
||||
|
||||
See the implementations of `Map.get_and_update/3` or `Keyword.get_and_update/3`
|
||||
for more examples.
|
||||
"""
|
||||
@callback get_and_update(term :: t, key, (value -> {value, value} | :pop)) :: {value, t}
|
||||
|
||||
@doc """
|
||||
Invoked to "pop" the value under `key` out of the given term.
|
||||
|
||||
When the key `key` exists in the given `term`, the implementation should
|
||||
return a `{value, new_term}` tuple where `value` is the value that was under
|
||||
`key` and `new_term` is `term` without `key`.
|
||||
|
||||
When the key `key` is not present in the given `term`, a tuple `{value, term}`
|
||||
should be returned, where `value` is implementation-defined.
|
||||
|
||||
See the implementations for `Map.pop/3` or `Keyword.pop/3` for more examples.
|
||||
"""
|
||||
@callback pop(term :: t, key) :: {value, t}
|
||||
@callback fetch(t, key) :: {:ok, value} | :error
|
||||
@callback get_and_update(t, key, (value -> {value, value})) :: {value, t}
|
||||
|
||||
defmacrop raise_undefined_behaviour(e, struct, top) do
|
||||
quote do
|
||||
stacktrace = System.stacktrace
|
||||
e =
|
||||
case stacktrace do
|
||||
[unquote(top) | _] ->
|
||||
[unquote(top)|_] ->
|
||||
%{unquote(e) | reason: "#{inspect unquote(struct)} does not implement the Access behaviour"}
|
||||
_ ->
|
||||
unquote(e)
|
||||
@@ -230,8 +109,7 @@ defmodule Access do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Fetches the value for the given key in a container (a map, keyword
|
||||
list, or struct that implements the `Access` behaviour).
|
||||
Fetches the container's value for the given key.
|
||||
"""
|
||||
@spec fetch(t, term) :: {:ok, term} | :error
|
||||
def fetch(container, key)
|
||||
@@ -243,12 +121,15 @@ defmodule Access do
|
||||
raise_undefined_behaviour e, struct, {^struct, :fetch, [^container, ^key], _}
|
||||
end
|
||||
|
||||
def fetch(map, key) when is_map(map) do
|
||||
Map.fetch(map, key)
|
||||
def fetch(%{} = map, key) do
|
||||
:maps.find(key, map)
|
||||
end
|
||||
|
||||
def fetch(list, key) when is_list(list) and is_atom(key) do
|
||||
Keyword.fetch(list, key)
|
||||
case :lists.keyfind(key, 1, list) do
|
||||
{^key, value} -> {:ok, value}
|
||||
false -> :error
|
||||
end
|
||||
end
|
||||
|
||||
def fetch(list, key) when is_list(list) do
|
||||
@@ -261,8 +142,7 @@ defmodule Access do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value for the given key in a container (a map, keyword
|
||||
list, or struct that implements the `Access` behaviour).
|
||||
Gets the container's value for the given key.
|
||||
"""
|
||||
@spec get(t, term, term) :: term
|
||||
def get(container, key, default \\ nil) do
|
||||
@@ -273,21 +153,17 @@ defmodule Access do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets and updates the given key in a container (a map, keyword
|
||||
list, or struct that implements the `Access` behaviour).
|
||||
Gets and updates the container's value for the given key, in a single pass.
|
||||
|
||||
This `fun` argument receives the value of `key` (or `nil` if `key`
|
||||
is not present) and must return a two-element tuple: the "get" value
|
||||
(the retrieved value, which can be operated on before being returned)
|
||||
and the new value to be stored under `key`. The `fun` may also
|
||||
return `:pop`, implying the current value shall be removed
|
||||
from the container and returned.
|
||||
The argument function `fun` must receive the value for the given `key` (or
|
||||
`nil` if the key doesn't exist in `container`). It must return a tuple
|
||||
containing the `get` value and the new value to be stored in the `container`.
|
||||
|
||||
The returned value is a two-element tuple with the "get" value returned by
|
||||
`fun` and a new container with the updated value under `key`.
|
||||
This function returns a two-element tuple.
|
||||
The first element is the `get` value, as returned by `fun`.
|
||||
The second element is the container, updated with the value returned by `fun`.
|
||||
"""
|
||||
@spec get_and_update(container :: t, key, (value -> {get_value, update_value} | :pop)) ::
|
||||
{get_value, container :: t} when get_value: var, update_value: value
|
||||
@spec get_and_update(t, term, (term -> {get, term})) :: {get, t} when get: var
|
||||
def get_and_update(container, key, fun)
|
||||
|
||||
def get_and_update(%{__struct__: struct} = container, key, fun) do
|
||||
@@ -297,8 +173,14 @@ defmodule Access do
|
||||
raise_undefined_behaviour e, struct, {^struct, :get_and_update, [^container, ^key, ^fun], _}
|
||||
end
|
||||
|
||||
def get_and_update(map, key, fun) when is_map(map) do
|
||||
Map.get_and_update(map, key, fun)
|
||||
def get_and_update(%{} = map, key, fun) do
|
||||
current_value = case :maps.find(key, map) do
|
||||
{:ok, value} -> value
|
||||
:error -> nil
|
||||
end
|
||||
|
||||
{get, update} = fun.(current_value)
|
||||
{get, :maps.put(key, update, map)}
|
||||
end
|
||||
|
||||
def get_and_update(list, key, fun) when is_list(list) do
|
||||
@@ -309,348 +191,4 @@ defmodule Access do
|
||||
raise ArgumentError,
|
||||
"could not put/update key #{inspect key} on a nil value"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes the entry with a given key from a container (a map, keyword
|
||||
list, or struct that implements the `Access` behaviour).
|
||||
|
||||
Returns a tuple containing the value associated with the key and the
|
||||
updated container. `nil` is returned for the value if the key isn't
|
||||
in the container.
|
||||
|
||||
## Examples
|
||||
|
||||
With a map:
|
||||
|
||||
iex> Access.pop(%{name: "Elixir", creator: "Valim"}, :name)
|
||||
{"Elixir", %{creator: "Valim"}}
|
||||
|
||||
A keyword list:
|
||||
|
||||
iex> Access.pop([name: "Elixir", creator: "Valim"], :name)
|
||||
{"Elixir", [creator: "Valim"]}
|
||||
|
||||
An unknown key:
|
||||
|
||||
iex> Access.pop(%{name: "Elixir", creator: "Valim"}, :year)
|
||||
{nil, %{creator: "Valim", name: "Elixir"}}
|
||||
|
||||
"""
|
||||
def pop(%{__struct__: struct} = container, key) do
|
||||
struct.pop(container, key)
|
||||
rescue
|
||||
e in UndefinedFunctionError ->
|
||||
raise_undefined_behaviour e, struct, {^struct, :pop, [^container, ^key], _}
|
||||
end
|
||||
|
||||
def pop(map, key) when is_map(map) do
|
||||
Map.pop(map, key)
|
||||
end
|
||||
|
||||
def pop(list, key) when is_list(list) do
|
||||
Keyword.pop(list, key)
|
||||
end
|
||||
|
||||
def pop(nil, key) do
|
||||
raise ArgumentError,
|
||||
"could not pop key #{inspect key} on a nil value"
|
||||
end
|
||||
|
||||
## Accessors
|
||||
|
||||
@doc false
|
||||
def key(key) do
|
||||
IO.warn "Access.key/1 is deprecated due to erratic behaviour for missing keys, " <>
|
||||
"please use Access.key/2 instead with proper default values " <>
|
||||
"(or Access.key!/1 if you expect the key to always be available)"
|
||||
|
||||
fn
|
||||
:get, data, next ->
|
||||
next.(Map.get(to_map(data), key))
|
||||
:get_and_update, data, next ->
|
||||
value = Map.get(to_map(data), key)
|
||||
case next.(value) do
|
||||
{get, update} -> {get, Map.put(data, key, update)}
|
||||
:pop -> {value, Map.delete(data, key)}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp to_map(nil), do: %{}
|
||||
defp to_map(%{} = map), do: map
|
||||
defp to_map(data), do: raise "Access.key/1 expected a map/struct or nil, got: #{inspect data}"
|
||||
|
||||
@doc """
|
||||
Returns a function that accesses the given key in a map/struct.
|
||||
|
||||
The returned function is typically passed as an accessor to `Kernel.get_in/2`,
|
||||
`Kernel.get_and_update_in/3`, and friends.
|
||||
|
||||
The returned function uses the default value if the key does not exist.
|
||||
This can be used to specify defaults and safely traverse missing keys:
|
||||
|
||||
iex> get_in(%{}, [Access.key(:user, %{}), Access.key(:name, nil)])
|
||||
nil
|
||||
|
||||
Such is also useful when using update functions, allowing us to introduce
|
||||
values as we traverse the data-structure for updates:
|
||||
|
||||
iex> put_in(%{}, [Access.key(:user, %{}), Access.key(:name, nil)], "Mary")
|
||||
%{user: %{name: "Mary"}}
|
||||
|
||||
## Examples
|
||||
|
||||
iex> map = %{user: %{name: "john"}}
|
||||
iex> get_in(map, [Access.key(:unknown, %{}), Access.key(:name, "john")])
|
||||
"john"
|
||||
iex> get_and_update_in(map, [Access.key!(:user), Access.key!(:name)], fn
|
||||
...> prev -> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{"john", %{user: %{name: "JOHN"}}}
|
||||
iex> pop_in(map, [Access.key!(:user), Access.key!(:name)])
|
||||
{"john", %{user: %{}}}
|
||||
|
||||
An error is raised if the accessed structure is not a map or a struct:
|
||||
|
||||
iex> get_in(nil, [Access.key(:foo, nil)])
|
||||
** (BadMapError) expected a map, got: nil
|
||||
|
||||
iex> get_in([], [Access.key(:foo, nil)])
|
||||
** (BadMapError) expected a map, got: []
|
||||
|
||||
"""
|
||||
def key(key, default) do
|
||||
fn
|
||||
:get, data, next ->
|
||||
next.(Map.get(data, key, default))
|
||||
:get_and_update, data, next ->
|
||||
value = Map.get(data, key, default)
|
||||
case next.(value) do
|
||||
{get, update} -> {get, Map.put(data, key, update)}
|
||||
:pop -> {value, Map.delete(data, key)}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a function that accesses the given key in a map/struct.
|
||||
|
||||
The returned function is typically passed as an accessor to `Kernel.get_in/2`,
|
||||
`Kernel.get_and_update_in/3`, and friends.
|
||||
|
||||
Raises if the key does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> map = %{user: %{name: "john"}}
|
||||
iex> get_in(map, [Access.key!(:user), Access.key!(:name)])
|
||||
"john"
|
||||
iex> get_and_update_in(map, [Access.key!(:user), Access.key!(:name)], fn
|
||||
...> prev -> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{"john", %{user: %{name: "JOHN"}}}
|
||||
iex> pop_in(map, [Access.key!(:user), Access.key!(:name)])
|
||||
{"john", %{user: %{}}}
|
||||
iex> get_in(map, [Access.key!(:user), Access.key!(:unknown)])
|
||||
** (KeyError) key :unknown not found in: %{name: \"john\"}
|
||||
|
||||
An error is raised if the accessed structure is not a map/struct:
|
||||
|
||||
iex> get_in([], [Access.key!(:foo)])
|
||||
** (RuntimeError) Access.key!/1 expected a map/struct, got: []
|
||||
|
||||
"""
|
||||
def key!(key) do
|
||||
fn
|
||||
:get, %{} = data, next ->
|
||||
next.(Map.fetch!(data, key))
|
||||
:get_and_update, %{} = data, next ->
|
||||
value = Map.fetch!(data, key)
|
||||
case next.(value) do
|
||||
{get, update} -> {get, Map.put(data, key, update)}
|
||||
:pop -> {value, Map.delete(data, key)}
|
||||
end
|
||||
_op, data, _next ->
|
||||
raise "Access.key!/1 expected a map/struct, got: #{inspect data}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Returns a function that accesses the element at the given index in a tuple.
|
||||
|
||||
The returned function is typically passed as an accessor to `Kernel.get_in/2`,
|
||||
`Kernel.get_and_update_in/3`, and friends.
|
||||
|
||||
Raises if the index is out of bounds.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> map = %{user: {"john", 27}}
|
||||
iex> get_in(map, [:user, Access.elem(0)])
|
||||
"john"
|
||||
iex> get_and_update_in(map, [:user, Access.elem(0)], fn
|
||||
...> prev -> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{"john", %{user: {"JOHN", 27}}}
|
||||
iex> pop_in(map, [:user, Access.elem(0)])
|
||||
** (RuntimeError) cannot pop data from a tuple
|
||||
|
||||
An error is raised if the accessed structure is not a tuple:
|
||||
|
||||
iex> get_in(%{}, [Access.elem(0)])
|
||||
** (RuntimeError) Access.elem/1 expected a tuple, got: %{}
|
||||
|
||||
"""
|
||||
def elem(index) when is_integer(index) do
|
||||
pos = index + 1
|
||||
|
||||
fn
|
||||
:get, data, next when is_tuple(data) ->
|
||||
next.(:erlang.element(pos, data))
|
||||
:get_and_update, data, next when is_tuple(data) ->
|
||||
value = :erlang.element(pos, data)
|
||||
case next.(value) do
|
||||
{get, update} -> {get, :erlang.setelement(pos, data, update)}
|
||||
:pop -> raise "cannot pop data from a tuple"
|
||||
end
|
||||
_op, data, _next ->
|
||||
raise "Access.elem/1 expected a tuple, got: #{inspect data}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Returns a function that accesses all the elements in a list.
|
||||
|
||||
The returned function is typically passed as an accessor to `Kernel.get_in/2`,
|
||||
`Kernel.get_and_update_in/3`, and friends.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list = [%{name: "john"}, %{name: "mary"}]
|
||||
iex> get_in(list, [Access.all(), :name])
|
||||
["john", "mary"]
|
||||
iex> get_and_update_in(list, [Access.all(), :name], fn
|
||||
...> prev -> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{["john", "mary"], [%{name: "JOHN"}, %{name: "MARY"}]}
|
||||
iex> pop_in(list, [Access.all(), :name])
|
||||
{["john", "mary"], [%{}, %{}]}
|
||||
|
||||
Here is an example that traverses the list dropping even
|
||||
numbers and multipling odd numbers by 2:
|
||||
|
||||
iex> require Integer
|
||||
iex> get_and_update_in([1, 2, 3, 4, 5], [Access.all], fn
|
||||
...> num -> if Integer.is_even(num), do: :pop, else: {num, num * 2}
|
||||
...> end)
|
||||
{[1, 2, 3, 4, 5], [2, 6, 10]}
|
||||
|
||||
An error is raised if the accessed structure is not a list:
|
||||
|
||||
iex> get_in(%{}, [Access.all()])
|
||||
** (RuntimeError) Access.all/0 expected a list, got: %{}
|
||||
|
||||
"""
|
||||
def all() do
|
||||
&all/3
|
||||
end
|
||||
|
||||
defp all(:get, data, next) when is_list(data) do
|
||||
Enum.map(data, next)
|
||||
end
|
||||
|
||||
defp all(:get_and_update, data, next) when is_list(data) do
|
||||
all(data, next, [], [])
|
||||
end
|
||||
|
||||
defp all(_op, data, _next) do
|
||||
raise "Access.all/0 expected a list, got: #{inspect data}"
|
||||
end
|
||||
|
||||
defp all([head | rest], next, gets, updates) do
|
||||
case next.(head) do
|
||||
{get, update} -> all(rest, next, [get | gets], [update | updates])
|
||||
:pop -> all(rest, next, [head | gets], updates)
|
||||
end
|
||||
end
|
||||
|
||||
defp all([], _next, gets, updates) do
|
||||
{:lists.reverse(gets), :lists.reverse(updates)}
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Returns a function that accesses the element at `index` (zero based) of a list.
|
||||
|
||||
The returned function is typically passed as an accessor to `Kernel.get_in/2`,
|
||||
`Kernel.get_and_update_in/3`, and friends.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list = [%{name: "john"}, %{name: "mary"}]
|
||||
iex> get_in(list, [Access.at(1), :name])
|
||||
"mary"
|
||||
iex> get_and_update_in(list, [Access.at(0), :name], fn
|
||||
...> prev -> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{"john", [%{name: "JOHN"}, %{name: "mary"}]}
|
||||
|
||||
`at/1` can also be used to pop elements out of a list or
|
||||
a key inside of a list:
|
||||
|
||||
iex> list = [%{name: "john"}, %{name: "mary"}]
|
||||
iex> pop_in(list, [Access.at(0)])
|
||||
{%{name: "john"}, [%{name: "mary"}]}
|
||||
iex> pop_in(list, [Access.at(0), :name])
|
||||
{"john", [%{}, %{name: "mary"}]}
|
||||
|
||||
When the index is out of bounds, `nil` is returned and the update function is never called:
|
||||
|
||||
iex> list = [%{name: "john"}, %{name: "mary"}]
|
||||
iex> get_in(list, [Access.at(10), :name])
|
||||
nil
|
||||
iex> get_and_update_in(list, [Access.at(10), :name], fn
|
||||
...> prev -> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{nil, [%{name: "john"}, %{name: "mary"}]}
|
||||
|
||||
An error is raised for negative indexes:
|
||||
|
||||
iex> get_in([], [Access.at(-1)])
|
||||
** (FunctionClauseError) no function clause matching in Access.at/1
|
||||
|
||||
An error is raised if the accessed structure is not a list:
|
||||
|
||||
iex> get_in(%{}, [Access.at(1)])
|
||||
** (RuntimeError) Access.at/1 expected a list, got: %{}
|
||||
"""
|
||||
def at(index) when index >= 0 do
|
||||
fn(op, data, next) -> at(op, data, index, next) end
|
||||
end
|
||||
|
||||
defp at(:get, data, index, next) when is_list(data) do
|
||||
data |> Enum.at(index) |> next.()
|
||||
end
|
||||
|
||||
defp at(:get_and_update, data, index, next) when is_list(data) do
|
||||
get_and_update_at(data, index, next, [])
|
||||
end
|
||||
|
||||
defp at(_op, data, _index, _next) do
|
||||
raise "Access.at/1 expected a list, got: #{inspect data}"
|
||||
end
|
||||
|
||||
defp get_and_update_at([head | rest], 0, next, updates) do
|
||||
case next.(head) do
|
||||
{get, update} -> {get, :lists.reverse([update | updates], rest)}
|
||||
:pop -> {head, :lists.reverse(updates, rest)}
|
||||
end
|
||||
end
|
||||
|
||||
defp get_and_update_at([head | rest], index, next, updates) do
|
||||
get_and_update_at(rest, index - 1, next, [head | updates])
|
||||
end
|
||||
|
||||
defp get_and_update_at([], _index, _next, updates) do
|
||||
{nil, :lists.reverse(updates)}
|
||||
end
|
||||
end
|
||||
|
||||
+10
-3
@@ -144,9 +144,9 @@ defmodule Agent do
|
||||
## Return values
|
||||
|
||||
If the server is successfully created and initialized, the function returns
|
||||
`{:ok, pid}`, where `pid` is the PID of the server. If an agent with the
|
||||
`{:ok, pid}`, where `pid` is the pid of the server. If an agent with the
|
||||
specified name already exists, the function returns
|
||||
`{:error, {:already_started, pid}}` with the PID of that process.
|
||||
`{:error, {:already_started, pid}}` with the pid of that process.
|
||||
|
||||
If the given function callback fails with `reason`, the function returns
|
||||
`{:error, reason}`.
|
||||
@@ -307,6 +307,13 @@ defmodule Agent do
|
||||
"""
|
||||
@spec stop(agent, reason :: term, timeout) :: :ok
|
||||
def stop(agent, reason \\ :normal, timeout \\ :infinity) do
|
||||
:gen.stop(agent, reason, timeout)
|
||||
if is_integer(reason) or reason == :infinity do
|
||||
IO.write :stderr, "warning: Agent.stop(agent, timeout) is deprecated, " <>
|
||||
"please use Agent.stop(agent, :normal, timeout) instead\n" <>
|
||||
Exception.format_stacktrace
|
||||
:gen.stop(agent, :normal, reason)
|
||||
else
|
||||
:gen.stop(agent, reason, timeout)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -26,7 +26,7 @@ defmodule Application do
|
||||
Once an application is started, OTP provides an application environment
|
||||
that can be used to configure the application.
|
||||
|
||||
Assuming you are inside a Mix project, you can edit the `application/0`
|
||||
Assuming you are inside a Mix project, you can edit the `application`
|
||||
function in the `mix.exs` file to the following:
|
||||
|
||||
def application do
|
||||
@@ -90,65 +90,10 @@ defmodule Application do
|
||||
supervisor is automatically handled by the VM.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Called when an application is started.
|
||||
|
||||
This function is called when an the application is started using
|
||||
`Application.start/2` (and functions on top of that, such as
|
||||
`Application.ensure_started/2`). This function should start the top-level
|
||||
process of the application (which should be the top supervisor of the
|
||||
application's supervision tree if the application follows the OTP design
|
||||
principles around supervision).
|
||||
|
||||
`start_type` defines how the application is started:
|
||||
|
||||
* `:normal` - used if the startup is a normal startup or if the application
|
||||
is distributed and is started on the current node because of a failover
|
||||
from another mode and the application specification key `:start_phases`
|
||||
is `:undefined`.
|
||||
* `{:takeover, node}` - used if the application is distributed and is
|
||||
started on the current node because of a failover on the node `node`.
|
||||
* `{:failover, node}` - used if the application is distributed and is
|
||||
started on the current node because of a failover on node `node`, and the
|
||||
application specification key `:start_phases` is not `:undefined`.
|
||||
|
||||
`start_args` are the arguments passed to the application in the `:mod`
|
||||
specification key (e.g., `mod: {MyApp, [:my_args]}`).
|
||||
|
||||
This function should either return `{:ok, pid}` or `{:ok, pid, state}` if
|
||||
startup is successful. `pid` should be the PID of the top supervisor. `state`
|
||||
can be an arbitrary term, and if omitted will default to `[]`; if the
|
||||
application is later stopped, `state` is passed to the `stop/1` callback (see
|
||||
the documentation for the `c:stop/1` callback for more information).
|
||||
|
||||
`use Application` provides no default implementation for the `start/2`
|
||||
callback.
|
||||
"""
|
||||
@callback start(start_type, start_args :: term) ::
|
||||
{:ok, pid} |
|
||||
{:ok, pid, state} |
|
||||
{:error, reason :: term}
|
||||
|
||||
@doc """
|
||||
Called when an application is stopped.
|
||||
|
||||
This function is called when an application has stopped, i.e., when its
|
||||
supervision tree has been stopped. It should do the opposite of what the
|
||||
`start/2` callback did, and should perform any necessary cleanup. The return
|
||||
value of this callback is ignored.
|
||||
|
||||
`state` is the return value of the `start/2` callback or the return value of
|
||||
the `prep_stop/1` function if the application module defines such a function.
|
||||
|
||||
`use Application` defines a default implementation of this function which does
|
||||
nothing and just returns `:ok`.
|
||||
"""
|
||||
@callback stop(state) :: term
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote location: :keep do
|
||||
@behaviour Application
|
||||
@behaviour :application
|
||||
|
||||
@doc false
|
||||
def stop(_state) do
|
||||
@@ -162,7 +107,6 @@ defmodule Application do
|
||||
@type app :: atom
|
||||
@type key :: atom
|
||||
@type value :: term
|
||||
@type state :: term
|
||||
@type start_type :: :permanent | :transient | :temporary
|
||||
|
||||
@application_keys [:description, :id, :vsn, :modules, :maxP, :maxT, :registered,
|
||||
@@ -189,20 +133,18 @@ defmodule Application do
|
||||
@doc """
|
||||
Returns the value for `key` in `app`'s specification.
|
||||
|
||||
See `spec/1` for the supported keys. If the given
|
||||
See `spec/1` for the supporte keys. If the given
|
||||
specification parameter does not exist, this function
|
||||
will raise. Returns `nil` if the application is not loaded.
|
||||
will raise.
|
||||
"""
|
||||
@spec spec(app, key) :: value | nil
|
||||
@spec spec(app, key) :: value
|
||||
def spec(app, key) when key in @application_keys do
|
||||
case :application.get_key(app, key) do
|
||||
{:ok, value} -> value
|
||||
:undefined -> nil
|
||||
end
|
||||
{:ok, value} = :application.get_key(app, key)
|
||||
value
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the application for the given module.
|
||||
Get the application for the given module.
|
||||
|
||||
The application is located by analyzing the spec
|
||||
of all loaded applications. Returns `nil` if
|
||||
@@ -432,13 +374,10 @@ defmodule Application do
|
||||
@doc """
|
||||
Returns the given path inside `app_dir/1`.
|
||||
"""
|
||||
@spec app_dir(app, String.t | [String.t]) :: String.t
|
||||
@spec app_dir(app, String.t) :: String.t
|
||||
def app_dir(app, path) when is_binary(path) do
|
||||
Path.join(app_dir(app), path)
|
||||
end
|
||||
def app_dir(app, path) when is_list(path) do
|
||||
Path.join([app_dir(app) | path])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list with information about the applications which are currently running.
|
||||
@@ -464,9 +403,9 @@ defmodule Application do
|
||||
@spec format_error(any) :: String.t
|
||||
def format_error(reason) do
|
||||
try do
|
||||
do_format_error(reason)
|
||||
impl_format_error(reason)
|
||||
catch
|
||||
# A user could create an error that looks like a built-in one
|
||||
# A user could create an error that looks like a builtin one
|
||||
# causing an error.
|
||||
:error, _ ->
|
||||
inspect(reason)
|
||||
@@ -474,68 +413,68 @@ defmodule Application do
|
||||
end
|
||||
|
||||
# exit(:normal) call is special cased, undo the special case.
|
||||
defp do_format_error({{:EXIT, :normal}, {mod, :start, args}}) do
|
||||
defp impl_format_error({{:EXIT, :normal}, {mod, :start, args}}) do
|
||||
Exception.format_exit({:normal, {mod, :start, args}})
|
||||
end
|
||||
|
||||
# {:error, reason} return value
|
||||
defp do_format_error({reason, {mod, :start, args}}) do
|
||||
defp impl_format_error({reason, {mod, :start, args}}) do
|
||||
Exception.format_mfa(mod, :start, args) <> " returned an error: " <>
|
||||
Exception.format_exit(reason)
|
||||
end
|
||||
|
||||
# error or exit(reason) call, use exit reason as reason.
|
||||
defp do_format_error({:bad_return, {{mod, :start, args}, {:EXIT, reason}}}) do
|
||||
defp impl_format_error({:bad_return, {{mod, :start, args}, {:EXIT, reason}}}) do
|
||||
Exception.format_exit({reason, {mod, :start, args}})
|
||||
end
|
||||
|
||||
# bad return value
|
||||
defp do_format_error({:bad_return, {{mod, :start, args}, return}}) do
|
||||
defp impl_format_error({:bad_return, {{mod, :start, args}, return}}) do
|
||||
Exception.format_mfa(mod, :start, args) <>
|
||||
" returned a bad value: " <> inspect(return)
|
||||
end
|
||||
|
||||
defp do_format_error({:already_started, app}) when is_atom(app) do
|
||||
defp impl_format_error({:already_started, app}) when is_atom(app) do
|
||||
"already started application #{app}"
|
||||
end
|
||||
|
||||
defp do_format_error({:not_started, app}) when is_atom(app) do
|
||||
defp impl_format_error({:not_started, app}) when is_atom(app) do
|
||||
"not started application #{app}"
|
||||
end
|
||||
|
||||
defp do_format_error({:bad_application, app}) do
|
||||
defp impl_format_error({:bad_application, app}) do
|
||||
"bad application: #{inspect(app)}"
|
||||
end
|
||||
|
||||
defp do_format_error({:already_loaded, app}) when is_atom(app) do
|
||||
defp impl_format_error({:already_loaded, app}) when is_atom(app) do
|
||||
"already loaded application #{app}"
|
||||
end
|
||||
|
||||
defp do_format_error({:not_loaded, app}) when is_atom(app) do
|
||||
defp impl_format_error({:not_loaded, app}) when is_atom(app) do
|
||||
"not loaded application #{app}"
|
||||
end
|
||||
|
||||
defp do_format_error({:invalid_restart_type, restart}) do
|
||||
defp impl_format_error({:invalid_restart_type, restart}) do
|
||||
"invalid application restart type: #{inspect(restart)}"
|
||||
end
|
||||
|
||||
defp do_format_error({:invalid_name, name}) do
|
||||
defp impl_format_error({:invalid_name, name}) do
|
||||
"invalid application name: #{inspect(name)}"
|
||||
end
|
||||
|
||||
defp do_format_error({:invalid_options, opts}) do
|
||||
defp impl_format_error({:invalid_options, opts}) do
|
||||
"invalid application options: #{inspect(opts)}"
|
||||
end
|
||||
|
||||
defp do_format_error({:badstartspec, spec}) do
|
||||
defp impl_format_error({:badstartspec, spec}) do
|
||||
"bad application start specs: #{inspect(spec)}"
|
||||
end
|
||||
|
||||
defp do_format_error({'no such file or directory', file}) do
|
||||
defp impl_format_error({'no such file or directory', file}) do
|
||||
"could not find application file: #{file}"
|
||||
end
|
||||
|
||||
defp do_format_error(reason) do
|
||||
defp impl_format_error(reason) do
|
||||
Exception.format_exit(reason)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -22,23 +22,18 @@ defmodule Atom do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts an atom to a charlist.
|
||||
Converts an atom to a char list.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Atom.to_charlist(:"An atom")
|
||||
iex> Atom.to_char_list(:"An atom")
|
||||
'An atom'
|
||||
|
||||
"""
|
||||
@spec to_charlist(atom) :: charlist
|
||||
def to_charlist(atom) do
|
||||
@spec to_char_list(atom) :: char_list
|
||||
def to_char_list(atom) do
|
||||
:erlang.atom_to_list(atom)
|
||||
end
|
||||
|
||||
# TODO: Deprecate by v1.5
|
||||
@doc false
|
||||
@spec to_char_list(atom) :: charlist
|
||||
def to_char_list(atom), do: Atom.to_charlist(atom)
|
||||
end
|
||||
|
||||
+153
-360
@@ -138,28 +138,11 @@ defmodule Base do
|
||||
defp from_mixed(char),
|
||||
do: char
|
||||
|
||||
defp maybe_pad(subject, false, _, _),
|
||||
do: subject
|
||||
defp maybe_pad(subject, _, group_size, pad) do
|
||||
case rem(byte_size(subject), group_size) do
|
||||
0 -> subject
|
||||
x -> subject <> String.duplicate(pad, group_size - x)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a binary string into a base 16 encoded string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to use when encoding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - uses upper case characters (default)
|
||||
* `:lower` - uses lower case characters
|
||||
Accepts an atom `:upper` (default) for encoding to upper case characters or
|
||||
`:lower` for lower case characters.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -180,17 +163,9 @@ defmodule Base do
|
||||
@doc """
|
||||
Decodes a base 16 encoded string into a binary string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to accept when decoding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - only allows upper case characters (default)
|
||||
* `:lower` - only allows lower case characters
|
||||
* `:mixed` - allows mixed case characters
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -215,17 +190,9 @@ defmodule Base do
|
||||
@doc """
|
||||
Decodes a base 16 encoded string into a binary string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to accept when decoding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - only allows upper case characters (default)
|
||||
* `:lower` - only allows lower case characters
|
||||
* `:mixed` - allows mixed case characters
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
@@ -258,56 +225,29 @@ defmodule Base do
|
||||
@doc """
|
||||
Encodes a binary string into a base 64 encoded string.
|
||||
|
||||
Accepts `padding: false` option which will omit padding from
|
||||
the output string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.encode64("foobar")
|
||||
"Zm9vYmFy"
|
||||
|
||||
iex> Base.encode64("foob")
|
||||
"Zm9vYg=="
|
||||
|
||||
iex> Base.encode64("foob", padding: false)
|
||||
"Zm9vYg"
|
||||
|
||||
"""
|
||||
@spec encode64(binary) :: binary
|
||||
@spec encode64(binary, Keyword.t) :: binary
|
||||
def encode64(data, opts \\ []) when is_binary(data) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_encode64(data, pad?)
|
||||
def encode64(data) when is_binary(data) do
|
||||
do_encode64(data)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 64 encoded string into a binary string.
|
||||
|
||||
Accepts `ignore: :whitespace` option which will ignore all the
|
||||
whitespace characters in the input string.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.decode64("Zm9vYmFy")
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.decode64("Zm9vYmFy\\n", ignore: :whitespace)
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.decode64("Zm9vYg==")
|
||||
{:ok, "foob"}
|
||||
|
||||
iex> Base.decode64("Zm9vYg", padding: false)
|
||||
{:ok, "foob"}
|
||||
|
||||
"""
|
||||
@spec decode64(binary) :: {:ok, binary} | :error
|
||||
@spec decode64(binary, Keyword.t) :: {:ok, binary} | :error
|
||||
def decode64(string, opts \\ []) when is_binary(string) do
|
||||
{:ok, decode64!(string, opts)}
|
||||
def decode64(string) when is_binary(string) do
|
||||
{:ok, decode64!(string)}
|
||||
rescue
|
||||
ArgumentError -> :error
|
||||
end
|
||||
@@ -315,11 +255,7 @@ defmodule Base do
|
||||
@doc """
|
||||
Decodes a base 64 encoded string into a binary string.
|
||||
|
||||
Accepts `ignore: :whitespace` option which will ignore all the
|
||||
whitespace characters in the input string.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
The following alphabet is used both for encoding and decoding:
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
@@ -329,72 +265,44 @@ defmodule Base do
|
||||
iex> Base.decode64!("Zm9vYmFy")
|
||||
"foobar"
|
||||
|
||||
iex> Base.decode64!("Zm9vYmFy\\n", ignore: :whitespace)
|
||||
"foobar"
|
||||
|
||||
iex> Base.decode64!("Zm9vYg==")
|
||||
"foob"
|
||||
|
||||
iex> Base.decode64!("Zm9vYg", padding: false)
|
||||
"foob"
|
||||
|
||||
"""
|
||||
@spec decode64!(binary) :: binary
|
||||
@spec decode64!(binary, Keyword.t) :: binary
|
||||
def decode64!(string, opts \\ []) when is_binary(string) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
string |> remove_ignored(opts[:ignore]) |> do_decode64(pad?)
|
||||
def decode64!(string) when is_binary(string) and rem(byte_size(string), 4) == 0 do
|
||||
do_decode64(string)
|
||||
end
|
||||
|
||||
def decode64!(string) when is_binary(string) do
|
||||
raise ArgumentError, "incorrect padding"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a binary string into a base 64 encoded string with URL and filename
|
||||
safe alphabet.
|
||||
|
||||
Accepts `padding: false` option which will omit padding from
|
||||
the output string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.url_encode64(<<255, 127, 254, 252>>)
|
||||
"_3_-_A=="
|
||||
|
||||
iex> Base.url_encode64(<<255, 127, 254, 252>>, padding: false)
|
||||
"_3_-_A"
|
||||
|
||||
"""
|
||||
@spec url_encode64(binary) :: binary
|
||||
@spec url_encode64(binary, Keyword.t) :: binary
|
||||
def url_encode64(data, opts \\ []) when is_binary(data) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_encode64url(data, pad?)
|
||||
def url_encode64(data) when is_binary(data) do
|
||||
do_encode64url(data)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 64 encoded string with URL and filename safe alphabet
|
||||
into a binary string.
|
||||
|
||||
Accepts `ignore: :whitespace` option which will ignore all the
|
||||
whitespace characters in the input string.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.url_decode64("_3_-_A==")
|
||||
{:ok, <<255, 127, 254, 252>>}
|
||||
|
||||
iex> Base.url_decode64("_3_-_A==\\n", ignore: :whitespace)
|
||||
{:ok, <<255, 127, 254, 252>>}
|
||||
|
||||
iex> Base.url_decode64("_3_-_A", padding: false)
|
||||
{:ok, <<255, 127, 254, 252>>}
|
||||
|
||||
"""
|
||||
@spec url_decode64(binary) :: {:ok, binary} | :error
|
||||
@spec url_decode64(binary, Keyword.t) :: {:ok, binary} | :error
|
||||
def url_decode64(string, opts \\ []) when is_binary(string) do
|
||||
{:ok, url_decode64!(string, opts)}
|
||||
def url_decode64(string) when is_binary(string) do
|
||||
{:ok, url_decode64!(string)}
|
||||
rescue
|
||||
ArgumentError -> :error
|
||||
end
|
||||
@@ -403,12 +311,6 @@ defmodule Base do
|
||||
Decodes a base 64 encoded string with URL and filename safe alphabet
|
||||
into a binary string.
|
||||
|
||||
Accepts `ignore: :whitespace` option which will ignore all the
|
||||
whitespace characters in the input string.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
|
||||
@@ -417,39 +319,21 @@ defmodule Base do
|
||||
iex> Base.url_decode64!("_3_-_A==")
|
||||
<<255, 127, 254, 252>>
|
||||
|
||||
iex> Base.url_decode64!("_3_-_A==\\n", ignore: :whitespace)
|
||||
<<255, 127, 254, 252>>
|
||||
|
||||
iex> Base.url_decode64!("_3_-_A", padding: false)
|
||||
<<255, 127, 254, 252>>
|
||||
|
||||
"""
|
||||
@spec url_decode64!(binary) :: binary
|
||||
@spec url_decode64!(binary, Keyword.t) :: binary
|
||||
def url_decode64!(string, opts \\ []) when is_binary(string) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
string |> remove_ignored(opts[:ignore]) |> do_decode64url(pad?)
|
||||
def url_decode64!(string) when is_binary(string) and rem(byte_size(string), 4) == 0 do
|
||||
do_decode64url(string)
|
||||
end
|
||||
|
||||
def url_decode64!(string) when is_binary(string) do
|
||||
raise ArgumentError, "incorrect padding"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a binary string into a base 32 encoded string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to use when encoding
|
||||
* `:padding` - specifies whether to apply padding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - uses upper case characters (default)
|
||||
* `:lower` - uses lower case characters
|
||||
|
||||
The values for `:padding` can be:
|
||||
|
||||
* `true` - pad the output string to the nearest multiple of 8 (default)
|
||||
* `false` - omit padding from the output string
|
||||
Accepts an atom `:upper` (default) for encoding to upper case characters or
|
||||
`:lower` for lower case characters.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -459,38 +343,20 @@ defmodule Base do
|
||||
iex> Base.encode32("foobar", case: :lower)
|
||||
"mzxw6ytboi======"
|
||||
|
||||
iex> Base.encode32("foobar", padding: false)
|
||||
"MZXW6YTBOI"
|
||||
|
||||
"""
|
||||
@spec encode32(binary) :: binary
|
||||
@spec encode32(binary, Keyword.t) :: binary
|
||||
def encode32(data, opts \\ []) when is_binary(data) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_encode32(case, data, pad?)
|
||||
do_encode32(case, data)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 32 encoded string into a binary string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to accept when decoding
|
||||
* `:padding` - specifies whether to require padding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - only allows upper case characters (default)
|
||||
* `:lower` - only allows lower case characters
|
||||
* `:mixed` - allows mixed case characters
|
||||
|
||||
The values for `:padding` can be:
|
||||
|
||||
* `true` - requires the input string to be padded to the nearest multiple of 8 (default)
|
||||
* `false` - ignores padding from the input string
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -503,9 +369,6 @@ defmodule Base do
|
||||
iex> Base.decode32("mzXW6ytBOi======", case: :mixed)
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.decode32("MZXW6YTBOI", padding: false)
|
||||
{:ok, "foobar"}
|
||||
|
||||
"""
|
||||
@spec decode32(binary) :: {:ok, binary} | :error
|
||||
@spec decode32(binary, Keyword.t) :: {:ok, binary} | :error
|
||||
@@ -518,27 +381,13 @@ defmodule Base do
|
||||
@doc """
|
||||
Decodes a base 32 encoded string into a binary string.
|
||||
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to accept when decoding
|
||||
* `:padding` - specifies whether to require padding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - only allows upper case characters (default)
|
||||
* `:lower` - only allows lower case characters
|
||||
* `:mixed` - allows mixed case characters
|
||||
|
||||
The values for `:padding` can be:
|
||||
|
||||
* `true` - requires the input string to be padded to the nearest multiple of 8 (default)
|
||||
* `false` - ignores padding from the input string
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.decode32!("MZXW6YTBOI======")
|
||||
@@ -550,38 +399,26 @@ defmodule Base do
|
||||
iex> Base.decode32!("mzXW6ytBOi======", case: :mixed)
|
||||
"foobar"
|
||||
|
||||
iex> Base.decode32!("MZXW6YTBOI", padding: false)
|
||||
"foobar"
|
||||
|
||||
"""
|
||||
@spec decode32!(binary) :: binary
|
||||
@spec decode32!(binary, Keyword.t) :: binary
|
||||
def decode32!(string, opts \\ []) when is_binary(string) do
|
||||
def decode32!(string, opts \\ [])
|
||||
|
||||
def decode32!(string, opts) when is_binary(string) and rem(byte_size(string), 8) == 0 do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_decode32(case, string, pad?)
|
||||
do_decode32(case, string)
|
||||
end
|
||||
|
||||
def decode32!(string, _opts) when is_binary(string) do
|
||||
raise ArgumentError, "incorrect padding"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a binary string into a base 32 encoded string with an
|
||||
extended hexadecimal alphabet.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to use when encoding
|
||||
* `:padding` - specifies whether to apply padding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - uses upper case characters (default)
|
||||
* `:lower` - uses lower case characters
|
||||
|
||||
The values for `:padding` can be:
|
||||
|
||||
* `true` - pad the output string to the nearest multiple of 8 (default)
|
||||
* `false` - omit padding from the output string
|
||||
Accepts an atom `:upper` (default) for encoding to upper case characters or
|
||||
`:lower` for lower case characters.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -591,39 +428,21 @@ defmodule Base do
|
||||
iex> Base.hex_encode32("foobar", case: :lower)
|
||||
"cpnmuoj1e8======"
|
||||
|
||||
iex> Base.hex_encode32("foobar", padding: false)
|
||||
"CPNMUOJ1E8"
|
||||
|
||||
"""
|
||||
@spec hex_encode32(binary) :: binary
|
||||
@spec hex_encode32(binary, Keyword.t) :: binary
|
||||
def hex_encode32(data, opts \\ []) when is_binary(data) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_hex_encode32(case, data, pad?)
|
||||
do_hex_encode32(case, data)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 32 encoded string with extended hexadecimal alphabet
|
||||
into a binary string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to accept when decoding
|
||||
* `:padding` - specifies whether to require padding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - only allows upper case characters (default)
|
||||
* `:lower` - only allows lower case characters
|
||||
* `:mixed` - allows mixed case characters
|
||||
|
||||
The values for `:padding` can be:
|
||||
|
||||
* `true` - requires the input string to be padded to the nearest multiple of 8 (default)
|
||||
* `false` - ignores padding from the input string
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -636,9 +455,6 @@ defmodule Base do
|
||||
iex> Base.hex_decode32("cpnMuOJ1E8======", case: :mixed)
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.hex_decode32("CPNMUOJ1E8", padding: false)
|
||||
{:ok, "foobar"}
|
||||
|
||||
"""
|
||||
@spec hex_decode32(binary) :: {:ok, binary} | :error
|
||||
@spec hex_decode32(binary, Keyword.t) :: {:ok, binary} | :error
|
||||
@@ -652,27 +468,13 @@ defmodule Base do
|
||||
Decodes a base 32 encoded string with extended hexadecimal alphabet
|
||||
into a binary string.
|
||||
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to accept when decoding
|
||||
* `:padding` - specifies whether to require padding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - only allows upper case characters (default)
|
||||
* `:lower` - only allows lower case characters
|
||||
* `:mixed` - allows mixed case characters
|
||||
|
||||
The values for `:padding` can be:
|
||||
|
||||
* `true` - requires the input string to be padded to the nearest multiple of 8 (default)
|
||||
* `false` - ignores padding from the input string
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.hex_decode32!("CPNMUOJ1E8======")
|
||||
@@ -684,21 +486,18 @@ defmodule Base do
|
||||
iex> Base.hex_decode32!("cpnMuOJ1E8======", case: :mixed)
|
||||
"foobar"
|
||||
|
||||
iex> Base.hex_decode32!("CPNMUOJ1E8", padding: false)
|
||||
"foobar"
|
||||
|
||||
"""
|
||||
@spec hex_decode32!(binary) :: binary
|
||||
@spec hex_decode32!(binary, Keyword.t) :: binary
|
||||
def hex_decode32!(string, opts \\ []) when is_binary(string) do
|
||||
def hex_decode32!(string, opts \\ [])
|
||||
|
||||
def hex_decode32!(string, opts) when is_binary(string) and rem(byte_size(string), 8) == 0 do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_hex_decode32(case, string, pad?)
|
||||
do_hex_decode32(case, string)
|
||||
end
|
||||
|
||||
defp remove_ignored(string, nil), do: string
|
||||
defp remove_ignored(string, :whitespace) do
|
||||
for <<c::8 <- string>>, not c in '\s\t\r\n', into: <<>>, do: <<c::8>>
|
||||
def hex_decode32!(string, _opts) when is_binary(string) do
|
||||
raise ArgumentError, "incorrect padding"
|
||||
end
|
||||
|
||||
defp do_encode16(_, <<>>), do: <<>>
|
||||
@@ -726,219 +525,213 @@ defmodule Base do
|
||||
end
|
||||
end
|
||||
|
||||
defp do_encode64(<<>>, _), do: <<>>
|
||||
defp do_encode64(data, pad?) do
|
||||
defp do_encode64(<<>>), do: <<>>
|
||||
defp do_encode64(data) do
|
||||
split = 3 * div(byte_size(data), 3)
|
||||
<<main::size(split)-binary, rest::binary>> = data
|
||||
main = for <<c::6 <- main>>, into: <<>>, do: <<enc64(c)::8>>
|
||||
tail = case rest do
|
||||
case rest do
|
||||
<<c1::6, c2::6, c3::4>> ->
|
||||
<<enc64(c1)::8, enc64(c2)::8, enc64(bsl(c3, 2))::8>>
|
||||
<<main::binary, enc64(c1)::8, enc64(c2)::8, enc64(bsl(c3, 2))::8, ?=>>
|
||||
<<c1::6, c2::2>> ->
|
||||
<<enc64(c1)::8, enc64(bsl(c2, 4))::8>>
|
||||
<<main::binary, enc64(c1)::8, enc64(bsl(c2, 4))::8, ?=, ?=>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
main
|
||||
end
|
||||
main <> maybe_pad(tail, pad?, 4, "=")
|
||||
end
|
||||
|
||||
defp do_decode64(<<>>, _), do: <<>>
|
||||
defp do_decode64(string, false) do
|
||||
maybe_pad(string, true, 4, "=") |> do_decode64(true)
|
||||
end
|
||||
defp do_decode64(string, _pad?) when rem(byte_size(string), 4) == 0 do
|
||||
defp do_decode64(<<>>), do: <<>>
|
||||
defp do_decode64(string) do
|
||||
split = byte_size(string) - 4
|
||||
<<main::size(split)-binary, rest::binary>> = string
|
||||
main = for <<c::8 <- main>>, into: <<>>, do: <<dec64(c)::6>>
|
||||
tail = case rest do
|
||||
case rest do
|
||||
<<c1::8, c2::8, ?=, ?=>> ->
|
||||
<<dec64(c1)::6, bsr(dec64(c2), 4)::2>>
|
||||
<<main::binary, dec64(c1)::6, bsr(dec64(c2), 4)::2>>
|
||||
<<c1::8, c2::8, c3::8, ?=>> ->
|
||||
<<dec64(c1)::6, dec64(c2)::6, bsr(dec64(c3), 2)::4>>
|
||||
<<main::binary, dec64(c1)::6, dec64(c2)::6, bsr(dec64(c3), 2)::4>>
|
||||
<<c1::8, c2::8, c3::8, c4::8>> ->
|
||||
<<dec64(c1)::6, dec64(c2)::6, dec64(c3)::6, dec64(c4)::6>>
|
||||
<<main::binary, dec64(c1)::6, dec64(c2)::6, dec64(c3)::6, dec64(c4)::6>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
main
|
||||
end
|
||||
main <> tail
|
||||
end
|
||||
defp do_decode64(_, _) do
|
||||
raise ArgumentError, "incorrect padding"
|
||||
end
|
||||
|
||||
defp do_encode64url(<<>>, _), do: <<>>
|
||||
defp do_encode64url(data, pad?) do
|
||||
defp do_encode64url(<<>>), do: <<>>
|
||||
defp do_encode64url(data) do
|
||||
split = 3 * div(byte_size(data), 3)
|
||||
<<main::size(split)-binary, rest::binary>> = data
|
||||
main = for <<c::6 <- main>>, into: <<>>, do: <<enc64url(c)::8>>
|
||||
tail = case rest do
|
||||
case rest do
|
||||
<<c1::6, c2::6, c3::4>> ->
|
||||
<<enc64url(c1)::8, enc64url(c2)::8, enc64url(bsl(c3, 2))::8>>
|
||||
<<main::binary, enc64url(c1)::8, enc64url(c2)::8, enc64url(bsl(c3, 2))::8, ?=>>
|
||||
<<c1::6, c2::2>> ->
|
||||
<<enc64url(c1)::8, enc64url(bsl(c2, 4))::8>>
|
||||
<<main::binary, enc64url(c1)::8, enc64url(bsl(c2, 4))::8, ?=, ?=>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
main
|
||||
end
|
||||
main <> maybe_pad(tail, pad?, 4, "=")
|
||||
end
|
||||
|
||||
defp do_decode64url(<<>>, _), do: <<>>
|
||||
defp do_decode64url(string, false) do
|
||||
maybe_pad(string, true, 4, "=") |> do_decode64url(true)
|
||||
end
|
||||
defp do_decode64url(string, _pad?) when rem(byte_size(string), 4) == 0 do
|
||||
defp do_decode64url(<<>>), do: <<>>
|
||||
defp do_decode64url(string) do
|
||||
split = byte_size(string) - 4
|
||||
<<main::size(split)-binary, rest::binary>> = string
|
||||
main = for <<c::8 <- main>>, into: <<>>, do: <<dec64url(c)::6>>
|
||||
tail = case rest do
|
||||
case rest do
|
||||
<<c1::8, c2::8, ?=, ?=>> ->
|
||||
<<dec64url(c1)::6, bsr(dec64url(c2), 4)::2>>
|
||||
<<main::binary, dec64url(c1)::6, bsr(dec64url(c2), 4)::2>>
|
||||
<<c1::8, c2::8, c3::8, ?=>> ->
|
||||
<<dec64url(c1)::6, dec64url(c2)::6, bsr(dec64url(c3), 2)::4>>
|
||||
<<main::binary, dec64url(c1)::6, dec64url(c2)::6, bsr(dec64url(c3), 2)::4>>
|
||||
<<c1::8, c2::8, c3::8, c4::8>> ->
|
||||
<<dec64url(c1)::6, dec64url(c2)::6, dec64url(c3)::6, dec64url(c4)::6>>
|
||||
<<main::binary, dec64url(c1)::6, dec64url(c2)::6, dec64url(c3)::6, dec64url(c4)::6>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
main
|
||||
end
|
||||
main <> tail
|
||||
end
|
||||
defp do_decode64url(_, _) do
|
||||
raise ArgumentError, "incorrect padding"
|
||||
end
|
||||
|
||||
defp do_encode32(_, <<>>, _), do: <<>>
|
||||
|
||||
|
||||
defp do_encode32(_, <<>>), do: <<>>
|
||||
|
||||
for {case, fun} <- [upper: :to_upper, lower: :to_lower] do
|
||||
defp do_encode32(unquote(case), data, pad?) do
|
||||
defp do_encode32(unquote(case), data) do
|
||||
split = 5 * div(byte_size(data), 5)
|
||||
<<main::size(split)-binary, rest::binary>> = data
|
||||
main = for <<c::5 <- main>>, into: <<>>, do: <<unquote(fun)(enc32(c))::8>>
|
||||
tail = case rest do
|
||||
case rest do
|
||||
<<c1::5, c2::5, c3::5, c4::5, c5::5, c6::5, c7::2>> ->
|
||||
<<unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(c2))::8,
|
||||
<<main::binary,
|
||||
unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(c2))::8,
|
||||
unquote(fun)(enc32(c3))::8, unquote(fun)(enc32(c4))::8,
|
||||
unquote(fun)(enc32(c5))::8, unquote(fun)(enc32(c6))::8,
|
||||
unquote(fun)(enc32(bsl(c7, 3)))::8>>
|
||||
unquote(fun)(enc32(bsl(c7, 3)))::8, ?=>>
|
||||
<<c1::5, c2::5, c3::5, c4::5, c5::4>> ->
|
||||
<<unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(c2))::8,
|
||||
<<main::binary,
|
||||
unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(c2))::8,
|
||||
unquote(fun)(enc32(c3))::8, unquote(fun)(enc32(c4))::8,
|
||||
unquote(fun)(enc32(bsl(c5, 1)))::8>>
|
||||
unquote(fun)(enc32(bsl(c5, 1)))::8, ?=, ?=, ?=>>
|
||||
<<c1::5, c2::5, c3::5, c4::1>> ->
|
||||
<<unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(c2))::8,
|
||||
unquote(fun)(enc32(c3))::8, unquote(fun)(enc32(bsl(c4, 4)))::8>>
|
||||
<<main::binary,
|
||||
unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(c2))::8,
|
||||
unquote(fun)(enc32(c3))::8, unquote(fun)(enc32(bsl(c4, 4)))::8,
|
||||
?=, ?=, ?=, ?=>>
|
||||
<<c1::5, c2::3>> ->
|
||||
<<unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(bsl(c2, 2)))::8>>
|
||||
<<main::binary,
|
||||
unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(bsl(c2, 2)))::8, ?=, ?=,
|
||||
?=, ?=, ?=, ?=>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
main
|
||||
end
|
||||
main <> maybe_pad(tail, pad?, 8, "=")
|
||||
end
|
||||
end
|
||||
|
||||
defp do_decode32(_, <<>>, _), do: <<>>
|
||||
defp do_decode32(case, string, false),
|
||||
do: do_decode32(case, maybe_pad(string, true, 8, "="), true)
|
||||
defp do_decode32(_, <<>>), do: <<>>
|
||||
|
||||
for {case, fun} <- [upper: :from_upper, lower: :from_lower, mixed: :from_mixed] do
|
||||
defp do_decode32(unquote(case), string, _pad?) when rem(byte_size(string), 8) == 0 do
|
||||
defp do_decode32(unquote(case), string) do
|
||||
split = byte_size(string) - 8
|
||||
<<main::size(split)-binary, rest::binary>> = string
|
||||
main = for <<c::8 <- main>>, into: <<>>, do: <<dec32(unquote(fun)(c))::5>>
|
||||
tail = case rest do
|
||||
case rest do
|
||||
<<c1::8, c2::8, ?=, ?=, ?=, ?=, ?=, ?=>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, bsr(dec32(unquote(fun)(c2)), 2)::3>>
|
||||
<<main::binary, dec32(unquote(fun)(c1))::5,
|
||||
bsr(dec32(unquote(fun)(c2)), 2)::3>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, ?=, ?=, ?=, ?=>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
<<main::binary,
|
||||
dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
dec32(unquote(fun)(c3))::5, bsr(dec32(unquote(fun)(c4)), 4)::1>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, ?=, ?=, ?=>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
<<main::binary,
|
||||
dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
dec32(unquote(fun)(c3))::5, dec32(unquote(fun)(c4))::5,
|
||||
bsr(dec32(unquote(fun)(c5)), 1)::4>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, c6::8, c7::8, ?=>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
<<main::binary,
|
||||
dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
dec32(unquote(fun)(c3))::5, dec32(unquote(fun)(c4))::5,
|
||||
dec32(unquote(fun)(c5))::5, dec32(unquote(fun)(c6))::5,
|
||||
bsr(dec32(unquote(fun)(c7)), 3)::2>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, c6::8, c7::8, c8::8>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
<<main::binary,
|
||||
dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
dec32(unquote(fun)(c3))::5, dec32(unquote(fun)(c4))::5,
|
||||
dec32(unquote(fun)(c5))::5, dec32(unquote(fun)(c6))::5,
|
||||
dec32(unquote(fun)(c7))::5, dec32(unquote(fun)(c8))::5>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
main
|
||||
end
|
||||
main <> tail
|
||||
end
|
||||
end
|
||||
|
||||
defp do_decode32(_, _, _),
|
||||
do: raise ArgumentError, "incorrect padding"
|
||||
|
||||
defp do_hex_encode32(_, <<>>, _), do: <<>>
|
||||
defp do_hex_encode32(_, <<>>), do: <<>>
|
||||
|
||||
for {case, fun} <- [upper: :to_upper, lower: :to_lower] do
|
||||
defp do_hex_encode32(unquote(case), data, pad?) do
|
||||
defp do_hex_encode32(unquote(case), data) do
|
||||
split = 5 * div(byte_size(data), 5)
|
||||
<<main::size(split)-binary, rest::binary>> = data
|
||||
main = for <<c::5 <- main>>, into: <<>>, do: <<unquote(fun)(enc32hex(c))::8>>
|
||||
tail = case rest do
|
||||
case rest do
|
||||
<<c1::5, c2::5, c3::5, c4::5, c5::5, c6::5, c7::2>> ->
|
||||
<<unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(c2))::8,
|
||||
<<main::binary,
|
||||
unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(c2))::8,
|
||||
unquote(fun)(enc32hex(c3))::8, unquote(fun)(enc32hex(c4))::8,
|
||||
unquote(fun)(enc32hex(c5))::8, unquote(fun)(enc32hex(c6))::8,
|
||||
unquote(fun)(enc32hex(bsl(c7, 3)))::8>>
|
||||
unquote(fun)(enc32hex(bsl(c7, 3)))::8, ?=>>
|
||||
<<c1::5, c2::5, c3::5, c4::5, c5::4>> ->
|
||||
<<unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(c2))::8,
|
||||
<<main::binary,
|
||||
unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(c2))::8,
|
||||
unquote(fun)(enc32hex(c3))::8, unquote(fun)(enc32hex(c4))::8,
|
||||
unquote(fun)(enc32hex(bsl(c5, 1)))::8>>
|
||||
unquote(fun)(enc32hex(bsl(c5, 1)))::8, ?=, ?=, ?=>>
|
||||
<<c1::5, c2::5, c3::5, c4::1>> ->
|
||||
<<unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(c2))::8,
|
||||
unquote(fun)(enc32hex(c3))::8, unquote(fun)(enc32hex(bsl(c4, 4)))::8>>
|
||||
<<main::binary,
|
||||
unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(c2))::8,
|
||||
unquote(fun)(enc32hex(c3))::8, unquote(fun)(enc32hex(bsl(c4, 4)))::8,
|
||||
?=, ?=, ?=, ?=>>
|
||||
<<c1::5, c2::3>> ->
|
||||
<<unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(bsl(c2, 2)))::8>>
|
||||
<<main::binary,
|
||||
unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(bsl(c2, 2)))::8, ?=, ?=,
|
||||
?=, ?=, ?=, ?=>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
main
|
||||
end
|
||||
main <> maybe_pad(tail, pad?, 8, "=")
|
||||
end
|
||||
end
|
||||
|
||||
defp do_hex_decode32(_, <<>>, _), do: <<>>
|
||||
defp do_hex_decode32(case, string, false),
|
||||
do: do_hex_decode32(case, maybe_pad(string, true, 8, "="), true)
|
||||
defp do_hex_decode32(_, <<>>), do: <<>>
|
||||
|
||||
for {case, fun} <- [upper: :from_upper, lower: :from_lower, mixed: :from_mixed] do
|
||||
defp do_hex_decode32(unquote(case), string, _pad?) when rem(byte_size(string), 8) == 0 do
|
||||
defp do_hex_decode32(unquote(case), string) do
|
||||
split = byte_size(string) - 8
|
||||
<<main::size(split)-binary, rest::binary>> = string
|
||||
main = for <<c::8 <- main>>, into: <<>>, do: <<dec32hex(unquote(fun)(c))::5>>
|
||||
tail = case rest do
|
||||
case rest do
|
||||
<<c1::8, c2::8, ?=, ?=, ?=, ?=, ?=, ?=>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, bsr(dec32hex(unquote(fun)(c2)), 2)::3>>
|
||||
<<main::binary, dec32hex(unquote(fun)(c1))::5,
|
||||
bsr(dec32hex(unquote(fun)(c2)), 2)::3>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, ?=, ?=, ?=, ?=>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
<<main::binary,
|
||||
dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
dec32hex(unquote(fun)(c3))::5, bsr(dec32hex(unquote(fun)(c4)), 4)::1>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, ?=, ?=, ?=>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
<<main::binary,
|
||||
dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
dec32hex(unquote(fun)(c3))::5, dec32hex(unquote(fun)(c4))::5,
|
||||
bsr(dec32hex(unquote(fun)(c5)), 1)::4>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, c6::8, c7::8, ?=>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
<<main::binary,
|
||||
dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
dec32hex(unquote(fun)(c3))::5, dec32hex(unquote(fun)(c4))::5,
|
||||
dec32hex(unquote(fun)(c5))::5, dec32hex(unquote(fun)(c6))::5,
|
||||
bsr(dec32hex(unquote(fun)(c7)), 3)::2>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, c6::8, c7::8, c8::8>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
<<main::binary,
|
||||
dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
dec32hex(unquote(fun)(c3))::5, dec32hex(unquote(fun)(c4))::5,
|
||||
dec32hex(unquote(fun)(c5))::5, dec32hex(unquote(fun)(c6))::5,
|
||||
dec32hex(unquote(fun)(c7))::5, dec32hex(unquote(fun)(c8))::5>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
main
|
||||
end
|
||||
main <> tail
|
||||
end
|
||||
end
|
||||
|
||||
defp do_hex_decode32(_, _, _),
|
||||
do: raise ArgumentError, "incorrect padding"
|
||||
end
|
||||
|
||||
@@ -2,14 +2,14 @@ defmodule Behaviour do
|
||||
@moduledoc """
|
||||
This module has been deprecated.
|
||||
|
||||
Instead of `defcallback/1` and `defmacrocallback/1`, the `@callback` and
|
||||
`@macrocallback` module attributes can be used (respectively). See the
|
||||
documentation for `Module` for more information on these attributes.
|
||||
|
||||
Instead of `MyModule.__behaviour__(:callbacks)`,
|
||||
`MyModule.behaviour_info(:callbacks)` can be used.
|
||||
Instead of `defcallback`, one can simply use `@callback`.
|
||||
Instead of `defmacrocallback`, one can simply use `@macrocallback`.
|
||||
Instead of `__behaviour__(:callbacks)`, one can simply use `behaviour_info(:callbacks)`.
|
||||
"""
|
||||
|
||||
# TODO: Deprecate by 1.2
|
||||
# TODO: Remove by 2.0
|
||||
|
||||
@doc """
|
||||
Defines a function callback according to the given type specification.
|
||||
"""
|
||||
@@ -80,12 +80,6 @@ defmodule Behaviour do
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote do
|
||||
warning =
|
||||
"the Behaviour module is deprecated. Instead of using this module, " <>
|
||||
"use the @callback and @macrocallback module attributes. See the " <>
|
||||
"documentation for Module for more information on these attributes"
|
||||
IO.warn(warning)
|
||||
|
||||
@doc false
|
||||
def __behaviour__(:callbacks) do
|
||||
__MODULE__.behaviour_info(:callbacks)
|
||||
|
||||
+11
-17
@@ -1,35 +1,30 @@
|
||||
defmodule Bitwise do
|
||||
@moduledoc """
|
||||
A set of macros that perform calculations on bits.
|
||||
This module provides macro-based operators that perform calculations
|
||||
on (sets of) bits.
|
||||
|
||||
The macros in this module come in two flavors: named or
|
||||
operators. For example:
|
||||
In general, you should `use` the Bitwise module as a whole:
|
||||
|
||||
iex> use Bitwise
|
||||
iex> bnot 1 # named
|
||||
iex> bnot 1
|
||||
-2
|
||||
iex> 1 &&& 1 # operator
|
||||
iex> 1 &&& 1
|
||||
1
|
||||
|
||||
If you prefer to use only operators or skip them, you can
|
||||
pass the following options:
|
||||
When used, it accepts the following options:
|
||||
|
||||
* `:only_operators` - includes only operators
|
||||
* `:skip_operators` - skips operators
|
||||
|
||||
For example:
|
||||
* `:only_operators` - include only operators
|
||||
* `:skip_operators` - skip operators
|
||||
|
||||
iex> use Bitwise, only_operators: true
|
||||
iex> 1 &&& 1
|
||||
1
|
||||
|
||||
When invoked with no options, `use Bitwise` is equivalent
|
||||
to `import Bitwise`.
|
||||
|
||||
All bitwise macros can be used in guards:
|
||||
These macros can be used in guards:
|
||||
|
||||
iex> use Bitwise
|
||||
iex> odd? = fn int when band(int, 1) == 1 -> true; _ -> false end
|
||||
iex> odd? = fn(int) when band(int, 1) == 1 -> true; (_) -> false end
|
||||
iex> odd?.(1)
|
||||
true
|
||||
|
||||
@@ -42,8 +37,7 @@ defmodule Bitwise do
|
||||
[bnot: 1, band: 2, bor: 2, bxor: 2, bsl: 2, bsr: 2]
|
||||
Keyword.get(options, :skip_operators) ->
|
||||
[~~~: 1, &&&: 2, |||: 2, ^^^: 2, <<<: 2, >>>: 2]
|
||||
true ->
|
||||
[]
|
||||
true -> []
|
||||
end
|
||||
|
||||
quote do
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,254 +0,0 @@
|
||||
defmodule Calendar.ISO do
|
||||
@moduledoc """
|
||||
A calendar implementation that follows to ISO8601.
|
||||
|
||||
This calendar implements the proleptic Gregorian calendar and
|
||||
is therefore compatible with the calendar used in most countries
|
||||
today. The proleptic means the Gregorian rules for leap years are
|
||||
applied for all time, consequently the dates give different results
|
||||
before the year 1583 from when the Gregorian calendar was adopted.
|
||||
"""
|
||||
|
||||
@behaviour Calendar
|
||||
@unix_epoch :calendar.datetime_to_gregorian_seconds {{1970, 1, 1}, {0, 0, 0}}
|
||||
|
||||
@type year :: 0..9999
|
||||
@type month :: 1..12
|
||||
@type day :: 1..31
|
||||
|
||||
@doc """
|
||||
Returns how many days there are in the given year-month.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.days_in_month(1900, 1)
|
||||
31
|
||||
iex> Calendar.ISO.days_in_month(1900, 2)
|
||||
28
|
||||
iex> Calendar.ISO.days_in_month(2000, 2)
|
||||
29
|
||||
iex> Calendar.ISO.days_in_month(2001, 2)
|
||||
28
|
||||
iex> Calendar.ISO.days_in_month(2004, 2)
|
||||
29
|
||||
iex> Calendar.ISO.days_in_month(2004, 4)
|
||||
30
|
||||
|
||||
"""
|
||||
@spec days_in_month(year, month) :: 28..31
|
||||
def days_in_month(year, month)
|
||||
|
||||
def days_in_month(year, 2) do
|
||||
if leap_year?(year), do: 29, else: 28
|
||||
end
|
||||
def days_in_month(_, month) when month in [4, 6, 9, 11], do: 30
|
||||
def days_in_month(_, month) when month in 1..12, do: 31
|
||||
|
||||
@doc """
|
||||
Returns if the given year is a leap year.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.leap_year?(2000)
|
||||
true
|
||||
iex> Calendar.ISO.leap_year?(2001)
|
||||
false
|
||||
iex> Calendar.ISO.leap_year?(2004)
|
||||
true
|
||||
iex> Calendar.ISO.leap_year?(1900)
|
||||
false
|
||||
|
||||
"""
|
||||
@spec leap_year?(year) :: boolean()
|
||||
def leap_year?(year) when is_integer(year) and year >= 0 do
|
||||
rem(year, 4) === 0 and (rem(year, 100) > 0 or rem(year, 400) === 0)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the day of the week from the given `year`, `month`, and `day`.
|
||||
|
||||
It is an integer from 1 to 7, where 1 is Monday and 7 is Sunday.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.day_of_week(2016, 10, 31)
|
||||
1
|
||||
iex> Calendar.ISO.day_of_week(2016, 11, 01)
|
||||
2
|
||||
iex> Calendar.ISO.day_of_week(2016, 11, 02)
|
||||
3
|
||||
iex> Calendar.ISO.day_of_week(2016, 11, 03)
|
||||
4
|
||||
iex> Calendar.ISO.day_of_week(2016, 11, 04)
|
||||
5
|
||||
iex> Calendar.ISO.day_of_week(2016, 11, 05)
|
||||
6
|
||||
iex> Calendar.ISO.day_of_week(2016, 11, 06)
|
||||
7
|
||||
"""
|
||||
@spec day_of_week(year, month, day) :: 1..7
|
||||
def day_of_week(year, month, day)
|
||||
when is_integer(year) and is_integer(month) and is_integer(day) do
|
||||
:calendar.day_of_the_week(year, month, day)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given date into a string.
|
||||
"""
|
||||
def date_to_string(year, month, day) do
|
||||
zero_pad(year, 4) <> "-" <> zero_pad(month, 2) <> "-" <> zero_pad(day, 2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the datetime (without time zone) into a string.
|
||||
"""
|
||||
def naive_datetime_to_string(year, month, day, hour, minute, second, microsecond) do
|
||||
date_to_string(year, month, day) <> " " <> time_to_string(hour, minute, second, microsecond)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Convers the datetime (with time zone) into a string.
|
||||
"""
|
||||
def datetime_to_string(year, month, day, hour, minute, second, microsecond,
|
||||
time_zone, zone_abbr, utc_offset, std_offset) do
|
||||
date_to_string(year, month, day) <> " " <>
|
||||
time_to_string(hour, minute, second, microsecond) <>
|
||||
offset_to_string(utc_offset, std_offset, time_zone) <>
|
||||
zone_to_string(utc_offset, std_offset, zone_abbr, time_zone)
|
||||
end
|
||||
|
||||
defp offset_to_string(0, 0, "Etc/UTC"), do: "Z"
|
||||
defp offset_to_string(utc, std, _zone) do
|
||||
total = utc + std
|
||||
second = abs(total)
|
||||
minute = second |> rem(3600) |> div(60)
|
||||
hour = second |> div(3600)
|
||||
sign(total) <> zero_pad(hour, 2) <> ":" <> zero_pad(minute, 2)
|
||||
end
|
||||
|
||||
defp zone_to_string(0, 0, _abbr, "Etc/UTC"), do: ""
|
||||
defp zone_to_string(_, _, abbr, zone), do: " " <> abbr <> " " <> zone
|
||||
|
||||
defp sign(total) when total < 0, do: "-"
|
||||
defp sign(_), do: "+"
|
||||
|
||||
defp zero_pad(val, count) do
|
||||
num = Integer.to_string(val)
|
||||
:binary.copy("0", count - byte_size(num)) <> num
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
@doc false
|
||||
def time_to_string(hour, minute, second, {_, 0}) do
|
||||
time_to_string(hour, minute, second)
|
||||
end
|
||||
def time_to_string(hour, minute, second, {microsecond, precision}) do
|
||||
time_to_string(hour, minute, second) <> "." <>
|
||||
(microsecond |> zero_pad(6) |> binary_part(0, precision))
|
||||
end
|
||||
defp time_to_string(hour, minute, second) do
|
||||
zero_pad(hour, 2) <> ":" <> zero_pad(minute, 2) <> ":" <> zero_pad(second, 2)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def date(year, month, day) when is_integer(year) and is_integer(month) and is_integer(day) do
|
||||
if :calendar.valid_date(year, month, day) and year <= 9999 do
|
||||
{:ok, %Date{year: year, month: month, day: day}}
|
||||
else
|
||||
{:error, :invalid_date}
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def from_unix(integer, unit) when is_integer(integer) do
|
||||
total = System.convert_time_unit(integer, unit, :microsecond)
|
||||
if total < -@unix_epoch * 1_000_000 do
|
||||
{:error, :invalid_unix_time}
|
||||
else
|
||||
microsecond = rem(total, 1_000_000)
|
||||
precision = precision_for_unit(unit)
|
||||
{date, time} = :calendar.gregorian_seconds_to_datetime(@unix_epoch + div(total, 1_000_000))
|
||||
{:ok, date, time, {microsecond, precision}}
|
||||
end
|
||||
end
|
||||
|
||||
defp precision_for_unit(unit) do
|
||||
subsecond = div System.convert_time_unit(1, :second, unit), 10
|
||||
precision_for_unit(subsecond, 0)
|
||||
end
|
||||
|
||||
defp precision_for_unit(0, precision),
|
||||
do: precision
|
||||
defp precision_for_unit(_, 6),
|
||||
do: 6
|
||||
defp precision_for_unit(number, precision),
|
||||
do: precision_for_unit(div(number, 10), precision + 1)
|
||||
|
||||
@doc false
|
||||
def date_to_iso8601(year, month, day) do
|
||||
date_to_string(year, month, day)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def time_to_iso8601(hour, minute, second, microsecond) do
|
||||
time_to_string(hour, minute, second, microsecond)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def naive_datetime_to_iso8601(year, month, day, hour, minute, second, microsecond) do
|
||||
date_to_string(year, month, day) <> "T" <> time_to_string(hour, minute, second, microsecond)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def datetime_to_iso8601(year, month, day, hour, minute, second, microsecond,
|
||||
time_zone, _zone_abbr, utc_offset, std_offset) do
|
||||
date_to_string(year, month, day) <> "T" <>
|
||||
time_to_string(hour, minute, second, microsecond) <>
|
||||
offset_to_string(utc_offset, std_offset, time_zone)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def parse_microsecond("." <> rest) do
|
||||
case parse_microsecond(rest, 0, "") do
|
||||
{"", 0, _} ->
|
||||
:error
|
||||
{microsecond, precision, rest} when precision in 1..6 ->
|
||||
pad = String.duplicate("0", 6 - byte_size(microsecond))
|
||||
{{String.to_integer(microsecond <> pad), precision}, rest}
|
||||
{microsecond, _precision, rest} ->
|
||||
{{String.to_integer(binary_part(microsecond, 0, 6)), 6}, rest}
|
||||
end
|
||||
end
|
||||
def parse_microsecond(rest) do
|
||||
{{0, 0}, rest}
|
||||
end
|
||||
|
||||
defp parse_microsecond(<<h, t::binary>>, precision, acc) when h in ?0..?9,
|
||||
do: parse_microsecond(t, precision + 1, <<acc::binary, h>>)
|
||||
defp parse_microsecond(rest, precision, acc),
|
||||
do: {acc, precision, rest}
|
||||
|
||||
@doc false
|
||||
def parse_offset(""),
|
||||
do: {nil, ""}
|
||||
def parse_offset("Z"),
|
||||
do: {0, ""}
|
||||
def parse_offset("-00:00"),
|
||||
do: :error
|
||||
def parse_offset(<<?+, hour::2-bytes, ?:, min::2-bytes, rest::binary>>),
|
||||
do: parse_offset(1, hour, min, rest)
|
||||
def parse_offset(<<?-, hour::2-bytes, ?:, min::2-bytes, rest::binary>>),
|
||||
do: parse_offset(-1, hour, min, rest)
|
||||
def parse_offset(_),
|
||||
do: :error
|
||||
|
||||
defp parse_offset(sign, hour, min, rest) do
|
||||
with {hour, ""} when hour < 24 <- Integer.parse(hour),
|
||||
{min, ""} when min < 60 <- Integer.parse(min) do
|
||||
{((hour * 60) + min) * 60 * sign, rest}
|
||||
else
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
end
|
||||
+42
-60
@@ -56,11 +56,11 @@ defmodule Code do
|
||||
|
||||
"""
|
||||
def append_path(path) do
|
||||
:code.add_pathz(to_charlist(Path.expand path))
|
||||
:code.add_pathz(to_char_list(Path.expand path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prepends a path to the beginning of the Erlang VM code path list.
|
||||
Prepends a path to the begining of the Erlang VM code path list.
|
||||
|
||||
This is the list of directories the Erlang VM uses for finding
|
||||
module code.
|
||||
@@ -76,7 +76,7 @@ defmodule Code do
|
||||
|
||||
"""
|
||||
def prepend_path(path) do
|
||||
:code.add_patha(to_charlist(Path.expand path))
|
||||
:code.add_patha(to_char_list(Path.expand path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -95,7 +95,7 @@ defmodule Code do
|
||||
|
||||
"""
|
||||
def delete_path(path) do
|
||||
:code.del_path(to_charlist(Path.expand path))
|
||||
:code.del_path(to_char_list(Path.expand path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -104,9 +104,7 @@ defmodule Code do
|
||||
The `binding` argument is a keyword list of variable bindings.
|
||||
The `opts` argument is a keyword list of environment options.
|
||||
|
||||
## Options
|
||||
|
||||
Options can be:
|
||||
Those options can be:
|
||||
|
||||
* `:file` - the file to be considered in the evaluation
|
||||
* `:line` - the line on which the script starts
|
||||
@@ -128,7 +126,7 @@ defmodule Code do
|
||||
Notice that setting any of the values above overrides Elixir's default
|
||||
values. For example, setting `:requires` to `[]`, will no longer
|
||||
automatically require the `Kernel` module; in the same way setting
|
||||
`:macros` will no longer auto-import `Kernel` macros like `if/2`, `case/2`,
|
||||
`:macros` will no longer auto-import `Kernel` macros like `if`, `case`,
|
||||
etc.
|
||||
|
||||
Returns a tuple of the form `{value, binding}`,
|
||||
@@ -150,7 +148,7 @@ defmodule Code do
|
||||
iex> Code.eval_string("a = a + b", [a: 1, b: 2])
|
||||
{3, [a: 3, b: 2]}
|
||||
|
||||
For convenience, you can pass `__ENV__/0` as the `opts` argument and
|
||||
For convenience, you can pass `__ENV__` as the `opts` argument and
|
||||
all imports, requires and aliases defined in the current environment
|
||||
will be automatically carried over:
|
||||
|
||||
@@ -161,25 +159,20 @@ defmodule Code do
|
||||
def eval_string(string, binding \\ [], opts \\ [])
|
||||
|
||||
def eval_string(string, binding, %Macro.Env{} = env) do
|
||||
{value, binding, _env, _scope} = :elixir.eval to_charlist(string), binding, Map.to_list(env)
|
||||
{value, binding, _env, _scope} = :elixir.eval to_char_list(string), binding, Map.to_list(env)
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
def eval_string(string, binding, opts) when is_list(opts) do
|
||||
validate_eval_opts(opts)
|
||||
{value, binding, _env, _scope} = :elixir.eval to_charlist(string), binding, opts
|
||||
{value, binding, _env, _scope} = :elixir.eval to_char_list(string), binding, opts
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evaluates the quoted contents.
|
||||
|
||||
**Warning**: Calling this function inside a macro is considered bad
|
||||
practice as it will attempt to evaluate runtime values at compile time.
|
||||
Macro arguments are typically transformed by unquoting them into the
|
||||
returned quoted expressions (instead of evaluated).
|
||||
|
||||
See `eval_string/3` for a description of bindings and options.
|
||||
See `eval_string/3` for a description of arguments and return values.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -187,7 +180,7 @@ defmodule Code do
|
||||
iex> Code.eval_quoted(contents, [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
|
||||
{3, [a: 1, b: 2]}
|
||||
|
||||
For convenience, you can pass `__ENV__/0` as the `opts` argument and
|
||||
For convenience, you can pass `__ENV__` as the `opts` argument and
|
||||
all options will be automatically extracted from the current environment:
|
||||
|
||||
iex> contents = quote(do: var!(a) + var!(b))
|
||||
@@ -254,9 +247,9 @@ defmodule Code do
|
||||
## Options
|
||||
|
||||
* `:file` - the filename to be used in stacktraces
|
||||
and the file reported in the `__ENV__/0` macro
|
||||
and the file reported in the `__ENV__` variable
|
||||
|
||||
* `:line` - the line reported in the `__ENV__/0` macro
|
||||
* `:line` - the line reported in the `__ENV__` variable
|
||||
|
||||
* `:existing_atoms_only` - when `true`, raises an error
|
||||
when non-existing atoms are found by the tokenizer
|
||||
@@ -270,7 +263,7 @@ defmodule Code do
|
||||
def string_to_quoted(string, opts \\ []) when is_list(opts) do
|
||||
file = Keyword.get opts, :file, "nofile"
|
||||
line = Keyword.get opts, :line, 1
|
||||
:elixir.string_to_quoted(to_charlist(string), line, file, opts)
|
||||
:elixir.string_to_quoted(to_char_list(string), line, file, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -286,7 +279,7 @@ defmodule Code do
|
||||
def string_to_quoted!(string, opts \\ []) when is_list(opts) do
|
||||
file = Keyword.get opts, :file, "nofile"
|
||||
line = Keyword.get opts, :line, 1
|
||||
:elixir.string_to_quoted!(to_charlist(string), line, file, opts)
|
||||
:elixir.string_to_quoted!(to_char_list(string), line, file, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -318,7 +311,7 @@ defmodule Code do
|
||||
|
||||
## Examples
|
||||
|
||||
Code.load_file("eex_test.exs", "../eex/test") |> List.first
|
||||
Code.load_file("eex_test.exs","../eex/test") |> List.first
|
||||
#=> {EExTest.Compiled, <<70, 79, 82, 49, ...>>}
|
||||
|
||||
"""
|
||||
@@ -350,11 +343,11 @@ defmodule Code do
|
||||
|
||||
If the code is already loaded, it returns `nil`:
|
||||
|
||||
Code.require_file("eex_test.exs", "../eex/test") #=> nil
|
||||
Code.require_file("eex_test.exs","../eex/test") #=> nil
|
||||
|
||||
If the code is not loaded yet, it returns the same as `load_file/2`:
|
||||
|
||||
Code.require_file("eex_test.exs", "../eex/test") |> List.first
|
||||
Code.require_file("eex_test.exs","../eex/test") |> List.first
|
||||
#=> {EExTest.Compiled, <<70, 79, 82, 49, ...>>}
|
||||
|
||||
"""
|
||||
@@ -396,12 +389,12 @@ defmodule Code do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.available_compiler_options
|
||||
[:docs, :debug_info, :ignore_module_conflict, :relative_paths, :warnings_as_errors]
|
||||
Code.available_compiler_options
|
||||
#=> [:docs, :debug_info, :ignore_module_conflict, :warnings_as_errors]
|
||||
|
||||
"""
|
||||
def available_compiler_options do
|
||||
[:docs, :debug_info, :ignore_module_conflict, :relative_paths, :warnings_as_errors]
|
||||
[:docs, :debug_info, :ignore_module_conflict, :warnings_as_errors]
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -421,11 +414,7 @@ defmodule Code do
|
||||
* `:ignore_module_conflict` - when `true`, override modules that were
|
||||
already defined without raising errors, `false` by default
|
||||
|
||||
* `:relative_paths` - when `true`, use relative paths in quoted nodes,
|
||||
warnings and errors generated by the compiler, `true` by default.
|
||||
Note disabling this option won't affect runtime warnings and errors.
|
||||
|
||||
* `:warnings_as_errors` - causes compilation to fail when warnings are
|
||||
* `:warnings_as_errors` - cause compilation to fail when warnings are
|
||||
generated
|
||||
|
||||
It returns the new list of compiler options.
|
||||
@@ -440,16 +429,9 @@ defmodule Code do
|
||||
def compiler_options(opts) do
|
||||
available = available_compiler_options()
|
||||
|
||||
Enum.each(opts, fn({key, value}) ->
|
||||
cond do
|
||||
not key in available ->
|
||||
raise "unknown compiler option: #{inspect(key)}"
|
||||
not is_boolean(value) ->
|
||||
raise "compiler option #{inspect(key)} should be a boolean, got: #{inspect(value)}"
|
||||
true ->
|
||||
:ok
|
||||
end
|
||||
end)
|
||||
for {k, _} <- opts,
|
||||
not k in available,
|
||||
do: raise "unknown compiler options: #{k}"
|
||||
|
||||
:elixir_config.update :compiler_options, &Enum.into(opts, &1)
|
||||
end
|
||||
@@ -463,7 +445,7 @@ defmodule Code do
|
||||
For compiling many files at once, check `Kernel.ParallelCompiler.files/2`.
|
||||
"""
|
||||
def compile_string(string, file \\ "nofile") when is_binary(file) do
|
||||
:elixir_compiler.string to_charlist(string), file
|
||||
:elixir_compiler.string to_char_list(string), file
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -523,8 +505,6 @@ defmodule Code do
|
||||
{:error, :nofile}
|
||||
|
||||
"""
|
||||
@spec ensure_loaded(module) ::
|
||||
{:module, module} | {:error, :embedded | :badfile | :nofile | :on_load_failure}
|
||||
def ensure_loaded(module) when is_atom(module) do
|
||||
:code.ensure_loaded(module)
|
||||
end
|
||||
@@ -542,7 +522,7 @@ defmodule Code do
|
||||
true
|
||||
|
||||
"""
|
||||
def ensure_loaded?(module) when is_atom(module) do
|
||||
def ensure_loaded?(module) do
|
||||
match?({:module, ^module}, ensure_loaded(module))
|
||||
end
|
||||
|
||||
@@ -559,16 +539,18 @@ defmodule Code do
|
||||
Check `ensure_loaded/1` for more information on module loading
|
||||
and when to use `ensure_loaded/1` or `ensure_compiled/1`.
|
||||
"""
|
||||
@spec ensure_compiled(module) ::
|
||||
{:module, module} | {:error, :embedded | :badfile | :nofile | :on_load_failure}
|
||||
def ensure_compiled(module) when is_atom(module) do
|
||||
case :code.ensure_loaded(module) do
|
||||
{:error, :nofile} = error ->
|
||||
if is_pid(:erlang.get(:elixir_compiler_pid)) and
|
||||
Kernel.ErrorHandler.ensure_compiled(module, :module) do
|
||||
{:module, module}
|
||||
else
|
||||
error
|
||||
case :erlang.get(:elixir_ensure_compiled) do
|
||||
:undefined -> error
|
||||
_ ->
|
||||
try do
|
||||
module.__info__(:module)
|
||||
{:module, module}
|
||||
rescue
|
||||
UndefinedFunctionError -> error
|
||||
end
|
||||
end
|
||||
other -> other
|
||||
end
|
||||
@@ -581,8 +563,7 @@ defmodule Code do
|
||||
is already loaded or was successfully loaded and compiled.
|
||||
Returns `false` otherwise.
|
||||
"""
|
||||
@spec ensure_compiled?(module) :: boolean
|
||||
def ensure_compiled?(module) when is_atom(module) do
|
||||
def ensure_compiled?(module) do
|
||||
match?({:module, ^module}, ensure_compiled(module))
|
||||
end
|
||||
|
||||
@@ -616,10 +597,11 @@ defmodule Code do
|
||||
|
||||
## Examples
|
||||
|
||||
# Get the module documentation
|
||||
iex> {_line, text} = Code.get_docs(Atom, :moduledoc)
|
||||
# Get the documentation for the first function listed
|
||||
iex> [fun|_] = Code.get_docs(Atom, :docs) |> Enum.sort()
|
||||
iex> {{_function, _arity}, _line, _kind, _signature, text} = fun
|
||||
iex> String.split(text, "\n") |> Enum.at(0)
|
||||
"Convenience functions for working with atoms."
|
||||
"Converts an atom to a char list."
|
||||
|
||||
# Module doesn't exist
|
||||
iex> Code.get_docs(ModuleNotGood, :all)
|
||||
@@ -638,7 +620,7 @@ defmodule Code do
|
||||
end
|
||||
|
||||
def get_docs(binpath, kind) when is_binary(binpath) and kind in @doc_kinds do
|
||||
do_get_docs(String.to_charlist(binpath), kind)
|
||||
do_get_docs(String.to_char_list(binpath), kind)
|
||||
end
|
||||
|
||||
@docs_chunk 'ExDc'
|
||||
|
||||
@@ -13,7 +13,7 @@ defprotocol Collectable do
|
||||
The `Enumerable` protocol is useful to take values out of a collection.
|
||||
In order to support a wide range of values, the functions provided by
|
||||
the `Enumerable` protocol do not keep shape. For example, passing a
|
||||
map to `Enum.map/2` always returns a list.
|
||||
dictionary to `Enum.map/2` always returns a list.
|
||||
|
||||
This design is intentional. `Enumerable` was designed to support infinite
|
||||
collections, resources and other structures with fixed shape. For example,
|
||||
@@ -49,7 +49,7 @@ end
|
||||
defimpl Collectable, for: List do
|
||||
def into(original) do
|
||||
{[], fn
|
||||
list, {:cont, x} -> [x | list]
|
||||
list, {:cont, x} -> [x|list]
|
||||
list, :done -> original ++ :lists.reverse(list)
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
@@ -59,7 +59,7 @@ end
|
||||
defimpl Collectable, for: BitString do
|
||||
def into(original) do
|
||||
{original, fn
|
||||
acc, {:cont, x} when is_bitstring(x) -> [acc | x]
|
||||
acc, {:cont, x} when is_bitstring(x) -> [acc|x]
|
||||
acc, :done -> IO.iodata_to_binary(acc)
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
|
||||
+37
-7
@@ -13,7 +13,36 @@ defmodule Dict do
|
||||
@type value :: any
|
||||
@type t :: list | map
|
||||
|
||||
# TODO: Deprecate every function by 1.4
|
||||
# TODO: Remove callbacks on 1.3
|
||||
# TODO: Deprecate every function on 1.3
|
||||
@callback new :: t
|
||||
@callback delete(t, key) :: t
|
||||
@callback drop(t, Enum.t) :: t
|
||||
@callback equal?(t, t) :: boolean
|
||||
@callback get(t, key) :: value
|
||||
@callback get(t, key, value) :: value
|
||||
@callback get_lazy(t, key, (() -> value)) :: value
|
||||
@callback get_and_update(t, key, (value -> {value, value})) :: {value, t}
|
||||
@callback fetch(t, key) :: {:ok, value} | :error
|
||||
@callback fetch!(t, key) :: value | no_return
|
||||
@callback has_key?(t, key) :: boolean
|
||||
@callback keys(t) :: [key]
|
||||
@callback merge(t, t) :: t
|
||||
@callback merge(t, t, (key, value, value -> value)) :: t
|
||||
@callback pop(t, key) :: {value, t}
|
||||
@callback pop(t, key, value) :: {value, t}
|
||||
@callback pop_lazy(t, key, (() -> value)) :: {value, t}
|
||||
@callback put(t, key, value) :: t
|
||||
@callback put_new(t, key, value) :: t
|
||||
@callback put_new_lazy(t, key, (() -> value)) :: t
|
||||
@callback size(t) :: non_neg_integer()
|
||||
@callback split(t, Enum.t) :: {t, t}
|
||||
@callback take(t, Enum.t) :: t
|
||||
@callback to_list(t) :: list()
|
||||
@callback update(t, key, value, (value -> value)) :: t
|
||||
@callback update!(t, key, (value -> value)) :: t | no_return
|
||||
@callback values(t) :: list(value)
|
||||
|
||||
defmacro __using__(_) do
|
||||
# Use this import to guarantee proper code expansion
|
||||
import Kernel, except: [size: 1]
|
||||
@@ -22,6 +51,8 @@ defmodule Dict do
|
||||
:elixir_errors.warn(line, file, "the Dict module is deprecated")
|
||||
|
||||
quote do
|
||||
@behaviour Dict
|
||||
|
||||
def get(dict, key, default \\ nil) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} -> value
|
||||
@@ -72,7 +103,7 @@ defmodule Dict do
|
||||
end
|
||||
|
||||
def take(dict, keys) do
|
||||
Enum.reduce(keys, new(), fn key, acc ->
|
||||
Enum.reduce(keys, new, fn key, acc ->
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} -> put(acc, key, value)
|
||||
:error -> acc
|
||||
@@ -82,19 +113,19 @@ defmodule Dict do
|
||||
|
||||
def to_list(dict) do
|
||||
reduce(dict, {:cont, []}, fn
|
||||
kv, acc -> {:cont, [kv | acc]}
|
||||
kv, acc -> {:cont, [kv|acc]}
|
||||
end) |> elem(1) |> :lists.reverse
|
||||
end
|
||||
|
||||
def keys(dict) do
|
||||
reduce(dict, {:cont, []}, fn
|
||||
{k, _}, acc -> {:cont, [k | acc]}
|
||||
{k, _}, acc -> {:cont, [k|acc]}
|
||||
end) |> elem(1) |> :lists.reverse
|
||||
end
|
||||
|
||||
def values(dict) do
|
||||
reduce(dict, {:cont, []}, fn
|
||||
{_, v}, acc -> {:cont, [v | acc]}
|
||||
{_, v}, acc -> {:cont, [v|acc]}
|
||||
end) |> elem(1) |> :lists.reverse
|
||||
end
|
||||
|
||||
@@ -166,7 +197,7 @@ defmodule Dict do
|
||||
end
|
||||
|
||||
def split(dict, keys) do
|
||||
Enum.reduce(keys, {new(), dict}, fn key, {inc, exc} = acc ->
|
||||
Enum.reduce(keys, {new, dict}, fn key, {inc, exc} = acc ->
|
||||
case fetch(exc, key) do
|
||||
{:ok, value} ->
|
||||
{put(inc, key, value), delete(exc, key)}
|
||||
@@ -361,7 +392,6 @@ defmodule Dict do
|
||||
target(dict).to_list(dict)
|
||||
end
|
||||
|
||||
@spec unsupported_dict(t) :: no_return
|
||||
defp unsupported_dict(dict) do
|
||||
raise ArgumentError, "unsupported dict: #{inspect dict}"
|
||||
end
|
||||
|
||||
+705
-1038
File diff suppressed because it is too large
Load Diff
+78
-158
@@ -7,7 +7,7 @@ defmodule Exception do
|
||||
`System.stacktrace/0` will return the stacktrace for the
|
||||
last throw/error/exit that occurred in the current process.
|
||||
|
||||
Do not rely on the particular format returned by the `format*`
|
||||
Do not rely on the particular format returned by the `format`
|
||||
functions in this module. They may be changed in future releases
|
||||
in order to better suit Elixir's tool chain. In other words,
|
||||
by using the functions in this module it is guaranteed you will
|
||||
@@ -15,11 +15,7 @@ defmodule Exception do
|
||||
"""
|
||||
|
||||
@typedoc "The exception type"
|
||||
@type t :: %{
|
||||
required(:__struct__) => module,
|
||||
required(:__exception__) => true,
|
||||
atom => any
|
||||
}
|
||||
@type t :: %{__struct__: module, __exception__: true}
|
||||
|
||||
@typedoc "The kind handled by formatting functions"
|
||||
@type kind :: :error | :exit | :throw | {:EXIT, pid}
|
||||
@@ -234,13 +230,12 @@ defmodule Exception do
|
||||
"shutdown: #{inspect(reason)}"
|
||||
end
|
||||
|
||||
defp format_exit_reason(:calling_self), do: "process attempted to call itself"
|
||||
defp format_exit_reason(:timeout), do: "time out"
|
||||
defp format_exit_reason(:killed), do: "killed"
|
||||
defp format_exit_reason(:noconnection), do: "no connection"
|
||||
|
||||
defp format_exit_reason(:noproc) do
|
||||
"no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started"
|
||||
"no process"
|
||||
end
|
||||
|
||||
defp format_exit_reason({:nodedown, node_name}) when is_atom(node_name) do
|
||||
@@ -370,10 +365,10 @@ defmodule Exception do
|
||||
end
|
||||
|
||||
defp format_application(module) do
|
||||
# We cannot use Application due to bootstrap issues
|
||||
case :application.get_application(module) do
|
||||
{:ok, app} -> "(" <> Atom.to_string(app) <> ") "
|
||||
:undefined -> ""
|
||||
if app = Application.get_application(module) do
|
||||
"(" <> Atom.to_string(app) <> ") "
|
||||
else
|
||||
""
|
||||
end
|
||||
end
|
||||
|
||||
@@ -384,13 +379,13 @@ defmodule Exception do
|
||||
is retrieved from `Process.info/2`.
|
||||
"""
|
||||
def format_stacktrace(trace \\ nil) do
|
||||
trace = trace || case Process.info(self(), :current_stacktrace) do
|
||||
trace = trace || case Process.info(self, :current_stacktrace) do
|
||||
{:current_stacktrace, t} -> Enum.drop(t, 3)
|
||||
end
|
||||
|
||||
case trace do
|
||||
[] -> "\n"
|
||||
_ -> " " <> Enum.map_join(trace, "\n ", &format_stacktrace_entry(&1)) <> "\n"
|
||||
s -> " " <> Enum.map_join(s, "\n ", &format_stacktrace_entry(&1)) <> "\n"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -468,7 +463,11 @@ defmodule Exception do
|
||||
""
|
||||
|
||||
"""
|
||||
def format_file_line(file, line, suffix \\ "") do
|
||||
def format_file_line(file, line) do
|
||||
format_file_line(file, line, "")
|
||||
end
|
||||
|
||||
defp format_file_line(file, line, suffix) do
|
||||
if file do
|
||||
if line && line != 0 do
|
||||
"#{file}:#{line}:#{suffix}"
|
||||
@@ -499,7 +498,11 @@ defmodule ArgumentError do
|
||||
end
|
||||
|
||||
defmodule ArithmeticError do
|
||||
defexception message: "bad argument in arithmetic expression"
|
||||
defexception []
|
||||
|
||||
def message(_) do
|
||||
"bad argument in arithmetic expression"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule SystemLimitError do
|
||||
@@ -511,7 +514,7 @@ defmodule SystemLimitError do
|
||||
end
|
||||
|
||||
defmodule SyntaxError do
|
||||
defexception [:file, :line, description: "syntax error"]
|
||||
defexception [file: nil, line: nil, description: "syntax error"]
|
||||
|
||||
def message(exception) do
|
||||
Exception.format_file_line(Path.relative_to_cwd(exception.file), exception.line) <>
|
||||
@@ -520,25 +523,25 @@ defmodule SyntaxError do
|
||||
end
|
||||
|
||||
defmodule TokenMissingError do
|
||||
defexception [:file, :line, description: "expression is incomplete"]
|
||||
defexception [file: nil, line: nil, description: "expression is incomplete"]
|
||||
|
||||
def message(%{file: file, line: line, description: description}) do
|
||||
Exception.format_file_line(file && Path.relative_to_cwd(file), line) <>
|
||||
" " <> description
|
||||
def message(exception) do
|
||||
Exception.format_file_line(Path.relative_to_cwd(exception.file), exception.line) <>
|
||||
" " <> exception.description
|
||||
end
|
||||
end
|
||||
|
||||
defmodule CompileError do
|
||||
defexception [:file, :line, description: "compile error"]
|
||||
defexception [file: nil, line: nil, description: "compile error"]
|
||||
|
||||
def message(%{file: file, line: line, description: description}) do
|
||||
Exception.format_file_line(file && Path.relative_to_cwd(file), line) <>
|
||||
" " <> description
|
||||
def message(exception) do
|
||||
Exception.format_file_line(Path.relative_to_cwd(exception.file), exception.line) <>
|
||||
" " <> exception.description
|
||||
end
|
||||
end
|
||||
|
||||
defmodule BadFunctionError do
|
||||
defexception [:term]
|
||||
defexception [term: nil]
|
||||
|
||||
def message(exception) do
|
||||
"expected a function, got: #{inspect(exception.term)}"
|
||||
@@ -546,7 +549,7 @@ defmodule BadFunctionError do
|
||||
end
|
||||
|
||||
defmodule BadStructError do
|
||||
defexception [:struct, :term]
|
||||
defexception [struct: nil, term: nil]
|
||||
|
||||
def message(exception) do
|
||||
"expected a struct named #{inspect(exception.struct)}, got: #{inspect(exception.term)}"
|
||||
@@ -554,23 +557,15 @@ defmodule BadStructError do
|
||||
end
|
||||
|
||||
defmodule BadMapError do
|
||||
defexception [:term]
|
||||
defexception [term: nil]
|
||||
|
||||
def message(exception) do
|
||||
"expected a map, got: #{inspect(exception.term)}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule BadBooleanError do
|
||||
defexception [:term, :operator]
|
||||
|
||||
def message(exception) do
|
||||
"expected a boolean on left-side of \"#{exception.operator}\", got: #{inspect(exception.term)}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule MatchError do
|
||||
defexception [:term]
|
||||
defexception [term: nil]
|
||||
|
||||
def message(exception) do
|
||||
"no match of right hand side value: #{inspect(exception.term)}"
|
||||
@@ -578,21 +573,13 @@ defmodule MatchError do
|
||||
end
|
||||
|
||||
defmodule CaseClauseError do
|
||||
defexception [:term]
|
||||
defexception [term: nil]
|
||||
|
||||
def message(exception) do
|
||||
"no case clause matching: #{inspect(exception.term)}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule WithClauseError do
|
||||
defexception [:term]
|
||||
|
||||
def message(exception) do
|
||||
"no with clause matching: #{inspect(exception.term)}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule CondClauseError do
|
||||
defexception []
|
||||
|
||||
@@ -602,7 +589,7 @@ defmodule CondClauseError do
|
||||
end
|
||||
|
||||
defmodule TryClauseError do
|
||||
defexception [:term]
|
||||
defexception [term: nil]
|
||||
|
||||
def message(exception) do
|
||||
"no try clause matching: #{inspect(exception.term)}"
|
||||
@@ -610,7 +597,7 @@ defmodule TryClauseError do
|
||||
end
|
||||
|
||||
defmodule BadArityError do
|
||||
defexception [:function, :args]
|
||||
defexception [function: nil, args: nil]
|
||||
|
||||
def message(exception) do
|
||||
fun = exception.function
|
||||
@@ -626,7 +613,7 @@ defmodule BadArityError do
|
||||
end
|
||||
|
||||
defmodule UndefinedFunctionError do
|
||||
defexception [:module, :function, :arity, :reason, :exports]
|
||||
defexception [module: nil, function: nil, arity: nil, reason: nil]
|
||||
|
||||
def message(%{reason: nil, module: module, function: function, arity: arity} = e) do
|
||||
cond do
|
||||
@@ -640,81 +627,27 @@ defmodule UndefinedFunctionError do
|
||||
end
|
||||
|
||||
def message(%{reason: :"module could not be loaded", module: module, function: function, arity: arity}) do
|
||||
"function " <> Exception.format_mfa(module, function, arity) <>
|
||||
" is undefined (module #{inspect module} is not available)"
|
||||
"undefined function " <> Exception.format_mfa(module, function, arity) <>
|
||||
" (module #{inspect module} is not available)"
|
||||
end
|
||||
|
||||
def message(%{reason: :"function not exported", module: module, function: function, arity: arity, exports: exports}) do
|
||||
suffix =
|
||||
if macro_exported?(module, function, arity) do
|
||||
". However there is a macro with the same name and arity." <>
|
||||
" Be sure to require #{inspect(module)} if you intend to invoke this macro"
|
||||
else
|
||||
did_you_mean(module, function, arity, exports)
|
||||
end
|
||||
|
||||
"function " <>
|
||||
Exception.format_mfa(module, function, arity) <>
|
||||
" is undefined or private" <>
|
||||
suffix
|
||||
def message(%{reason: :"function not exported", module: module, function: function, arity: arity}) do
|
||||
"undefined function " <> Exception.format_mfa(module, function, arity)
|
||||
end
|
||||
|
||||
def message(%{reason: :"function not available", module: module, function: function, arity: arity}) do
|
||||
"nil." <> fa = Exception.format_mfa(nil, function, arity)
|
||||
"function " <> Exception.format_mfa(module, function, arity) <>
|
||||
" is undefined (function #{fa} is not available)"
|
||||
"undefined function " <> Exception.format_mfa(module, function, arity) <>
|
||||
" (function #{fa} is not available)"
|
||||
end
|
||||
|
||||
def message(%{reason: reason, module: module, function: function, arity: arity}) do
|
||||
"function " <> Exception.format_mfa(module, function, arity) <> " is undefined (#{reason})"
|
||||
end
|
||||
|
||||
@function_threshold 0.77
|
||||
@max_suggestions 5
|
||||
|
||||
defp did_you_mean(module, function, _arity, exports) do
|
||||
exports = exports || exports_for(module)
|
||||
|
||||
result =
|
||||
case Keyword.take(exports, [function]) do
|
||||
[] ->
|
||||
base = Atom.to_string(function)
|
||||
for {key, val} <- exports,
|
||||
dist = String.jaro_distance(base, Atom.to_string(key)),
|
||||
dist >= @function_threshold,
|
||||
do: {dist, key, val}
|
||||
arities ->
|
||||
for {key, val} <- arities, do: {1.0, key, val}
|
||||
end
|
||||
|> Enum.sort(&elem(&1, 0) >= elem(&2, 0))
|
||||
|> Enum.take(@max_suggestions)
|
||||
|> Enum.sort(&elem(&1, 1) <= elem(&2, 1))
|
||||
|
||||
case result do
|
||||
[] -> ""
|
||||
suggestions -> ". Did you mean one of:\n\n#{Enum.map(suggestions, &format_fa/1)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp format_fa({_dist, fun, arity}) do
|
||||
fun = with ":" <> fun <- inspect(fun), do: fun
|
||||
" * " <> fun <> "/" <> Integer.to_string(arity) <> "\n"
|
||||
end
|
||||
|
||||
defp exports_for(module) do
|
||||
if function_exported?(module, :__info__, 1) do
|
||||
module.__info__(:macros) ++ module.__info__(:functions)
|
||||
else
|
||||
module.module_info(:exports)
|
||||
end
|
||||
rescue
|
||||
# In case the module was removed while we are computing this
|
||||
UndefinedFunctionError -> []
|
||||
"undefined function " <> Exception.format_mfa(module, function, arity) <> " (#{reason})"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule FunctionClauseError do
|
||||
defexception [:module, :function, :arity]
|
||||
defexception [module: nil, function: nil, arity: nil]
|
||||
|
||||
def message(exception) do
|
||||
if exception.function do
|
||||
@@ -736,19 +669,20 @@ defmodule Code.LoadError do
|
||||
end
|
||||
|
||||
defmodule Protocol.UndefinedError do
|
||||
defexception [:protocol, :value, description: ""]
|
||||
defexception [protocol: nil, value: nil, description: nil]
|
||||
|
||||
def message(exception) do
|
||||
msg = "protocol #{inspect exception.protocol} not implemented for #{inspect exception.value}"
|
||||
case exception.description do
|
||||
"" -> msg
|
||||
descr -> msg <> ", " <> descr
|
||||
if exception.description do
|
||||
msg <> ", " <> exception.description
|
||||
else
|
||||
msg
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule KeyError do
|
||||
defexception [:key, :term]
|
||||
defexception key: nil, term: nil
|
||||
|
||||
def message(exception) do
|
||||
msg = "key #{inspect exception.key} not found"
|
||||
@@ -774,59 +708,53 @@ defmodule UnicodeConversionError do
|
||||
"encoding starting at #{inspect rest}"
|
||||
end
|
||||
|
||||
defp detail([h | _]) when is_integer(h) do
|
||||
defp detail([h|_]) when is_integer(h) do
|
||||
"code point #{h}"
|
||||
end
|
||||
|
||||
defp detail([h | _]) do
|
||||
defp detail([h|_]) do
|
||||
detail(h)
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Enum.OutOfBoundsError do
|
||||
defexception message: "out of bounds error"
|
||||
defexception []
|
||||
|
||||
def message(_) do
|
||||
"out of bounds error"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Enum.EmptyError do
|
||||
defexception message: "empty error"
|
||||
defexception []
|
||||
|
||||
def message(_) do
|
||||
"empty error"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule File.Error do
|
||||
defexception [:reason, :path, action: ""]
|
||||
defexception [reason: nil, action: "", path: nil]
|
||||
|
||||
def message(%{action: action, reason: reason, path: path}) do
|
||||
formatted =
|
||||
case {action, reason} do
|
||||
{"remove directory", :eexist} ->
|
||||
"directory is not empty"
|
||||
_ ->
|
||||
IO.iodata_to_binary(:file.format_error(reason))
|
||||
end
|
||||
|
||||
"could not #{action} #{inspect(path)}: #{formatted}"
|
||||
def message(exception) do
|
||||
formatted = IO.iodata_to_binary(:file.format_error(exception.reason))
|
||||
"could not #{exception.action} #{exception.path}: #{formatted}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule File.CopyError do
|
||||
defexception [:reason, :source, :destination, on: "", action: ""]
|
||||
defexception [reason: nil, action: "", source: nil, destination: nil, on: nil]
|
||||
|
||||
def message(exception) do
|
||||
formatted =
|
||||
IO.iodata_to_binary(:file.format_error(exception.reason))
|
||||
|
||||
location =
|
||||
case exception.on do
|
||||
"" -> ""
|
||||
on -> ". #{on}"
|
||||
end
|
||||
|
||||
"could not #{exception.action} from #{inspect(exception.source)} to " <>
|
||||
"#{inspect(exception.destination)}#{location}: #{formatted}"
|
||||
formatted = IO.iodata_to_binary(:file.format_error(exception.reason))
|
||||
location = if on = exception.on, do: ". #{on}", else: ""
|
||||
"could not #{exception.action} from #{exception.source} to " <>
|
||||
"#{exception.destination}#{location}: #{formatted}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule ErlangError do
|
||||
defexception [:original]
|
||||
defexception [original: nil]
|
||||
|
||||
def message(exception) do
|
||||
"erlang error: #{inspect(exception.original)}"
|
||||
@@ -869,17 +797,13 @@ defmodule ErlangError do
|
||||
%BadMapError{term: term}
|
||||
end
|
||||
|
||||
def normalize({:badbool, op, term}, _stacktrace) do
|
||||
%BadBooleanError{operator: op, term: term}
|
||||
end
|
||||
|
||||
def normalize({:badkey, key}, stacktrace) do
|
||||
term =
|
||||
case stacktrace || :erlang.get_stacktrace do
|
||||
[{Map, :get_and_update!, [map, _, _], _} | _] -> map
|
||||
[{Map, :update!, [map, _, _], _} | _] -> map
|
||||
[{:maps, :update, [_, _, map], _} | _] -> map
|
||||
[{:maps, :get, [_, map], _} | _] -> map
|
||||
[{Map, :get_and_update!, [map, _, _], _}|_] -> map
|
||||
[{Map, :update!, [map, _, _], _}|_] -> map
|
||||
[{:maps, :update, [_, _, map], _}|_] -> map
|
||||
[{:maps, :get, [_, map], _}|_] -> map
|
||||
_ -> nil
|
||||
end
|
||||
%KeyError{key: key, term: term}
|
||||
@@ -893,10 +817,6 @@ defmodule ErlangError do
|
||||
%CaseClauseError{term: term}
|
||||
end
|
||||
|
||||
def normalize({:with_clause, term}, _stacktrace) do
|
||||
%WithClauseError{term: term}
|
||||
end
|
||||
|
||||
def normalize({:try_clause, term}, _stacktrace) do
|
||||
%TryClauseError{term: term}
|
||||
end
|
||||
@@ -920,11 +840,11 @@ defmodule ErlangError do
|
||||
%ErlangError{original: other}
|
||||
end
|
||||
|
||||
defp from_stacktrace([{module, function, args, _} | _]) when is_list(args) do
|
||||
defp from_stacktrace([{module, function, args, _}|_]) when is_list(args) do
|
||||
{module, function, length(args)}
|
||||
end
|
||||
|
||||
defp from_stacktrace([{module, function, arity, _} | _]) do
|
||||
defp from_stacktrace([{module, function, arity, _}|_]) do
|
||||
{module, function, arity}
|
||||
end
|
||||
|
||||
|
||||
+71
-97
@@ -20,7 +20,7 @@ defmodule File do
|
||||
`IO.write/2` functions must be used as they are responsible for
|
||||
doing the proper conversions and providing the proper data guarantees.
|
||||
|
||||
Note that filenames when given as charlists in Elixir are
|
||||
Note that filenames when given as char lists in Elixir are
|
||||
always treated as UTF-8. In particular, we expect that the
|
||||
shell and the operating system are configured to use UTF-8
|
||||
encoding. Binary filenames are considered raw and passed
|
||||
@@ -75,9 +75,9 @@ defmodule File do
|
||||
@type posix :: :file.posix()
|
||||
@type io_device :: :file.io_device()
|
||||
@type stat_options :: [time: :local | :universal | :posix]
|
||||
@type mode :: :append | :binary | :charlist | :compressed | :delayed_write | :exclusive |
|
||||
:raw | :read | :read_ahead | :sync | :utf8 | :write |
|
||||
{:encoding, :latin1 | :unicode | :utf8 | :utf16 | :utf32 |
|
||||
@type mode :: :append | :binary | :compressed | :delayed_write | :exclusive |
|
||||
:raw | :read | :read_ahead | :sync | :write |
|
||||
{:encoding, :latin1 | :unicode | :utf16 | :utf32 | :utf8 |
|
||||
{:utf16, :big | :little} | {:utf32, :big | :little}} |
|
||||
{:read_ahead, pos_integer} |
|
||||
{:delayed_write, non_neg_integer, non_neg_integer}
|
||||
@@ -135,7 +135,7 @@ defmodule File do
|
||||
directories of `path`
|
||||
* `:eexist` - there is already a file or directory named `path`
|
||||
* `:enoent` - a component of `path` does not exist
|
||||
* `:enospc` - there is no space left on the device
|
||||
* `:enospc` - there is a no space left on the device
|
||||
* `:enotdir` - a component of `path` is not a directory;
|
||||
on some platforms, `:enoent` is returned instead
|
||||
"""
|
||||
@@ -165,7 +165,7 @@ defmodule File do
|
||||
|
||||
* `:eacces` - missing search or write permissions for the parent
|
||||
directories of `path`
|
||||
* `:enospc` - there is no space left on the device
|
||||
* `:enospc` - there is a no space left on the device
|
||||
* `:enotdir` - a component of `path` is not a directory
|
||||
"""
|
||||
@spec mkdir_p(Path.t) :: :ok | {:error, posix}
|
||||
@@ -411,7 +411,7 @@ defmodule File do
|
||||
@doc """
|
||||
Copies the contents of `source` to `destination`.
|
||||
|
||||
Both parameters can be a filename or an IO device opened
|
||||
Both parameters can be a filename or an io device opened
|
||||
with `open/2`. `bytes_count` specifies the number of
|
||||
bytes to copy, the default being `:infinity`.
|
||||
|
||||
@@ -430,23 +430,22 @@ defmodule File do
|
||||
Typical error reasons are the same as in `open/2`,
|
||||
`read/1` and `write/3`.
|
||||
"""
|
||||
@spec copy(Path.t | io_device, Path.t | io_device, pos_integer | :infinity) :: {:ok, non_neg_integer} | {:error, posix}
|
||||
@spec copy(Path.t, Path.t, pos_integer | :infinity) :: {:ok, non_neg_integer} | {:error, posix}
|
||||
def copy(source, destination, bytes_count \\ :infinity) do
|
||||
F.copy(maybe_to_string(source), maybe_to_string(destination), bytes_count)
|
||||
F.copy(IO.chardata_to_string(source), IO.chardata_to_string(destination), bytes_count)
|
||||
end
|
||||
|
||||
@doc """
|
||||
The same as `copy/3` but raises an `File.CopyError` if it fails.
|
||||
Returns the `bytes_copied` otherwise.
|
||||
"""
|
||||
@spec copy!(Path.t | io_device, Path.t | io_device, pos_integer | :infinity) :: non_neg_integer | no_return
|
||||
@spec copy!(Path.t, Path.t, pos_integer | :infinity) :: non_neg_integer | no_return
|
||||
def copy!(source, destination, bytes_count \\ :infinity) do
|
||||
case copy(source, destination, bytes_count) do
|
||||
{:ok, bytes_count} -> bytes_count
|
||||
{:error, reason} ->
|
||||
raise File.CopyError, reason: reason, action: "copy",
|
||||
source: maybe_to_string(source),
|
||||
destination: maybe_to_string(destination)
|
||||
source: IO.chardata_to_string(source), destination: IO.chardata_to_string(destination)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -456,7 +455,7 @@ defmodule File do
|
||||
specify the `destination` filename, it is not sufficient to simply specify
|
||||
its directory.
|
||||
|
||||
Returns `:ok` in case of success, `{:error, reason}` otherwise.
|
||||
It returns `:ok` in case of success, returns `{:error, reason}` otherwise.
|
||||
|
||||
Note: The command `mv` in Unix systems behaves differently depending
|
||||
if `source` is a file and the `destination` is an existing directory.
|
||||
@@ -485,7 +484,7 @@ defmodule File do
|
||||
The function returns `:ok` in case of success, returns
|
||||
`{:error, reason}` otherwise.
|
||||
|
||||
If you want to copy contents from an IO device to another device
|
||||
If you want to copy contents from an io device to another device
|
||||
or do a straight copy from a source to a destination without
|
||||
preserving modes, check `copy/3` instead.
|
||||
|
||||
@@ -610,7 +609,7 @@ defmodule File do
|
||||
{:ok, files} ->
|
||||
case mkdir(dest) do
|
||||
success when success in [:ok, {:error, :eexist}] ->
|
||||
Enum.reduce(files, [dest | acc], fn(x, acc) ->
|
||||
Enum.reduce(files, [dest|acc], fn(x, acc) ->
|
||||
do_cp_r(Path.join(src, x), Path.join(dest, x), callback, acc)
|
||||
end)
|
||||
{:error, reason} -> {:error, reason, dest}
|
||||
@@ -637,13 +636,13 @@ defmodule File do
|
||||
case F.copy(src, {dest, [:exclusive]}) do
|
||||
{:ok, _} ->
|
||||
copy_file_mode!(src, dest)
|
||||
[dest | acc]
|
||||
[dest|acc]
|
||||
{:error, :eexist} ->
|
||||
if path_differs?(src, dest) and callback.(src, dest) do
|
||||
case copy(src, dest) do
|
||||
{:ok, _} ->
|
||||
copy_file_mode!(src, dest)
|
||||
[dest | acc]
|
||||
[dest|acc]
|
||||
{:error, reason} -> {:error, reason, src}
|
||||
end
|
||||
else
|
||||
@@ -657,13 +656,13 @@ defmodule File do
|
||||
defp do_cp_link(link, src, dest, callback, acc) do
|
||||
case F.make_symlink(link, dest) do
|
||||
:ok ->
|
||||
[dest | acc]
|
||||
[dest|acc]
|
||||
{:error, :eexist} ->
|
||||
if path_differs?(src, dest) and callback.(src, dest) do
|
||||
# If rm/1 fails, F.make_symlink/2 will fail
|
||||
_ = rm(dest)
|
||||
case F.make_symlink(link, dest) do
|
||||
:ok -> [dest | acc]
|
||||
:ok -> [dest|acc]
|
||||
{:error, reason} -> {:error, reason, src}
|
||||
end
|
||||
else
|
||||
@@ -680,9 +679,6 @@ defmodule File do
|
||||
contents are overwritten. Returns `:ok` if successful, or `{:error, reason}`
|
||||
if an error occurs.
|
||||
|
||||
`content` must be `iodata` (a list of bytes or a binary). Setting the
|
||||
encoding for this function has no effect.
|
||||
|
||||
**Warning:** Every time this function is invoked, a file descriptor is opened
|
||||
and a new process is spawned to write to the file. For this reason, if you are
|
||||
doing multiple writes in a loop, opening the file via `File.open/2` and using
|
||||
@@ -694,7 +690,7 @@ defmodule File do
|
||||
* `:enoent` - a component of the file name does not exist
|
||||
* `:enotdir` - a component of the file name is not a directory;
|
||||
on some platforms, `:enoent` is returned instead
|
||||
* `:enospc` - there is no space left on the device
|
||||
* `:enospc` - there is a no space left on the device
|
||||
* `:eacces` - missing permission for writing the file or searching one of
|
||||
the parent directories
|
||||
* `:eisdir` - the named file is a directory
|
||||
@@ -703,7 +699,6 @@ defmodule File do
|
||||
"""
|
||||
@spec write(Path.t, iodata, [mode]) :: :ok | {:error, posix}
|
||||
def write(path, content, modes \\ []) do
|
||||
modes = normalize_modes(modes, false)
|
||||
F.write_file(IO.chardata_to_string(path), content, modes)
|
||||
end
|
||||
|
||||
@@ -712,7 +707,6 @@ defmodule File do
|
||||
"""
|
||||
@spec write!(Path.t, iodata, [mode]) :: :ok | no_return
|
||||
def write!(path, content, modes \\ []) do
|
||||
modes = normalize_modes(modes, false)
|
||||
case F.write_file(path, content, modes) do
|
||||
:ok -> :ok
|
||||
{:error, reason} ->
|
||||
@@ -855,7 +849,7 @@ defmodule File do
|
||||
case res do
|
||||
{:ok, acc} ->
|
||||
case rmdir(path) do
|
||||
:ok -> {:ok, [path | acc]}
|
||||
:ok -> {:ok, [path|acc]}
|
||||
{:error, :enoent} -> res
|
||||
{:error, reason} -> {:error, reason, path}
|
||||
end
|
||||
@@ -875,19 +869,19 @@ defmodule File do
|
||||
|
||||
defp do_rm_regular(path, {:ok, acc} = entry) do
|
||||
case rm(path) do
|
||||
:ok -> {:ok, [path | acc]}
|
||||
:ok -> {:ok, [path|acc]}
|
||||
{:error, :enoent} -> entry
|
||||
{:error, reason} -> {:error, reason, path}
|
||||
end
|
||||
end
|
||||
|
||||
# On Windows, symlinks are treated as directory and must be removed
|
||||
# On windows, symlinks are treated as directory and must be removed
|
||||
# with rmdir/1. But on Unix, we remove them via rm/1. So we first try
|
||||
# to remove it as a directory and, if we get :enotdir, we fallback to
|
||||
# a file removal.
|
||||
defp do_rm_directory(path, {:ok, acc} = entry) do
|
||||
case rmdir(path) do
|
||||
:ok -> {:ok, [path | acc]}
|
||||
:ok -> {:ok, [path|acc]}
|
||||
{:error, :enotdir} -> do_rm_regular(path, entry)
|
||||
{:error, :enoent} -> entry
|
||||
{:error, reason} -> {:error, reason, path}
|
||||
@@ -925,26 +919,17 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Opens the given `path`.
|
||||
Opens the given `path` according to the given list of modes.
|
||||
|
||||
In order to write and read files, one must use the functions
|
||||
in the `IO` module. By default, a file is opened in `:binary` mode,
|
||||
in the `IO` module. By default, a file is opened in binary mode,
|
||||
which requires the functions `IO.binread/2` and `IO.binwrite/2`
|
||||
to interact with the file. A developer may pass `:utf8` as an
|
||||
option when opening the file and then all other functions from
|
||||
`IO` are available, since they work directly with Unicode data.
|
||||
|
||||
`modes_or_function` can either be a list of modes or a function. If it's a
|
||||
list, it's considered to be a list of modes (that are documented below). If
|
||||
it's a function, then it's equivalent to calling `open(path, [],
|
||||
modes_or_function)`. See the documentation for `open/3` for more information
|
||||
on this function.
|
||||
|
||||
The allowed modes:
|
||||
|
||||
* `:binary` - opens the file in binary mode, disabling special handling of unicode sequences
|
||||
(default mode).
|
||||
|
||||
* `:read` - the file, which must exist, is opened for reading.
|
||||
|
||||
* `:write` - the file is opened for writing. It is created if it does not
|
||||
@@ -960,8 +945,8 @@ defmodule File do
|
||||
* `:exclusive` - the file, when opened for writing, is created if it does
|
||||
not exist. If the file exists, open will return `{:error, :eexist}`.
|
||||
|
||||
* `:charlist` - when this term is given, read operations on the file will
|
||||
return charlists rather than binaries.
|
||||
* `:char_list` - when this term is given, read operations on the file will
|
||||
return char lists rather than binaries.
|
||||
|
||||
* `:compressed` - makes it possible to read or write gzip compressed files.
|
||||
|
||||
@@ -978,15 +963,14 @@ defmodule File do
|
||||
cannot cope with the character range of the data, an error occurs and the
|
||||
file will be closed.
|
||||
|
||||
* `:delayed_write`, `:raw`, `:ram`, `:read_ahead`, `:sync`, `{:encoding, ...}`,
|
||||
`{:read_ahead, pos_integer}`, `{:delayed_write, non_neg_integer, non_neg_integer}` -
|
||||
for more information about these options see [`:file.open/2`](http://www.erlang.org/doc/man/file.html#open-2).
|
||||
For more information about other options like `:read_ahead` and `:delayed_write`,
|
||||
see [`:file.open/2`](http://www.erlang.org/doc/man/file.html#open-2).
|
||||
|
||||
This function returns:
|
||||
|
||||
* `{:ok, io_device}` - the file has been opened in the requested mode.
|
||||
|
||||
`io_device` is actually the PID of the process which handles the file.
|
||||
`io_device` is actually the pid of the process which handles the file.
|
||||
This process is linked to the process which originally opened the file.
|
||||
If any process to which the `io_device` is linked terminates, the file
|
||||
will be closed and the process itself will be terminated.
|
||||
@@ -1005,13 +989,13 @@ defmodule File do
|
||||
"""
|
||||
@spec open(Path.t, [mode | :ram]) :: {:ok, io_device} | {:error, posix}
|
||||
@spec open(Path.t, (io_device -> res)) :: {:ok, res} | {:error, posix} when res: var
|
||||
def open(path, modes_or_function \\ [])
|
||||
def open(path, modes \\ [])
|
||||
|
||||
def open(path, modes) when is_list(modes) do
|
||||
F.open(IO.chardata_to_string(path), normalize_modes(modes, true))
|
||||
F.open(IO.chardata_to_string(path), open_defaults(modes, true))
|
||||
end
|
||||
|
||||
def open(path, function) when is_function(function, 1) do
|
||||
def open(path, function) when is_function(function) do
|
||||
open(path, [], function)
|
||||
end
|
||||
|
||||
@@ -1022,7 +1006,7 @@ defmodule File do
|
||||
automatically closed after the function returns, regardless
|
||||
if there was an error when executing the function.
|
||||
|
||||
Returns `{:ok, function_result}` in case of success,
|
||||
It returns `{:ok, function_result}` in case of success,
|
||||
`{:error, reason}` otherwise.
|
||||
|
||||
This function expects the file to be closed with success,
|
||||
@@ -1036,51 +1020,43 @@ defmodule File do
|
||||
IO.read(file, :line)
|
||||
end)
|
||||
|
||||
See `open/2` for the list of available `modes`.
|
||||
"""
|
||||
@spec open(Path.t, [mode | :ram], (io_device -> res)) :: {:ok, res} | {:error, posix} when res: var
|
||||
def open(path, modes, function) when is_list(modes) and is_function(function, 1) do
|
||||
def open(path, modes, function) do
|
||||
case open(path, modes) do
|
||||
{:ok, io_device} ->
|
||||
{:ok, device} ->
|
||||
try do
|
||||
{:ok, function.(io_device)}
|
||||
{:ok, function.(device)}
|
||||
after
|
||||
:ok = close(io_device)
|
||||
:ok = close(device)
|
||||
end
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Similar to `open/2` but raises an error if file could not be opened.
|
||||
Same as `open/2` but raises an error if file could not be opened.
|
||||
|
||||
Returns the IO device otherwise.
|
||||
|
||||
See `open/2` for the list of available modes.
|
||||
Returns the `io_device` otherwise.
|
||||
"""
|
||||
@spec open!(Path.t, [mode | :ram]) :: io_device | no_return
|
||||
@spec open!(Path.t, (io_device -> res)) :: res | no_return when res: var
|
||||
def open!(path, modes_or_function \\ []) do
|
||||
case open(path, modes_or_function) do
|
||||
{:ok, io_device_or_function_result} ->
|
||||
io_device_or_function_result
|
||||
@spec open!(Path.t, [mode]) :: io_device | no_return
|
||||
def open!(path, modes \\ []) do
|
||||
case open(path, modes) do
|
||||
{:ok, device} -> device
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "open", path: IO.chardata_to_string(path)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Similar to `open/3` but raises an error if file could not be opened.
|
||||
Same as `open/3` but raises an error if file could not be opened.
|
||||
|
||||
If it succeeds opening the file, it returns the `function` result on the IO device.
|
||||
|
||||
See `open/2` for the list of available `modes`.
|
||||
Returns the function result otherwise.
|
||||
"""
|
||||
@spec open!(Path.t, [mode | :ram], (io_device -> res)) :: res | no_return when res: var
|
||||
def open!(path, modes, function) do
|
||||
case open(path, modes, function) do
|
||||
{:ok, function_result} ->
|
||||
function_result
|
||||
{:ok, device} -> device
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "open", path: IO.chardata_to_string(path)
|
||||
end
|
||||
@@ -1156,7 +1132,7 @@ defmodule File do
|
||||
"""
|
||||
@spec cd!(Path.t, (() -> res)) :: res | no_return when res: var
|
||||
def cd!(path, function) do
|
||||
old = cwd!()
|
||||
old = cwd!
|
||||
cd!(path)
|
||||
try do
|
||||
function.()
|
||||
@@ -1168,7 +1144,7 @@ defmodule File do
|
||||
@doc """
|
||||
Returns the list of files in the given directory.
|
||||
|
||||
Returns `{:ok, [files]}` in case of success,
|
||||
It returns `{:ok, [files]}` in case of success,
|
||||
`{:error, reason}` otherwise.
|
||||
"""
|
||||
@spec ls(Path.t) :: {:ok, [binary]} | {:error, posix}
|
||||
@@ -1199,7 +1175,7 @@ defmodule File do
|
||||
|
||||
Note that if the option `:delayed_write` was used when opening the file,
|
||||
`close/1` might return an old write error and not even try to close the file.
|
||||
See `open/2` for more information.
|
||||
See `open/2`.
|
||||
"""
|
||||
@spec close(io_device) :: :ok | {:error, posix | :badarg | :terminated}
|
||||
def close(io_device) do
|
||||
@@ -1212,7 +1188,7 @@ defmodule File do
|
||||
The stream implements both `Enumerable` and `Collectable` protocols,
|
||||
which means it can be used both for read and write.
|
||||
|
||||
The `line_or_bytes` argument configures how the file is read when
|
||||
The `line_or_byte` argument configures how the file is read when
|
||||
streaming, by `:line` (default) or by a given number of bytes.
|
||||
|
||||
Operating the stream can fail on open for the same reasons as
|
||||
@@ -1227,7 +1203,7 @@ defmodule File do
|
||||
in raw mode for performance reasons. Therefore, Elixir **will** open
|
||||
streams in `:raw` mode with the `:read_ahead` option unless an encoding
|
||||
is specified. This means any data streamed into the file must be
|
||||
converted to `t:iodata/0` type. If you pass `[:utf8]` in the modes parameter,
|
||||
converted to `iodata` type. If you pass `[:utf8]` in the modes parameter,
|
||||
the underlying stream will use `IO.write/2` and the `String.Chars` protocol
|
||||
to convert the data. See `IO.binwrite/2` and `IO.write/2` .
|
||||
|
||||
@@ -1238,14 +1214,14 @@ defmodule File do
|
||||
|
||||
# Read in 2048 byte chunks rather than lines
|
||||
File.stream!("./test/test.data", [], 2048)
|
||||
#=> %File.Stream{line_or_bytes: 2048, modes: [:raw, :read_ahead, :binary],
|
||||
#=> %File.Stream{line_or_bytes: 2048, modes: [:raw, :read_ahead, :binary],
|
||||
#=> path: "./test/test.data", raw: true}
|
||||
|
||||
See `Stream.run/1` for an example of streaming into a file.
|
||||
|
||||
"""
|
||||
def stream!(path, modes \\ [], line_or_bytes \\ :line) do
|
||||
modes = normalize_modes(modes, true)
|
||||
modes = open_defaults(modes, true)
|
||||
File.Stream.__build__(IO.chardata_to_string(path), modes, line_or_bytes)
|
||||
end
|
||||
|
||||
@@ -1339,26 +1315,24 @@ defmodule File do
|
||||
|
||||
## Helpers
|
||||
|
||||
@read_ahead_size 64 * 1024
|
||||
@read_ahead 64*1024
|
||||
|
||||
defp normalize_modes([:utf8 | rest], binary?) do
|
||||
[encoding: :utf8] ++ normalize_modes(rest, binary?)
|
||||
defp open_defaults([:char_list|t], _add_binary) do
|
||||
open_defaults(t, false)
|
||||
end
|
||||
defp normalize_modes([:read_ahead | rest], binary?) do
|
||||
[read_ahead: @read_ahead_size] ++ normalize_modes(rest, binary?)
|
||||
end
|
||||
# TODO: Deprecate :char_list mode by v1.5
|
||||
defp normalize_modes([mode | rest], _binary?) when mode in [:charlist, :char_list] do
|
||||
normalize_modes(rest, false)
|
||||
end
|
||||
defp normalize_modes([mode | rest], binary?) do
|
||||
[mode | normalize_modes(rest, binary?)]
|
||||
end
|
||||
defp normalize_modes([], true), do: [:binary]
|
||||
defp normalize_modes([], false), do: []
|
||||
|
||||
defp maybe_to_string(path) when is_pid(path),
|
||||
do: path
|
||||
defp maybe_to_string(path),
|
||||
do: IO.chardata_to_string(path)
|
||||
defp open_defaults([:utf8|t], add_binary) do
|
||||
open_defaults([{:encoding, :utf8}|t], add_binary)
|
||||
end
|
||||
|
||||
defp open_defaults([:read_ahead|t], add_binary) do
|
||||
open_defaults([{:read_ahead, @read_ahead}|t], add_binary)
|
||||
end
|
||||
|
||||
defp open_defaults([h|t], add_binary) do
|
||||
[h|open_defaults(t, add_binary)]
|
||||
end
|
||||
|
||||
defp open_defaults([], true), do: [:binary]
|
||||
defp open_defaults([], false), do: []
|
||||
end
|
||||
|
||||
@@ -32,7 +32,7 @@ defmodule File.Stat do
|
||||
systems which have no concept of links.
|
||||
|
||||
* `major_device` - identifies the file system where the file is located.
|
||||
In Windows, the number indicates a drive as follows: 0 means A:, 1 means
|
||||
In windows, the number indicates a drive as follows: 0 means A:, 1 means
|
||||
B:, and so on.
|
||||
|
||||
* `minor_device` - only valid for character devices on Unix. In all other
|
||||
|
||||
@@ -20,15 +20,14 @@ defmodule File.Stream do
|
||||
raw = :lists.keyfind(:encoding, 1, modes) == false
|
||||
|
||||
modes =
|
||||
case raw do
|
||||
true ->
|
||||
if :lists.keyfind(:read_ahead, 1, modes) == {:read_ahead, false} do
|
||||
[:raw | modes]
|
||||
else
|
||||
[:raw, :read_ahead | modes]
|
||||
end
|
||||
false ->
|
||||
modes
|
||||
if raw do
|
||||
if :lists.keyfind(:read_ahead, 1, modes) == {:read_ahead, false} do
|
||||
[:raw|modes]
|
||||
else
|
||||
[:raw, :read_ahead|modes]
|
||||
end
|
||||
else
|
||||
modes
|
||||
end
|
||||
|
||||
%File.Stream{path: path, modes: modes, raw: raw, line_or_bytes: line_or_bytes}
|
||||
@@ -38,7 +37,7 @@ defmodule File.Stream do
|
||||
def into(%{path: path, modes: modes, raw: raw} = stream) do
|
||||
modes = for mode <- modes, not mode in [:read], do: mode
|
||||
|
||||
case :file.open(path, [:write | modes]) do
|
||||
case :file.open(path, [:write|modes]) do
|
||||
{:ok, device} ->
|
||||
{:ok, into(device, stream, raw)}
|
||||
{:error, reason} ->
|
||||
|
||||
+76
-247
@@ -5,29 +5,27 @@ defmodule Float do
|
||||
Functions for working with floating point numbers.
|
||||
"""
|
||||
|
||||
import Bitwise
|
||||
|
||||
@power_of_2_to_52 4503599627370496
|
||||
|
||||
@doc """
|
||||
Parses a binary into a float.
|
||||
|
||||
If successful, returns a tuple in the form of `{float, remainder_of_binary}`;
|
||||
If successful, returns a tuple of the form `{float, remainder_of_binary}`;
|
||||
when the binary cannot be coerced into a valid float, the atom `:error` is
|
||||
returned.
|
||||
|
||||
If the size of float exceeds the maximum size of `1.7976931348623157e+308`,
|
||||
the `ArgumentError` exception is raised.
|
||||
|
||||
If you want to convert a string-formatted float directly to a float,
|
||||
If a float formatted string wants to be directly converted to a float,
|
||||
`String.to_float/1` can be used instead.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.parse("34")
|
||||
{34.0, ""}
|
||||
|
||||
iex> Float.parse("34.25")
|
||||
{34.25, ""}
|
||||
|
||||
iex> Float.parse("56.5xyz")
|
||||
{56.5, "xyz"}
|
||||
|
||||
@@ -80,18 +78,6 @@ defmodule Float do
|
||||
|
||||
`floor/2` also accepts a precision to round a floating point value down
|
||||
to an arbitrary number of fractional digits (between 0 and 15).
|
||||
The operation is performed on the binary floating point, without a
|
||||
conversion to decimal.
|
||||
|
||||
The behaviour of `floor/2` for floats can be surprising. For example:
|
||||
|
||||
iex> Float.floor(12.52, 2)
|
||||
12.51
|
||||
|
||||
One may have expected it to floor to 12.52. This is not a bug.
|
||||
Most decimal fractions cannot be represented as a binary floating point
|
||||
and therefore the number above is internally represented as 12.51999999,
|
||||
which explains the behaviour above.
|
||||
|
||||
This function always returns a float. `Kernel.trunc/1` may be used instead to
|
||||
truncate the result to an integer afterwards.
|
||||
@@ -100,15 +86,21 @@ defmodule Float do
|
||||
|
||||
iex> Float.floor(34.25)
|
||||
34.0
|
||||
|
||||
iex> Float.floor(-56.5)
|
||||
-57.0
|
||||
|
||||
iex> Float.floor(34.259, 2)
|
||||
34.25
|
||||
|
||||
"""
|
||||
@spec floor(float, 0..15) :: float
|
||||
def floor(number, precision \\ 0) when is_float(number) and precision in 0..15 do
|
||||
round(number, precision, :floor)
|
||||
power = power_of_10(precision)
|
||||
number = number * power
|
||||
truncated = trunc(number)
|
||||
variance = if number - truncated < 0, do: -1.0, else: 0.0
|
||||
(truncated + variance) / power
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -117,19 +109,6 @@ defmodule Float do
|
||||
`ceil/2` also accepts a precision to round a floating point value down
|
||||
to an arbitrary number of fractional digits (between 0 and 15).
|
||||
|
||||
The operation is performed on the binary floating point, without a
|
||||
conversion to decimal.
|
||||
|
||||
The behaviour of `ceil/2` for floats can be surprising. For example:
|
||||
|
||||
iex> Float.ceil(-12.52, 2)
|
||||
-12.51
|
||||
|
||||
One may have expected it to ceil to -12.52. This is not a bug.
|
||||
Most decimal fractions cannot be represented as a binary floating point
|
||||
and therefore the number above is internally represented as -12.51999999,
|
||||
which explains the behaviour above.
|
||||
|
||||
This function always returns floats. `Kernel.trunc/1` may be used instead to
|
||||
truncate the result to an integer afterwards.
|
||||
|
||||
@@ -137,286 +116,136 @@ defmodule Float do
|
||||
|
||||
iex> Float.ceil(34.25)
|
||||
35.0
|
||||
|
||||
iex> Float.ceil(-56.5)
|
||||
-56.0
|
||||
|
||||
iex> Float.ceil(34.251, 2)
|
||||
34.26
|
||||
|
||||
"""
|
||||
@spec ceil(float, 0..15) :: float
|
||||
def ceil(number, precision \\ 0) when is_float(number) and precision in 0..15 do
|
||||
round(number, precision, :ceil)
|
||||
power = power_of_10(precision)
|
||||
number = number * power
|
||||
truncated = trunc(number)
|
||||
variance = if number - truncated > 0, do: 1.0, else: 0.0
|
||||
(truncated + variance) / power
|
||||
end
|
||||
|
||||
@doc """
|
||||
Rounds a floating point value to an arbitrary number of fractional
|
||||
digits (between 0 and 15).
|
||||
|
||||
The rounding direction always ties to half up. The operation is
|
||||
performed on the binary floating point, without a conversion to decimal.
|
||||
Rounds a floating point value to an arbitrary number of fractional digits
|
||||
(between 0 and 15).
|
||||
|
||||
This function only accepts floats and always returns a float. Use
|
||||
`Kernel.round/1` if you want a function that accepts both floats
|
||||
and integers and always returns an integer.
|
||||
|
||||
The behaviour of `round/2` for floats can be surprising. For example:
|
||||
|
||||
iex> Float.round(5.5675, 3)
|
||||
5.567
|
||||
|
||||
One may have expected it to round to the half up 5.568. This is not a bug.
|
||||
Most decimal fractions cannot be represented as a binary floating point
|
||||
and therefore the number above is internally represented as 5.567499999,
|
||||
which explains the behaviour above. If you want exact rounding for decimals,
|
||||
you must use a decimal library. The behaviour above is also in accordance
|
||||
to reference implementations, such as "Correctly Rounded Binary-Decimal and
|
||||
Decimal-Binary Conversions" by David M. Gay.
|
||||
`Kernel.round/1` if you want a function that accepts both floats and integers
|
||||
and always returns an integer.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.round(12.5)
|
||||
13.0
|
||||
iex> Float.round(5.5674, 3)
|
||||
5.567
|
||||
|
||||
iex> Float.round(5.5675, 3)
|
||||
5.567
|
||||
5.568
|
||||
|
||||
iex> Float.round(-5.5674, 3)
|
||||
-5.567
|
||||
iex> Float.round(-5.5675)
|
||||
-6.0
|
||||
iex> Float.round(12.341444444444441, 15)
|
||||
12.341444444444441
|
||||
|
||||
iex> Float.round(-5.5675, 3)
|
||||
-5.568
|
||||
|
||||
"""
|
||||
@spec round(float, 0..15) :: float
|
||||
|
||||
# This implementation is slow since it relies on big integers.
|
||||
# Faster implementations are available on more recent papers
|
||||
# and could be implemented in the future.
|
||||
def round(float, precision \\ 0) when is_float(float) and precision in 0..15 do
|
||||
round(float, precision, :half_up)
|
||||
def round(number, precision \\ 0) when is_float(number) and precision in 0..15 do
|
||||
power = power_of_10(precision)
|
||||
Kernel.round(number * power) / power
|
||||
end
|
||||
|
||||
defp round(float, precision, rounding) do
|
||||
<<sign::1, exp::11, significant::52-bitstring>> = <<float::float>>
|
||||
{num, count, _} = decompose(significant)
|
||||
count = count - exp + 1023
|
||||
|
||||
cond do
|
||||
count <= 0 or # There is no decimal precision
|
||||
(0 == exp and <<0::52>> == significant) -> #zero or minus zero
|
||||
float
|
||||
|
||||
count >= 104 -> # Precision beyond 15 digits
|
||||
case rounding do
|
||||
:ceil when sign === 0 -> 1 / power_of_10(precision)
|
||||
:floor when sign === 1 -> -1 / power_of_10(precision)
|
||||
_ -> 0.0
|
||||
end
|
||||
|
||||
count <= precision -> # We are asking more precision than we have
|
||||
float
|
||||
|
||||
true ->
|
||||
# Difference in precision between float and asked precision
|
||||
# We subtract 1 because we need to calculate the remainder too
|
||||
diff = count - precision - 1
|
||||
|
||||
# Get up to latest so we calculate the remainder
|
||||
power_of_10 = power_of_10(diff)
|
||||
|
||||
# Convert the numerand to decimal base
|
||||
num = num * power_of_5(count)
|
||||
|
||||
# Move to the given precision - 1
|
||||
num = div(num, power_of_10)
|
||||
div = div(num, 10)
|
||||
num = rounding(rounding, sign, num, div)
|
||||
|
||||
# Convert back to float without loss
|
||||
# http://www.exploringbinary.com/correct-decimal-to-floating-point-using-big-integers/
|
||||
den = power_of_10(precision)
|
||||
boundary = den <<< 52
|
||||
|
||||
cond do
|
||||
num == 0 ->
|
||||
0.0
|
||||
num >= boundary ->
|
||||
{den, exp} = scale_down(num, boundary, 52)
|
||||
decimal_to_float(sign, num, den, exp)
|
||||
true ->
|
||||
{num, exp} = scale_up(num, boundary, 52)
|
||||
decimal_to_float(sign, num, den, exp)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp scale_up(num, boundary, exp) when num >= boundary, do: {num, exp}
|
||||
defp scale_up(num, boundary, exp), do: scale_up(num <<< 1, boundary, exp - 1)
|
||||
|
||||
defp scale_down(num, den, exp) do
|
||||
new_den = den <<< 1
|
||||
if num < new_den do
|
||||
{den >>> 52, exp}
|
||||
else
|
||||
scale_down(num, new_den, exp + 1)
|
||||
end
|
||||
end
|
||||
|
||||
defp decimal_to_float(sign, num, den, exp) do
|
||||
quo = div(num, den)
|
||||
rem = num - quo * den
|
||||
|
||||
tmp =
|
||||
case den >>> 1 do
|
||||
den when rem > den -> quo + 1
|
||||
den when rem < den -> quo
|
||||
_ when (quo &&& 1) === 1 -> quo + 1
|
||||
_ -> quo
|
||||
end
|
||||
|
||||
tmp = tmp - @power_of_2_to_52
|
||||
<<tmp::float>> = <<sign::1, (exp + 1023)::11, tmp::52>>
|
||||
tmp
|
||||
end
|
||||
|
||||
defp rounding(:floor, 1, _num, div), do: div + 1
|
||||
defp rounding(:ceil, 0, _num, div), do: div + 1
|
||||
defp rounding(:half_up, _sign, num, div) do
|
||||
case rem(num, 10) do
|
||||
rem when rem < 5 -> div
|
||||
rem when rem >= 5 -> div + 1
|
||||
end
|
||||
end
|
||||
defp rounding(_, _, _, div), do: div
|
||||
|
||||
Enum.reduce 0..104, 1, fn x, acc ->
|
||||
Enum.reduce 0..15, 1, fn x, acc ->
|
||||
defp power_of_10(unquote(x)), do: unquote(acc)
|
||||
acc * 10
|
||||
end
|
||||
|
||||
Enum.reduce 0..104, 1, fn x, acc ->
|
||||
defp power_of_5(unquote(x)), do: unquote(acc)
|
||||
acc * 5
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a pair of integers whose ratio is exactly equal
|
||||
to the original float and with a positive denominator.
|
||||
Returns a char list which corresponds to the text representation of the given float.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.ratio(3.14)
|
||||
{7070651414971679, 2251799813685248}
|
||||
iex> Float.ratio(-3.14)
|
||||
{-7070651414971679, 2251799813685248}
|
||||
iex> Float.ratio(1.5)
|
||||
{3, 2}
|
||||
iex> Float.ratio(-1.5)
|
||||
{-3, 2}
|
||||
iex> Float.ratio(16.0)
|
||||
{16, 1}
|
||||
iex> Float.ratio(-16.0)
|
||||
{-16, 1}
|
||||
iex> Float.to_char_list(7.0)
|
||||
'7.00000000000000000000e+00'
|
||||
|
||||
"""
|
||||
def ratio(float) when is_float(float) do
|
||||
<<sign::1, exp::11, significant::52-bitstring>> = <<float::float>>
|
||||
{num, _, den} = decompose(significant)
|
||||
num = sign(sign, num)
|
||||
case exp - 1023 do
|
||||
exp when exp > 0 ->
|
||||
{den, exp} = shift_right(den, exp)
|
||||
{shift_left(num, exp), den}
|
||||
exp when exp < 0 ->
|
||||
{num, shift_left(den, -exp)}
|
||||
0 ->
|
||||
{num, den}
|
||||
end
|
||||
@spec to_char_list(float) :: char_list
|
||||
def to_char_list(float) do
|
||||
:erlang.float_to_list(float)
|
||||
end
|
||||
|
||||
defp decompose(significant) do
|
||||
decompose(significant, 1, 0, 2, 1, 1)
|
||||
end
|
||||
|
||||
defp decompose(<<1::1, bits::bitstring>>, count, last_count, power, _last_power, acc) do
|
||||
decompose(bits, count + 1, count, power <<< 1, power, shift_left(acc, count - last_count) + 1)
|
||||
end
|
||||
defp decompose(<<0::1, bits::bitstring>>, count, last_count, power, last_power, acc) do
|
||||
decompose(bits, count + 1, last_count, power <<< 1, last_power, acc)
|
||||
end
|
||||
defp decompose(<<>>, _count, last_count, _power, last_power, acc) do
|
||||
{acc, last_count, last_power}
|
||||
end
|
||||
|
||||
defp sign(0, num), do: num
|
||||
defp sign(1, num), do: -num
|
||||
|
||||
defp shift_left(num, 0), do: num
|
||||
defp shift_left(num, times), do: shift_left(num <<< 1, times - 1)
|
||||
|
||||
defp shift_right(num, 0), do: {num, 0}
|
||||
defp shift_right(1, times), do: {1, times}
|
||||
defp shift_right(num, times), do: shift_right(num >>> 1, times - 1)
|
||||
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the text representation
|
||||
Returns a list which corresponds to the text representation
|
||||
of the given float.
|
||||
|
||||
It uses the shortest representation according to algorithm described
|
||||
in "Printing Floating-Point Numbers Quickly and Accurately" in
|
||||
Proceedings of the SIGPLAN '96 Conference on Programming Language
|
||||
Design and Implementation.
|
||||
## Options
|
||||
|
||||
* `:decimals` - number of decimal points to show
|
||||
* `:scientific` - number of decimal points to show, in scientific format
|
||||
* `:compact` - when `true`, use the most compact representation (ignored
|
||||
with the `scientific` option)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_charlist(7.0)
|
||||
'7.0'
|
||||
iex> Float.to_char_list 7.1, [decimals: 2, compact: true]
|
||||
'7.1'
|
||||
|
||||
"""
|
||||
@spec to_charlist(float) :: charlist
|
||||
def to_charlist(float) when is_float(float) do
|
||||
:io_lib_format.fwrite_g(float)
|
||||
@spec to_char_list(float, list) :: char_list
|
||||
def to_char_list(float, options) do
|
||||
:erlang.float_to_list(float, expand_compact(options))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of the given float.
|
||||
|
||||
It uses the shortest representation according to algorithm described
|
||||
in "Printing Floating-Point Numbers Quickly and Accurately" in
|
||||
Proceedings of the SIGPLAN '96 Conference on Programming Language
|
||||
Design and Implementation.
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_string(7.0)
|
||||
"7.0"
|
||||
"7.00000000000000000000e+00"
|
||||
|
||||
"""
|
||||
@spec to_string(float) :: String.t
|
||||
def to_string(float) when is_float(float) do
|
||||
IO.iodata_to_binary(:io_lib_format.fwrite_g(float))
|
||||
def to_string(float) do
|
||||
:erlang.float_to_binary(float)
|
||||
end
|
||||
|
||||
# TODO: Deprecate by v1.5
|
||||
@doc false
|
||||
def to_char_list(float), do: Float.to_charlist(float)
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `float`.
|
||||
|
||||
@doc false
|
||||
def to_char_list(float, options) do
|
||||
IO.warn "Float.to_char_list/2 is deprecated, use :erlang.float_to_list/2 instead"
|
||||
:erlang.float_to_list(float, expand_compact(options))
|
||||
end
|
||||
## Options
|
||||
|
||||
@doc false
|
||||
* `:decimals` - number of decimal points to show
|
||||
* `:scientific` - number of decimal points to show, in scientific format
|
||||
* `:compact` - when `true`, use the most compact representation (ignored
|
||||
with the `scientific` option)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_string 7.1, [decimals: 2, compact: true]
|
||||
"7.1"
|
||||
|
||||
"""
|
||||
@spec to_string(float, list) :: String.t
|
||||
def to_string(float, options) do
|
||||
IO.warn "Float.to_string/2 is deprecated, use :erlang.float_to_binary/2 instead"
|
||||
:erlang.float_to_binary(float, expand_compact(options))
|
||||
end
|
||||
|
||||
defp expand_compact([{:compact, false} | t]), do: expand_compact(t)
|
||||
defp expand_compact([{:compact, true} | t]), do: [:compact | expand_compact(t)]
|
||||
defp expand_compact([h | t]), do: [h | expand_compact(t)]
|
||||
defp expand_compact([]), do: []
|
||||
defp expand_compact([{:compact, false}|t]), do: expand_compact(t)
|
||||
defp expand_compact([{:compact, true}|t]), do: [:compact|expand_compact(t)]
|
||||
defp expand_compact([h|t]), do: [h|expand_compact(t)]
|
||||
defp expand_compact([]), do: []
|
||||
end
|
||||
|
||||
+66
-93
@@ -1,6 +1,4 @@
|
||||
defmodule GenEvent do
|
||||
# TODO: Deprecate by 1.5
|
||||
|
||||
@moduledoc """
|
||||
A behaviour module for implementing event handling functionality.
|
||||
|
||||
@@ -16,7 +14,7 @@ defmodule GenEvent do
|
||||
|
||||
There are many use cases for event handlers. For example, a logging
|
||||
system can be built using event handlers where each log message is
|
||||
an event and different event handlers can be attached to handle the
|
||||
an event and different event handlers can be plugged to handle the
|
||||
log messages. One handler may print error messages on the terminal,
|
||||
another can write it to a file, while a third one can keep the
|
||||
messages in memory (like a buffer) until they are read.
|
||||
@@ -24,14 +22,14 @@ defmodule GenEvent do
|
||||
As an example, let's have a GenEvent that accumulates messages until
|
||||
they are collected by an explicit call.
|
||||
|
||||
# Define an Event Handler
|
||||
# Define a Event Handler
|
||||
defmodule LoggerHandler do
|
||||
use GenEvent
|
||||
|
||||
# Callbacks
|
||||
|
||||
def handle_event({:log, x}, messages) do
|
||||
{:ok, [x | messages]}
|
||||
{:ok, [x|messages]}
|
||||
end
|
||||
|
||||
def handle_call(:messages, messages) do
|
||||
@@ -62,7 +60,7 @@ defmodule GenEvent do
|
||||
|
||||
We start a new event manager by calling `GenEvent.start_link/1`.
|
||||
Notifications can be sent to the event manager which will then
|
||||
invoke `c:handle_event/2` for each registered handler.
|
||||
invoke `handle_event/2` for each registered handler.
|
||||
|
||||
We can add new handlers with `add_handler/3` and `add_mon_handler/3`.
|
||||
Calls can also be made to specific handlers by using `call/3`.
|
||||
@@ -81,7 +79,7 @@ defmodule GenEvent do
|
||||
|
||||
## Modes
|
||||
|
||||
GenEvent supports three different notifications.
|
||||
GenEvent stream supports three different notifications.
|
||||
|
||||
On `GenEvent.ack_notify/2`, the manager acknowledges each event,
|
||||
providing backpressure, but processing of the message happens
|
||||
@@ -102,7 +100,7 @@ defmodule GenEvent do
|
||||
stream = GenEvent.stream(pid)
|
||||
|
||||
# Discard the next 3 events
|
||||
_ = Enum.take(stream, 3)
|
||||
_ = Enum.drop(stream, 3)
|
||||
|
||||
# Print all remaining events
|
||||
for event <- stream do
|
||||
@@ -116,9 +114,11 @@ defmodule GenEvent do
|
||||
|
||||
## Learn more and compatibility
|
||||
|
||||
If you wish to find out more about GenEvent, the documentation and links
|
||||
in Erlang can provide extra insight.
|
||||
If you wish to find out more about gen events, Elixir getting started
|
||||
guides provide a tutorial-like introduction. The documentation and links
|
||||
in Erlang can also provide extra insight.
|
||||
|
||||
* [Introduction to Mix – Elixir's Getting Started Guide](http://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html)
|
||||
* [`:gen_event` module documentation](http://www.erlang.org/doc/man/gen_event.html)
|
||||
* [Event Handlers – Learn You Some Erlang for Great Good!](http://learnyousomeerlang.com/event-handlers)
|
||||
|
||||
@@ -136,7 +136,7 @@ defmodule GenEvent do
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Invoked when the handler is added to the `GenEvent` process. `add_handler/3`
|
||||
Invoked when the handler is added to the `GenEvent` process. `add_handler/3`,
|
||||
(and `add_mon_handler/3`) will block until it returns.
|
||||
|
||||
`args` is the argument term (third argument) passed to `add_handler/3`.
|
||||
@@ -146,7 +146,7 @@ defmodule GenEvent do
|
||||
|
||||
Returning `{:ok, state, :hibernate}` is similar to
|
||||
`{:ok, state}` except the `GenEvent` process is hibernated before continuing
|
||||
its loop. See `c:handle_event/2` for more information on hibernation.
|
||||
its loop. See `handle_event/2` for more information on hibernation.
|
||||
|
||||
Returning `{:error, reason}` will cause `add_handler/3` to return
|
||||
`{:error, reason}` and the handler is not added to `GenEvent` loop.
|
||||
@@ -177,7 +177,7 @@ defmodule GenEvent do
|
||||
beneficial.
|
||||
|
||||
Returning `:remove_handler` removes the handler from the `GenEvent` loop and
|
||||
calls `c:terminate/2` with reason `:remove_handler` and state `state`.
|
||||
calls `terminate/2` with reason `:remove_handler` and state `state`.
|
||||
"""
|
||||
@callback handle_event(event :: term, state :: term) ::
|
||||
{:ok, new_state} |
|
||||
@@ -195,10 +195,10 @@ defmodule GenEvent do
|
||||
|
||||
Returning `{:ok, reply, new_state, :hibernate}` is similar to
|
||||
`{:ok, reply, new_state}` except the process is hibernated. See
|
||||
`c:handle_event/2` for more information on hibernation.
|
||||
`handle_event/2` for more information on hibernation.
|
||||
|
||||
Returning `{:remove_handler, reply}` sends `reply` as a response to the call,
|
||||
removes the handler from the `GenEvent` loop and calls `c:terminate/2` with
|
||||
Returning `{:remove_handler, reply}` sends `reply` as a reponse to the call,
|
||||
removes the handler from the `GenEvent` loop and calls `terminate/2` with
|
||||
reason `:remove_handler` and state `state`.
|
||||
"""
|
||||
|
||||
@@ -214,7 +214,7 @@ defmodule GenEvent do
|
||||
|
||||
`msg` is the message and `state` is the current state of the handler.
|
||||
|
||||
Return values are the same as `c:handle_event/2`.
|
||||
Return values are the same as `handle_event/2`.
|
||||
"""
|
||||
@callback handle_info(msg :: term, state :: term) ::
|
||||
{:ok, new_state} |
|
||||
@@ -240,23 +240,23 @@ defmodule GenEvent do
|
||||
If part of a supervision tree, a `GenEvent`'s `Supervisor` will send an exit
|
||||
signal when shutting it down. The exit signal is based on the shutdown
|
||||
strategy in the child's specification. If it is `:brutal_kill` the `GenEvent`
|
||||
is killed and so `c:terminate/2` is not called for its handlers. However if it is
|
||||
is killed and so `terminate/2` is not called for its handlers. However if it is
|
||||
a timeout the `Supervisor` will send the exit signal `:shutdown` and the
|
||||
`GenEvent` will have the duration of the timeout to call `c:terminate/2` on all
|
||||
`GenEvent` will have the duration of the timeout to call `terminate/2` on all
|
||||
of its handlers - if the process is still alive after the timeout it is
|
||||
killed.
|
||||
|
||||
If the `GenEvent` receives an exit signal (that is not `:normal`) from any
|
||||
process when it is not trapping exits it will exit abruptly with the same
|
||||
reason and so not call the handlers' `c:terminate/2`. Note that a process does
|
||||
reason and so not call the handlers' `terminate/2`. Note that a process does
|
||||
*NOT* trap exits by default and an exit signal is sent when a linked process
|
||||
exits or its node is disconnected. Therefore it is not guaranteed that
|
||||
`c:terminate/2` is called when a `GenEvent` exits.
|
||||
`terminate/2` is called when a `GenEvent` exits.
|
||||
|
||||
Care should be taken to cleanup because the `GenEvent` can continue to loop
|
||||
Care should be taken to cleanup because the `GenEvent` can continue is loop
|
||||
after removing the handler. This is different to most other OTP behaviours.
|
||||
For example if the handler controls a `port` (e.g. `:gen_tcp.socket`) or
|
||||
`t:File.io_device/0`, it will be need to be closed in `c:terminate/2` as the
|
||||
`File.io_device`, it will be need to be closed in `terminate/2` as the
|
||||
process is not exiting so will not be automatically cleaned up.
|
||||
"""
|
||||
@callback terminate(reason, state :: term) ::
|
||||
@@ -264,18 +264,18 @@ defmodule GenEvent do
|
||||
|
||||
@doc """
|
||||
Invoked to change the state of the handler when a different version of the
|
||||
handler's module is loaded (hot code swapping) and the state's term
|
||||
handler's module module is loaded (hot code swapping) and the state's term
|
||||
structure should be changed.
|
||||
|
||||
`old_vsn` is the previous version of the module (defined by the `@vsn`
|
||||
attribute) when upgrading. When downgrading the previous version is wrapped in
|
||||
a 2-tuple with first element `:down`. `state` is the current state of the
|
||||
handler and `extra` is any extra data required to change the state.
|
||||
handker and `extra` is any extra data required to change the state.
|
||||
|
||||
Returning `{:ok, new_state}` changes the state to `new_state` and the code
|
||||
change is successful.
|
||||
|
||||
If `c:code_change/3` raises, the code change fails and the handler will continue
|
||||
If `code_change/3` raises, the code change fails and the handler will continue
|
||||
with its previous state. Therefore this callback does not usually contain side
|
||||
effects.
|
||||
"""
|
||||
@@ -314,16 +314,11 @@ defmodule GenEvent do
|
||||
|
||||
@doc false
|
||||
def handle_call(msg, state) do
|
||||
proc =
|
||||
case Process.info(self(), :registered_name) do
|
||||
{_, []} -> self()
|
||||
{_, name} -> name
|
||||
end
|
||||
|
||||
# We do this to trick Dialyzer to not complain about non-local returns.
|
||||
# We do this to trick dialyzer to not complain about non-local returns.
|
||||
reason = {:bad_call, msg}
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> raise "attempted to call GenEvent #{inspect proc} but no handle_call/2 clause was provided"
|
||||
1 -> {:remove_handler, {:bad_call, msg}}
|
||||
0 -> exit(reason)
|
||||
1 -> {:remove_handler, reason}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -356,9 +351,9 @@ defmodule GenEvent do
|
||||
section in the `GenServer` module docs.
|
||||
|
||||
If the event manager is successfully created and initialized, the function
|
||||
returns `{:ok, pid}`, where `pid` is the PID of the server. If a process with
|
||||
returns `{:ok, pid}`, where pid is the pid of the server. If a process with
|
||||
the specified server name already exists, the function returns
|
||||
`{:error, {:already_started, pid}}` with the PID of that process.
|
||||
`{:error, {:already_started, pid}}` with the pid of that process.
|
||||
|
||||
Note that a `GenEvent` started with `start_link/1` is linked to the
|
||||
parent process and will exit not only on crashes but also if the parent
|
||||
@@ -387,21 +382,8 @@ defmodule GenEvent do
|
||||
:gen.start(GenEvent, mode, @no_callback, [], [])
|
||||
atom when is_atom(atom) ->
|
||||
:gen.start(GenEvent, mode, {:local, atom}, @no_callback, [], [])
|
||||
{:global, _term} = tuple ->
|
||||
:gen.start(GenEvent, mode, tuple, @no_callback, [], [])
|
||||
{:via, via_module, _term} = tuple when is_atom(via_module) ->
|
||||
:gen.start(GenEvent, mode, tuple, @no_callback, [], [])
|
||||
other ->
|
||||
raise ArgumentError, """
|
||||
expected :name option to be one of:
|
||||
|
||||
* nil
|
||||
* atom
|
||||
* {:global, term}
|
||||
* {:via, module, term}
|
||||
|
||||
Got: #{inspect(other)}
|
||||
"""
|
||||
other when is_tuple(other) ->
|
||||
:gen.start(GenEvent, mode, other, @no_callback, [], [])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -430,10 +412,10 @@ defmodule GenEvent do
|
||||
@doc """
|
||||
Adds a new event handler to the event `manager`.
|
||||
|
||||
The event manager will call the `c:init/1` callback with `args` to
|
||||
The event manager will call the `init/1` callback with `args` to
|
||||
initiate the event handler and its internal state.
|
||||
|
||||
If `c:init/1` returns a correct value indicating successful completion,
|
||||
If `init/1` returns a correct value indicating successful completion,
|
||||
the event manager adds the event handler and this function returns
|
||||
`:ok`. If the callback fails with `reason` or returns `{:error, reason}`,
|
||||
the event handler is ignored and this function returns `{:error, reason}`.
|
||||
@@ -442,7 +424,7 @@ defmodule GenEvent do
|
||||
function returns `{:error, :already_present}`.
|
||||
|
||||
For installing multiple instances of the same handler, `{Module, id}` instead
|
||||
of `Module` must be used. The handler could be then referenced with
|
||||
of `Module` must be used. The handler could be then referenced with
|
||||
`{Module, id}` instead of just `Module`.
|
||||
"""
|
||||
@spec add_handler(manager, handler, term) :: :ok | {:error, term}
|
||||
@@ -461,7 +443,7 @@ defmodule GenEvent do
|
||||
by the GenEvent manager.
|
||||
|
||||
If the calling process later terminates with `reason`, the event manager
|
||||
will delete the event handler by calling the `c:terminate/2` callback with
|
||||
will delete the event handler by calling the `terminate/2` callback with
|
||||
`{:stop, reason}` as argument. If the event handler later is deleted,
|
||||
the event manager sends a message `{:gen_event_EXIT, handler, reason}`
|
||||
to the calling process. Reason is one of the following:
|
||||
@@ -473,10 +455,10 @@ defmodule GenEvent do
|
||||
* `:shutdown` - if the event handler has been removed because the event
|
||||
manager is terminating
|
||||
|
||||
* `{:swapped, new_handler, pid}` - if the process PID has replaced the
|
||||
* `{:swapped, new_handler, pid}` - if the process pid has replaced the
|
||||
event handler by another
|
||||
|
||||
* `term` - if the event handler is removed due to an error. Which term
|
||||
* a term - if the event handler is removed due to an error. Which term
|
||||
depends on the error
|
||||
|
||||
Keep in mind that the `{:gen_event_EXIT, handler, reason}` message is not
|
||||
@@ -498,7 +480,7 @@ defmodule GenEvent do
|
||||
@doc """
|
||||
Sends an event notification to the event `manager`.
|
||||
|
||||
The event manager will call `c:handle_event/2` for each
|
||||
The event manager will call `handle_event/2` for each
|
||||
installed event handler.
|
||||
|
||||
`notify` is asynchronous and will return immediately after the
|
||||
@@ -517,7 +499,7 @@ defmodule GenEvent do
|
||||
end
|
||||
end
|
||||
|
||||
def notify({:via, mod, name}, msg) when is_atom(mod) do
|
||||
def notify({:via, mod, name}, msg) do
|
||||
try do
|
||||
mod.send(name, {:notify, msg})
|
||||
:ok
|
||||
@@ -526,12 +508,8 @@ defmodule GenEvent do
|
||||
end
|
||||
end
|
||||
|
||||
def notify(manager, msg)
|
||||
when is_pid(manager)
|
||||
when is_atom(manager)
|
||||
when tuple_size(manager) == 2 and
|
||||
is_atom(elem(manager, 0)) and is_atom(elem(manager, 1)) do
|
||||
send(manager, {:notify, msg})
|
||||
def notify(other, msg) do
|
||||
send(other, {:notify, msg})
|
||||
:ok
|
||||
end
|
||||
|
||||
@@ -539,7 +517,7 @@ defmodule GenEvent do
|
||||
Sends a sync event notification to the event `manager`.
|
||||
|
||||
In other words, this function only returns `:ok` after the event manager
|
||||
invokes the `c:handle_event/2` callback on each installed event handler.
|
||||
invokes the `handle_event/2` callback on each installed event handler.
|
||||
|
||||
See `notify/2` for more info.
|
||||
"""
|
||||
@@ -549,7 +527,7 @@ defmodule GenEvent do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends an ack event notification to the event `manager`.
|
||||
Sends a ack event notification to the event `manager`.
|
||||
|
||||
In other words, this function only returns `:ok` as soon as the
|
||||
event manager starts processing this event, but it does not wait
|
||||
@@ -567,10 +545,10 @@ defmodule GenEvent do
|
||||
Makes a synchronous call to the event `handler` installed in `manager`.
|
||||
|
||||
The given `request` is sent and the caller waits until a reply arrives or
|
||||
a timeout occurs. The event manager will call `c:handle_call/2` to handle
|
||||
a timeout occurs. The event manager will call `handle_call/2` to handle
|
||||
the request.
|
||||
|
||||
The return value `reply` is defined in the return value of `c:handle_call/2`.
|
||||
The return value `reply` is defined in the return value of `handle_call/2`.
|
||||
If the specified event handler is not installed, the function returns
|
||||
`{:error, :not_found}`.
|
||||
"""
|
||||
@@ -589,7 +567,7 @@ defmodule GenEvent do
|
||||
@doc """
|
||||
Removes an event handler from the event `manager`.
|
||||
|
||||
The event manager will call `c:terminate/2` to terminate the event handler
|
||||
The event manager will call `terminate/2` to terminate the event handler
|
||||
and return the callback value. If the specified event handler is not
|
||||
installed, the function returns `{:error, :not_found}`.
|
||||
"""
|
||||
@@ -601,17 +579,17 @@ defmodule GenEvent do
|
||||
@doc """
|
||||
Replaces an old event handler with a new one in the event `manager`.
|
||||
|
||||
First, the old event handler is deleted by calling `c:terminate/2` with
|
||||
First, the old event handler is deleted by calling `terminate/2` with
|
||||
the given `args1` and collects the return value. Then the new event handler
|
||||
is added and initiated by calling `init({args2, term})`, where `term` is the
|
||||
return value of calling `c:terminate/2` in the old handler. This makes it
|
||||
return value of calling `terminate/2` in the old handler. This makes it
|
||||
possible to transfer information from one handler to another.
|
||||
|
||||
The new handler will be added even if the specified old event handler
|
||||
is not installed or if the handler fails to terminate with a given reason
|
||||
in which case `state = {:error, term}`.
|
||||
|
||||
If `c:init/1` in the second handler returns a correct value, this
|
||||
If `init/1` in the second handler returns a correct value, this
|
||||
function returns `:ok`.
|
||||
"""
|
||||
@spec swap_handler(manager, handler, term, handler, term) :: :ok | {:error, term}
|
||||
@@ -670,14 +648,9 @@ defmodule GenEvent do
|
||||
init_it(starter, self(), name, mod, args, options)
|
||||
end
|
||||
|
||||
def init_it(starter, parent, name, _mod, _args, options) do
|
||||
def init_it(starter, parent, name, _, _, options) do
|
||||
Process.put(:"$initial_call", {__MODULE__, :init_it, 6})
|
||||
debug =
|
||||
if function_exported?(:gen, :debug_options, 2) do
|
||||
:gen.debug_options(name, options)
|
||||
else
|
||||
:gen.debug_options(options)
|
||||
end
|
||||
debug = :gen.debug_options(options)
|
||||
:proc_lib.init_ack(starter, {:ok, self()})
|
||||
loop(parent, name(name), [], debug, false)
|
||||
end
|
||||
@@ -928,13 +901,13 @@ defmodule GenEvent do
|
||||
{hib, server_collect_process_handlers(mode, event, streams, handlers, name)}
|
||||
end
|
||||
|
||||
defp server_split_process_handlers(mode, event, [handler | t], handlers, streams) do
|
||||
defp server_split_process_handlers(mode, event, [handler|t], handlers, streams) do
|
||||
case handler(handler, :id) do
|
||||
{pid, _ref} when is_pid(pid) ->
|
||||
server_process_notify(mode, event, handler)
|
||||
server_split_process_handlers(mode, event, t, handlers, [handler | streams])
|
||||
server_split_process_handlers(mode, event, t, handlers, [handler|streams])
|
||||
_ ->
|
||||
server_split_process_handlers(mode, event, t, [handler | handlers], streams)
|
||||
server_split_process_handlers(mode, event, t, [handler|handlers], streams)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -950,10 +923,10 @@ defmodule GenEvent do
|
||||
defp mode_to_tag(:sync), do: :sync_notify
|
||||
defp mode_to_tag(:async), do: :notify
|
||||
|
||||
defp server_notify(event, fun, [handler | t], name, handlers, acc, hib) do
|
||||
defp server_notify(event, fun, [handler|t], name, handlers, acc, hib) do
|
||||
case server_update(handler, fun, event, name, handlers) do
|
||||
{new_hib, handler} ->
|
||||
server_notify(event, fun, t, name, handlers, [handler | acc], hib or new_hib)
|
||||
server_notify(event, fun, t, name, handlers, [handler|acc], hib or new_hib)
|
||||
:error ->
|
||||
server_notify(event, fun, t, name, handlers, acc, hib)
|
||||
end
|
||||
@@ -987,16 +960,16 @@ defmodule GenEvent do
|
||||
end
|
||||
end
|
||||
|
||||
defp server_collect_process_handlers(:async, event, [handler | t], handlers, name) do
|
||||
server_collect_process_handlers(:async, event, t, [handler | handlers], name)
|
||||
defp server_collect_process_handlers(:async, event, [handler|t], handlers, name) do
|
||||
server_collect_process_handlers(:async, event, t, [handler|handlers], name)
|
||||
end
|
||||
|
||||
defp server_collect_process_handlers(mode, event, [handler | t], handlers, name) when mode in [:sync, :ack] do
|
||||
defp server_collect_process_handlers(mode, event, [handler|t], handlers, name) when mode in [:sync, :ack] do
|
||||
handler(ref: ref, id: id) = handler
|
||||
|
||||
receive do
|
||||
{^ref, :ok} ->
|
||||
server_collect_process_handlers(mode, event, t, [handler | handlers], name)
|
||||
server_collect_process_handlers(mode, event, t, [handler|handlers], name)
|
||||
{_from, tag, {:delete_handler, ^id, args}} ->
|
||||
do_terminate(handler, args, :remove, name, :normal)
|
||||
reply(tag, :ok)
|
||||
@@ -1086,9 +1059,9 @@ defmodule GenEvent do
|
||||
{:ok, res} ->
|
||||
case res do
|
||||
{:ok, state} ->
|
||||
{false, succ, [handler(handler, state: state) | handlers]}
|
||||
{false, succ, [handler(handler, state: state)|handlers]}
|
||||
{:ok, state, :hibernate} ->
|
||||
{true, succ, [handler(handler, state: state) | handlers]}
|
||||
{true, succ, [handler(handler, state: state)|handlers]}
|
||||
{:error, _} = error ->
|
||||
{false, error, handlers}
|
||||
other ->
|
||||
@@ -1151,7 +1124,7 @@ defmodule GenEvent do
|
||||
defp report_error(handler, reason, state, last_in, name) do
|
||||
reason =
|
||||
case reason do
|
||||
{:undef, [{m, f, a, _} | _]=mfas} ->
|
||||
{:undef, [{m, f, a, _}|_]=mfas} ->
|
||||
cond do
|
||||
:code.is_loaded(m) === false ->
|
||||
{:"module could not be loaded", mfas}
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
defmodule GenEvent.Stream do
|
||||
@moduledoc false
|
||||
@moduledoc """
|
||||
Defines a `GenEvent` stream.
|
||||
|
||||
This is a struct returned by `GenEvent.stream/2`. The struct is public and
|
||||
contains the following fields:
|
||||
|
||||
* `:manager` - the manager reference given to `GenEvent.stream/2`
|
||||
* `:timeout` - the timeout between events, defaults to `:infinity`
|
||||
|
||||
"""
|
||||
defstruct manager: nil, timeout: :infinity
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
@@ -13,7 +22,7 @@ defmodule GenEvent.Stream do
|
||||
|
||||
@doc false
|
||||
def handle_event(event, _state) do
|
||||
# We do this to trick Dialyzer to not complain about non-local returns.
|
||||
# We do this to trick dialyzer to not complain about non-local returns.
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> exit({:bad_event, event})
|
||||
1 -> :remove_handler
|
||||
@@ -22,7 +31,7 @@ defmodule GenEvent.Stream do
|
||||
|
||||
@doc false
|
||||
def handle_call(msg, _state) do
|
||||
# We do this to trick Dialyzer to not complain about non-local returns.
|
||||
# We do this to trick dialyzer to not complain about non-local returns.
|
||||
reason = {:bad_call, msg}
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> exit(reason)
|
||||
|
||||
+134
-332
@@ -23,12 +23,12 @@ defmodule GenServer do
|
||||
|
||||
# Callbacks
|
||||
|
||||
def handle_call(:pop, _from, [h | t]) do
|
||||
def handle_call(:pop, _from, [h|t]) do
|
||||
{:reply, h, t}
|
||||
end
|
||||
|
||||
def handle_cast({:push, item}, state) do
|
||||
{:noreply, [item | state]}
|
||||
{:noreply, [item|state]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -53,8 +53,8 @@ defmodule GenServer do
|
||||
while **cast** messages do not.
|
||||
|
||||
Every time you do a `GenServer.call/3`, the client will send a message
|
||||
that must be handled by the `c:handle_call/3` callback in the GenServer.
|
||||
A `cast/2` message must be handled by `c:handle_cast/2`.
|
||||
that must be handled by the `handle_call/3` callback in the GenServer.
|
||||
A `cast/2` message must be handled by `handle_cast/2`.
|
||||
|
||||
## Callbacks
|
||||
|
||||
@@ -79,12 +79,10 @@ defmodule GenServer do
|
||||
mechanism and name. The `:via` option expects a module that exports
|
||||
`register_name/2`, `unregister_name/1`, `whereis_name/1` and `send/2`.
|
||||
One such example is the `:global` module which uses these functions
|
||||
for keeping the list of names of processes and their associated PIDs
|
||||
that are available globally for a network of Elixir nodes. Elixir also
|
||||
ships with a local, decentralized and scalable registry called `Registry`
|
||||
for locally storing names that are generated dynamically.
|
||||
for keeping the list of names of processes and their associated pid's
|
||||
that are available globally for a network of Erlang nodes.
|
||||
|
||||
For example, we could start and register our `Stack` server locally as follows:
|
||||
For example, we could start and register our Stack server locally as follows:
|
||||
|
||||
# Start the server and register it locally with name MyStack
|
||||
{:ok, _} = GenServer.start_link(Stack, [:hello], name: MyStack)
|
||||
@@ -103,11 +101,6 @@ defmodule GenServer do
|
||||
* `{:via, module, name}` if the server is registered through an alternative
|
||||
registry
|
||||
|
||||
If there is an interest to register dynamic names locally, do not use
|
||||
atoms, as atoms are never garbage collected and therefore dynamically
|
||||
generated atoms won't be garbage collected. For such cases, you can
|
||||
set up your own local registry by using the `Registry` module.
|
||||
|
||||
## Client / Server APIs
|
||||
|
||||
Although in the example above we have used `GenServer.start_link/3` and
|
||||
@@ -136,7 +129,7 @@ defmodule GenServer do
|
||||
|
||||
# Server (callbacks)
|
||||
|
||||
def handle_call(:pop, _from, [h | t]) do
|
||||
def handle_call(:pop, _from, [h|t]) do
|
||||
{:reply, h, t}
|
||||
end
|
||||
|
||||
@@ -146,7 +139,7 @@ defmodule GenServer do
|
||||
end
|
||||
|
||||
def handle_cast({:push, item}, state) do
|
||||
{:noreply, [item | state]}
|
||||
{:noreply, [item|state]}
|
||||
end
|
||||
|
||||
def handle_cast(request, state) do
|
||||
@@ -158,109 +151,13 @@ defmodule GenServer do
|
||||
the same module. If the server and/or client implementations are growing
|
||||
complex, you may want to have them in different modules.
|
||||
|
||||
## Receiving "regular" messages
|
||||
## Receiving custom messages
|
||||
|
||||
The goal of a `GenServer` is to abstract the "receive" loop for developers,
|
||||
automatically handling system messages, support code change, synchronous
|
||||
calls and more. Therefore, you should never call your own "receive" inside
|
||||
the GenServer callbacks as doing so will cause the GenServer to misbehave.
|
||||
|
||||
Besides the synchronous and asynchronous communication provided by `call/3`
|
||||
and `cast/2`, "regular" messages sent by functions such `Kernel.send/2`,
|
||||
`Process.send_after/4` and similar, can be handled inside the `c:handle_info/2`
|
||||
callback.
|
||||
|
||||
`c:handle_info/2` can be used in many situations, such as handling monitor
|
||||
DOWN messages sent by `Process.monitor/1`. Another use case for `c:handle_info/2`
|
||||
is to perform periodic work, with the help of `Process.send_after/4`:
|
||||
|
||||
defmodule MyApp.Periodically do
|
||||
use GenServer
|
||||
|
||||
def start_link do
|
||||
GenServer.start_link(__MODULE__, %{})
|
||||
end
|
||||
|
||||
def init(state) do
|
||||
schedule_work() # Schedule work to be performed on start
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
def handle_info(:work, state) do
|
||||
# Do the desired work here
|
||||
schedule_work() # Reschedule once more
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
defp schedule_work() do
|
||||
Process.send_after(self(), :work, 2 * 60 * 60 * 1000) # In 2 hours
|
||||
end
|
||||
end
|
||||
|
||||
## Debugging with the :sys module
|
||||
|
||||
GenServers, as [special processes](http://erlang.org/doc/design_principles/spec_proc.html),
|
||||
can be debugged using the `:sys` module. Through various hooks, this module
|
||||
allows developers to introspect the state of the process and trace
|
||||
system events that happen during its execution, such as received messages,
|
||||
sent replies and state changes.
|
||||
|
||||
Let's explore the basic functions from the `:sys` module used for debugging:
|
||||
|
||||
* [`:sys.get_state/2`](http://erlang.org/doc/man/sys.html#get_state-2) -
|
||||
allows retrieval of the state of the process. In the case of
|
||||
a GenServer process, it will be the callback module state, as
|
||||
passed into the callback functions as last argument.
|
||||
* [`:sys.get_status/2`](http://erlang.org/doc/man/sys.html#get_status-2) -
|
||||
allows retrieval of the status of the process. This status includes
|
||||
the process dictionary, if the process is running or is suspended,
|
||||
the parent PID, the debugger state, and the state of the behaviour module,
|
||||
which includes the callback module state (as returned by `:sys.get_state/2`).
|
||||
It's possible to change how this status is represented by defining
|
||||
the optional `c:GenServer.format_status/2` callback.
|
||||
* [`:sys.trace/3`](http://erlang.org/doc/man/sys.html#trace-3) -
|
||||
prints all the system events to `:stdio`.
|
||||
* [`:sys.statistics/3`](http://erlang.org/doc/man/sys.html#statistics-3) -
|
||||
manages collection of process statistics.
|
||||
* [`:sys.no_debug/2`](http://erlang.org/doc/man/sys.html#no_debug-2) -
|
||||
turns off all debug handlers for the given process. It is very important
|
||||
to switch off debugging once we're done. Excessive debug handlers or
|
||||
those that should be turned off, but weren't, can seriously damage
|
||||
the performance of the system.
|
||||
|
||||
Let's see how we could use those functions for debugging the stack server
|
||||
we defined earlier.
|
||||
|
||||
iex> {:ok, pid} = Stack.start_link([])
|
||||
iex> :sys.statistics(pid, true) # turn on collecting process statistics
|
||||
iex> :sys.trace(pid, true) # turn on event printing
|
||||
iex> Stack.push(pid, 1)
|
||||
*DBG* <0.122.0> got cast {push,1}
|
||||
*DBG* <0.122.0> new state [1]
|
||||
:ok
|
||||
iex> :sys.get_state(pid)
|
||||
[1]
|
||||
iex> Stack.pop(pid)
|
||||
*DBG* <0.122.0> got call pop from <0.80.0>
|
||||
*DBG* <0.122.0> sent 1 to <0.80.0>, new state []
|
||||
1
|
||||
iex> :sys.statistics(pid, :get)
|
||||
{:ok,
|
||||
[start_time: {{2016, 7, 16}, {12, 29, 41}},
|
||||
current_time: {{2016, 7, 16}, {12, 29, 50}},
|
||||
reductions: 117, messages_in: 2, messages_out: 0]}
|
||||
iex> :sys.no_debug(pid) # turn off all debug handlers
|
||||
:ok
|
||||
iex> :sys.get_status(pid)
|
||||
{:status, #PID<0.122.0>, {:module, :gen_server},
|
||||
[["$initial_call": {Stack, :init, 1}, # pdict
|
||||
"$ancestors": [#PID<0.80.0>, #PID<0.51.0>]],
|
||||
:running, # :running | :suspended
|
||||
#PID<0.80.0>, # parent
|
||||
[], # debugger state
|
||||
[header: 'Status for generic server <0.122.0>', # module status
|
||||
data: [{'Status', :running}, {'Parent', #PID<0.80.0>},
|
||||
{'Logged events', []}], data: [{'State', [1]}]]]}
|
||||
If you want to receive custom messages, always receive them in `handle_info/2`.
|
||||
|
||||
## Learn more
|
||||
|
||||
@@ -275,7 +172,7 @@ defmodule GenServer do
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Invoked when the server is started. `start_link/3` or `start/3` will
|
||||
Invoked when the server is started. `start_link/3` (or `start/3`) will
|
||||
block until it returns.
|
||||
|
||||
`args` is the argument term (second argument) passed to `start_link/3`.
|
||||
@@ -289,10 +186,10 @@ defmodule GenServer do
|
||||
|
||||
Returning `{:ok, state, :hibernate}` is similar to
|
||||
`{:ok, state}` except the process is hibernated before entering the loop. See
|
||||
`c:handle_call/3` for more information on hibernation.
|
||||
`handle_call/3` for more information on hibernation.
|
||||
|
||||
Returning `:ignore` will cause `start_link/3` to return `:ignore` and the
|
||||
process will exit normally without entering the loop or calling `c:terminate/2`.
|
||||
process will exit normally without entering the loop or calling `terminate/2`.
|
||||
If used when part of a supervision tree the parent supervisor will not fail
|
||||
to start nor immediately try to restart the `GenServer`. The remainder of the
|
||||
supervision tree will be (re)started and so the `GenServer` should not be
|
||||
@@ -300,14 +197,14 @@ defmodule GenServer do
|
||||
`Supervisor.restart_child/2` as the child specification is saved in the parent
|
||||
supervisor. The main use cases for this are:
|
||||
|
||||
* The `GenServer` is disabled by configuration but might be enabled later.
|
||||
* An error occurred and it will be handled by a different mechanism than the
|
||||
`Supervisor`. Likely this approach involves calling `Supervisor.restart_child/2`
|
||||
after a delay to attempt a restart.
|
||||
- The `GenServer` is disabled by configuration but might be enabled later.
|
||||
- An error occured and it will be handled by a different mechanism than the
|
||||
`Supervisor`. Likely this approach involves calling `Supervisor.restart_child/2`
|
||||
after a delay to attempt a restart.
|
||||
|
||||
Returning `{:stop, reason}` will cause `start_link/3` to return
|
||||
`{:error, reason}` and the process to exit with reason `reason` without
|
||||
entering the loop or calling `c:terminate/2`.
|
||||
entering the loop or calling `terminate/2`.
|
||||
"""
|
||||
@callback init(args :: term) ::
|
||||
{:ok, state} |
|
||||
@@ -320,7 +217,7 @@ defmodule GenServer do
|
||||
reply is received (unless the call times out or nodes are disconnected).
|
||||
|
||||
`request` is the request message sent by a `call/3`, `from` is a 2-tuple
|
||||
containing the caller's PID and a term that uniquely identifies the call, and
|
||||
containing the caller's pid and a term that uniquely identifies the call, and
|
||||
`state` is the current state of the `GenServer`.
|
||||
|
||||
Returning `{:reply, reply, new_state}` sends the response `reply` to the
|
||||
@@ -332,10 +229,10 @@ defmodule GenServer do
|
||||
|
||||
Returning `{:reply, reply, new_state, :hibernate}` is similar to
|
||||
`{:reply, reply, new_state}` except the process is hibernated and will
|
||||
continue the loop once a message is in its message queue. If a message is
|
||||
already in the message queue this will be immediately. Hibernating a
|
||||
`GenServer` causes garbage collection and leaves a continuous heap that
|
||||
minimises the memory used by the process.
|
||||
continue the loop once a message is its message queue. If a message is already
|
||||
in the message queue this will be immediately. Hibernating a `GenServer`
|
||||
causes garbage collection and leaves a continuous heap that minimises the
|
||||
memory used by the process.
|
||||
|
||||
Hibernating should not be used aggressively as too much time could be spent
|
||||
garbage collecting. Normally it should only be used when a message is not
|
||||
@@ -348,11 +245,11 @@ defmodule GenServer do
|
||||
|
||||
There are three main use cases for not replying using the return value:
|
||||
|
||||
* To reply before returning from the callback because the response is known
|
||||
before calling a slow function.
|
||||
* To reply after returning from the callback because the response is not yet
|
||||
available.
|
||||
* To reply from another process, such as a task.
|
||||
- To reply before returning from the callback because the response is known
|
||||
before calling a slow function.
|
||||
- To reply after returning from the callback because the response is not yet
|
||||
available.
|
||||
- To reply from another process, such as a task.
|
||||
|
||||
When replying from another process the `GenServer` should exit if the other
|
||||
process exits without replying as the caller will be blocking awaiting a
|
||||
@@ -362,15 +259,12 @@ defmodule GenServer do
|
||||
`{:noreply, new_state}` except a timeout or hibernation occurs as with a
|
||||
`:reply` tuple.
|
||||
|
||||
Returning `{:stop, reason, reply, new_state}` stops the loop and `c:terminate/2`
|
||||
Returning `{:stop, reason, reply, new_state}` stops the loop and `terminate/2`
|
||||
is called with reason `reason` and state `new_state`. Then the `reply` is sent
|
||||
as the response to call and the process exits with reason `reason`.
|
||||
|
||||
Returning `{:stop, reason, new_state}` is similar to
|
||||
`{:stop, reason, reply, new_state}` except a reply is not sent.
|
||||
|
||||
If this callback is not implemented, the default implementation by
|
||||
`use GenServer` will return `{:stop, {:bad_call, request}, state}`.
|
||||
"""
|
||||
@callback handle_call(request :: term, from, state :: term) ::
|
||||
{:reply, reply, new_state} |
|
||||
@@ -389,19 +283,16 @@ defmodule GenServer do
|
||||
Returning `{:noreply, new_state}` continues the loop with new state `new_state`.
|
||||
|
||||
Returning `{:noreply, new_state, timeout}` is similar to
|
||||
`{:noreply, new_state}` except `handle_info(:timeout, new_state)` will be
|
||||
called after `timeout` milliseconds if no messages are received.
|
||||
`{:noreply, reply, new_state}` except `handle_info(:timeout, new_state)` will
|
||||
be called after `timeout` milliseconds if no messages are received.
|
||||
|
||||
Returning `{:noreply, new_state, :hibernate}` is similar to
|
||||
`{:noreply, new_state}` except the process is hibernated before continuing the
|
||||
loop. See `c:handle_call/3` for more information.
|
||||
loop. See `handle_call/3` for more information.
|
||||
|
||||
Returning `{:stop, reason, new_state}` stops the loop and `c:terminate/2` is
|
||||
Returning `{:stop, reason, new_state}` stops the loop and `terminate/2` is
|
||||
called with the reason `reason` and state `new_state`. The process exits with
|
||||
reason `reason`.
|
||||
|
||||
If this callback is not implemented, the default implementation by
|
||||
`use GenServer` will return `{:stop, {:bad_cast, request}, state}`.
|
||||
"""
|
||||
@callback handle_cast(request :: term, state :: term) ::
|
||||
{:noreply, new_state} |
|
||||
@@ -414,10 +305,7 @@ defmodule GenServer do
|
||||
`msg` is the message and `state` is the current state of the `GenServer`. When
|
||||
a timeout occurs the message is `:timeout`.
|
||||
|
||||
Return values are the same as `c:handle_cast/2`.
|
||||
|
||||
If this callback is not implemented, the default implementation by
|
||||
`use GenServer` will return `{:noreply, state}`.
|
||||
Return values are the same as `handle_cast/2`.
|
||||
"""
|
||||
@callback handle_info(msg :: :timeout | term, state :: term) ::
|
||||
{:noreply, new_state} |
|
||||
@@ -430,7 +318,7 @@ defmodule GenServer do
|
||||
`reason` is exit reason and `state` is the current state of the `GenServer`.
|
||||
The return value is ignored.
|
||||
|
||||
`c:terminate/2` is called if a callback (except `c:init/1`) returns a `:stop`
|
||||
`terminate/2` is called if a callback (except `init/1`) returns a `:stop`
|
||||
tuple, raises, calls `Kernel.exit/1` or returns an invalid value. It may also
|
||||
be called if the `GenServer` traps exits using `Process.flag/2` *and* the
|
||||
parent process sends an exit signal.
|
||||
@@ -438,23 +326,23 @@ defmodule GenServer do
|
||||
If part of a supervision tree a `GenServer`'s `Supervisor` will send an exit
|
||||
signal when shutting it down. The exit signal is based on the shutdown
|
||||
strategy in the child's specification. If it is `:brutal_kill` the `GenServer`
|
||||
is killed and so `c:terminate/2` is not called. However if it is a timeout the
|
||||
is killed and so `terminate/2` is not called. However if it is a timeout the
|
||||
`Supervisor` will send the exit signal `:shutdown` and the `GenServer` will
|
||||
have the duration of the timeout to call `c:terminate/2` - if the process is
|
||||
have the duration of the timeout to call `terminate/2` - if the process is
|
||||
still alive after the timeout it is killed.
|
||||
|
||||
If the `GenServer` receives an exit signal (that is not `:normal`) from any
|
||||
process when it is not trapping exits it will exit abruptly with the same
|
||||
reason and so not call `c:terminate/2`. Note that a process does *NOT* trap
|
||||
reason and so not call `terminate/2`. Note that a process does *NOT* trap
|
||||
exits by default and an exit signal is sent when a linked process exits or its
|
||||
node is disconnected.
|
||||
|
||||
Therefore it is not guaranteed that `c:terminate/2` is called when a `GenServer`
|
||||
Therefore it is not guaranteed that `terminate/2` is called when a `GenServer`
|
||||
exits. For such reasons, we usually recommend important clean-up rules to
|
||||
happen in separated processes either by use of monitoring or by links
|
||||
themselves. For example if the `GenServer` controls a `port` (e.g.
|
||||
`:gen_tcp.socket`) or `t:File.io_device/0`, they will be closed on receiving a
|
||||
`GenServer`'s exit signal and do not need to be closed in `c:terminate/2`.
|
||||
`:gen_tcp.socket`) or `File.io_device`, they will be closed on receiving a
|
||||
`GenServer`'s exit signal and do not need to be closed in `terminate/2`.
|
||||
|
||||
If `reason` is not `:normal`, `:shutdown` nor `{:shutdown, term}` an error is
|
||||
logged.
|
||||
@@ -478,35 +366,13 @@ defmodule GenServer do
|
||||
Returning `{:error, reason}` fails the code change with reason `reason` and
|
||||
the state remains as the previous state.
|
||||
|
||||
If `c:code_change/3` raises the code change fails and the loop will continue
|
||||
If `code_change/3` raises the code change fails and the loop will continue
|
||||
with its previous state. Therefore this callback does not usually contain side effects.
|
||||
"""
|
||||
@callback code_change(old_vsn, state :: term, extra :: term) ::
|
||||
{:ok, new_state :: term} |
|
||||
{:error, reason :: term} when old_vsn: term | {:down, term}
|
||||
|
||||
@doc """
|
||||
Invoked in some cases to retrieve a formatted version of the `GenServer` status.
|
||||
|
||||
This callback can be useful to control the *appearance* of the status of the
|
||||
`GenServer`. For example, it can be used to return a compact representation of
|
||||
the `GenServer`'s state to avoid having large state terms printed.
|
||||
|
||||
* one of `:sys.get_status/1` or `:sys.get_status/2` is invoked to get the
|
||||
status of the `GenServer`; in such cases, `reason` is `:normal`
|
||||
|
||||
* the `GenServer` terminates abnormally and logs an error; in such cases,
|
||||
`reason` is `:terminate`
|
||||
|
||||
`pdict_and_state` is a two-elements list `[pdict, state]` where `pdict` is a
|
||||
list of `{key, value}` tuples representing the current process dictionary of
|
||||
the `GenServer` and `state` is the current state of the `GenServer`.
|
||||
"""
|
||||
@callback format_status(reason, pdict_and_state :: list) ::
|
||||
term when reason: :normal | :terminate
|
||||
|
||||
@optional_callbacks format_status: 2
|
||||
|
||||
@typedoc "Return values of `start*` functions"
|
||||
@type on_start :: {:ok, pid} | :ignore | {:error, {:already_started, pid} | term}
|
||||
|
||||
@@ -531,7 +397,7 @@ defmodule GenServer do
|
||||
@typedoc """
|
||||
Tuple describing the client of a call request.
|
||||
|
||||
`pid` is the PID of the caller and `tag` is a unique term used to identify the
|
||||
`pid` is the pid of the caller and `tag` is a unique term used to identify the
|
||||
call.
|
||||
"""
|
||||
@type from :: {pid, tag :: term}
|
||||
@@ -539,7 +405,7 @@ defmodule GenServer do
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote location: :keep do
|
||||
@behaviour GenServer
|
||||
@behaviour :gen_server
|
||||
|
||||
@doc false
|
||||
def init(args) do
|
||||
@@ -548,43 +414,26 @@ defmodule GenServer do
|
||||
|
||||
@doc false
|
||||
def handle_call(msg, _from, state) do
|
||||
proc =
|
||||
case Process.info(self(), :registered_name) do
|
||||
{_, []} -> self()
|
||||
{_, name} -> name
|
||||
end
|
||||
|
||||
# We do this to trick Dialyzer to not complain about non-local returns.
|
||||
reason = {:bad_call, msg}
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> raise "attempted to call GenServer #{inspect proc} but no handle_call/3 clause was provided"
|
||||
1 -> {:stop, {:bad_call, msg}, state}
|
||||
0 -> exit(reason)
|
||||
1 -> {:stop, reason, state}
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_info(msg, state) do
|
||||
proc =
|
||||
case Process.info(self(), :registered_name) do
|
||||
{_, []} -> self()
|
||||
{_, name} -> name
|
||||
end
|
||||
:error_logger.warning_msg('~p ~p received unexpected message in handle_info/2: ~p~n',
|
||||
[__MODULE__, proc, msg])
|
||||
def handle_info(_msg, state) do
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_cast(msg, state) do
|
||||
proc =
|
||||
case Process.info(self(), :registered_name) do
|
||||
{_, []} -> self()
|
||||
{_, name} -> name
|
||||
end
|
||||
|
||||
# We do this to trick Dialyzer to not complain about non-local returns.
|
||||
reason = {:bad_cast, msg}
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> raise "attempted to cast GenServer #{inspect proc} but no handle_cast/2 clause was provided"
|
||||
1 -> {:stop, {:bad_cast, msg}, state}
|
||||
0 -> exit(reason)
|
||||
1 -> {:stop, reason, state}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -608,41 +457,38 @@ defmodule GenServer do
|
||||
|
||||
This is often used to start the `GenServer` as part of a supervision tree.
|
||||
|
||||
Once the server is started, the `c:init/1` function of the given `module` is
|
||||
called with `args` as its arguments to initialize the server. To ensure a
|
||||
synchronized start-up procedure, this function does not return until `c:init/1`
|
||||
has returned.
|
||||
Once the server is started, it calls the `init/1` function in the given `module`
|
||||
passing the given `args` to initialize it. To ensure a synchronized start-up
|
||||
procedure, this function does not return until `init/1` has returned.
|
||||
|
||||
Note that a `GenServer` started with `start_link/3` is linked to the
|
||||
parent process and will exit in case of crashes from the parent. The GenServer
|
||||
will also exit due to the `:normal` reasons in case it is configured to trap
|
||||
exits in the `c:init/1` callback.
|
||||
parent process and will exit in case of crashes. The GenServer will also
|
||||
exit due to the `:normal` reasons in case it is configured to trap exits
|
||||
in the `init/1` callback.
|
||||
|
||||
## Options
|
||||
|
||||
* `:name` - used for name registration as described in the "Name
|
||||
registration" section of the module documentation
|
||||
The `:name` option is used for name registration as described in the module
|
||||
documentation. If the option `:timeout` option is present, the server is
|
||||
allowed to spend the given milliseconds initializing or it will be
|
||||
terminated and the start function will return `{:error, :timeout}`.
|
||||
|
||||
* `:timeout` - if present, the server is allowed to spend the given amount of
|
||||
milliseconds initializing or it will be terminated and the start function
|
||||
will return `{:error, :timeout}`
|
||||
If the `:debug` option is present, the corresponding function in the
|
||||
[`:sys` module](http://www.erlang.org/doc/man/sys.html) will be invoked.
|
||||
|
||||
* `:debug` - if present, the corresponding function in the [`:sys`
|
||||
module](http://www.erlang.org/doc/man/sys.html) is invoked
|
||||
|
||||
* `:spawn_opt` - if present, its value is passed as options to the
|
||||
underlying process as in `Process.spawn/4`
|
||||
If the `:spawn_opt` option is present, its value will be passed as options
|
||||
to the underlying process as in `Process.spawn/4`.
|
||||
|
||||
## Return values
|
||||
|
||||
If the server is successfully created and initialized, this function returns
|
||||
`{:ok, pid}`, where `pid` is the PID of the server. If a process with the
|
||||
specified server name already exists, this function returns
|
||||
`{:error, {:already_started, pid}}` with the PID of that process.
|
||||
If the server is successfully created and initialized, the function returns
|
||||
`{:ok, pid}`, where pid is the pid of the server. If a process with the
|
||||
specified server name already exists, the function returns
|
||||
`{:error, {:already_started, pid}}` with the pid of that process.
|
||||
|
||||
If the `c:init/1` callback fails with `reason`, this function returns
|
||||
If the `init/1` callback fails with `reason`, the function returns
|
||||
`{:error, reason}`. Otherwise, if it returns `{:stop, reason}`
|
||||
or `:ignore`, the process is terminated and this function returns
|
||||
or `:ignore`, the process is terminated and the function returns
|
||||
`{:error, reason}` or `:ignore`, respectively.
|
||||
"""
|
||||
@spec start_link(module, any, options) :: on_start
|
||||
@@ -666,34 +512,22 @@ defmodule GenServer do
|
||||
:gen.start(:gen_server, link, module, args, opts)
|
||||
{atom, opts} when is_atom(atom) ->
|
||||
:gen.start(:gen_server, link, {:local, atom}, module, args, opts)
|
||||
{{:global, _term} = tuple, opts} ->
|
||||
:gen.start(:gen_server, link, tuple, module, args, opts)
|
||||
{{:via, via_module, _term} = tuple, opts} when is_atom(via_module) ->
|
||||
:gen.start(:gen_server, link, tuple, module, args, opts)
|
||||
other ->
|
||||
raise ArgumentError, """
|
||||
expected :name option to be one of:
|
||||
|
||||
* nil
|
||||
* atom
|
||||
* {:global, term}
|
||||
* {:via, module, term}
|
||||
|
||||
Got: #{inspect(other)}
|
||||
"""
|
||||
{other, opts} when is_tuple(other) ->
|
||||
:gen.start(:gen_server, link, other, module, args, opts)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the server with the given `reason`.
|
||||
|
||||
The `c:terminate/2` callback of the given `server` will be invoked before
|
||||
exiting. This function returns `:ok` if the server terminates with the
|
||||
given reason; if it terminates with another reason, the call exits.
|
||||
The `terminate/2` callback will be invoked before exiting.
|
||||
It returns `:ok` if the server terminates with the given
|
||||
reason, if it terminates with another reason, the call will
|
||||
exit.
|
||||
|
||||
This function keeps OTP semantics regarding error reporting.
|
||||
If the reason is any other than `:normal`, `:shutdown` or
|
||||
`{:shutdown, _}`, an error report is logged.
|
||||
`{:shutdown, _}`, an error report will be logged.
|
||||
"""
|
||||
@spec stop(server, reason :: term, timeout) :: :ok
|
||||
def stop(server, reason \\ :normal, timeout \\ :infinity) do
|
||||
@@ -704,52 +538,50 @@ defmodule GenServer do
|
||||
Makes a synchronous call to the `server` and waits for its reply.
|
||||
|
||||
The client sends the given `request` to the server and waits until a reply
|
||||
arrives or a timeout occurs. `c:handle_call/3` will be called on the server
|
||||
arrives or a timeout occurs. `handle_call/3` will be called on the server
|
||||
to handle the request.
|
||||
|
||||
`server` can be any of the values described in the "Name registration"
|
||||
section of the documentation for this module.
|
||||
The server can be any of the values described in the `Name Registration`
|
||||
section of the module documentation.
|
||||
|
||||
## Timeouts
|
||||
|
||||
`timeout` is an integer greater than zero which specifies how many
|
||||
The `timeout` is an integer greater than zero which specifies how many
|
||||
milliseconds to wait for a reply, or the atom `:infinity` to wait
|
||||
indefinitely. The default value is `5000`. If no reply is received within
|
||||
the specified time, the function call fails and the caller exits. If the
|
||||
caller catches the failure and continues running, and the server is just late
|
||||
with the reply, it may arrive at any time later into the caller's message
|
||||
queue. The caller must in this case be prepared for this and discard any such
|
||||
garbage messages that are two-element tuples with a reference as the first
|
||||
element.
|
||||
indefinitely. The default value is 5000. If no reply is received within
|
||||
the specified time, the function call fails. If the caller catches the
|
||||
failure and continues running, and the server is just late with the reply,
|
||||
it may arrive at any time later into the caller's message queue. The caller
|
||||
must in this case be prepared for this and discard any such garbage messages
|
||||
that are two-element tuples with a reference as the first element.
|
||||
"""
|
||||
@spec call(server, term, timeout) :: term
|
||||
def call(server, request, timeout \\ 5000) do
|
||||
case whereis(server) do
|
||||
nil ->
|
||||
exit({:noproc, {__MODULE__, :call, [server, request, timeout]}})
|
||||
pid when pid == self() ->
|
||||
exit({:calling_self, {__MODULE__, :call, [server, request, timeout]}})
|
||||
pid ->
|
||||
try do
|
||||
:gen.call(pid, :"$gen_call", request, timeout)
|
||||
catch
|
||||
:exit, reason ->
|
||||
exit({reason, {__MODULE__, :call, [server, request, timeout]}})
|
||||
else
|
||||
{:ok, res} -> res
|
||||
end
|
||||
try do
|
||||
:gen.call(server, :"$gen_call", request, timeout)
|
||||
catch
|
||||
:exit, reason ->
|
||||
exit({reason, {__MODULE__, :call, [server, request, timeout]}})
|
||||
else
|
||||
{:ok, res} -> res
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends an asynchronous request to the `server`.
|
||||
|
||||
This function always returns `:ok` regardless of whether
|
||||
the destination `server` (or node) exists. Therefore it
|
||||
This function returns `:ok` without waiting for the
|
||||
destination `server` to handle the message. Therefore it
|
||||
is unknown whether the destination `server` successfully
|
||||
handled the message.
|
||||
handled the message. If the `server` is an atom without
|
||||
an associated process an `ArgumentError` is raised. In
|
||||
all other cases the function returns `:ok` regardless of
|
||||
whether the destination `server` (or node) exists. Note
|
||||
that `{name, node()}` can be used when an exception is
|
||||
not desired if no process is locally associated with the
|
||||
atom `name`.
|
||||
|
||||
`c:handle_cast/2` will be called on the server to handle
|
||||
`handle_cast/2` will be called on the server to handle
|
||||
the request. In case the `server` is on a node which is
|
||||
not yet connected to the caller one, the call is going to
|
||||
block until a connection happens. This is different than
|
||||
@@ -787,13 +619,13 @@ defmodule GenServer do
|
||||
@doc """
|
||||
Casts all servers locally registered as `name` at the specified nodes.
|
||||
|
||||
This function returns immediately and ignores nodes that do not exist, or where the
|
||||
The function returns immediately and ignores nodes that do not exist, or where the
|
||||
server name does not exist.
|
||||
|
||||
See `multi_call/4` for more information.
|
||||
"""
|
||||
@spec abcast([node], name :: atom, term) :: :abcast
|
||||
def abcast(nodes \\ [node() | Node.list()], name, request) when is_list(nodes) and is_atom(name) do
|
||||
def abcast(nodes \\ nodes(), name, request) when is_list(nodes) and is_atom(name) do
|
||||
msg = cast_msg(request)
|
||||
_ = for node <- nodes, do: do_send({name, node}, msg)
|
||||
:abcast
|
||||
@@ -804,83 +636,49 @@ defmodule GenServer do
|
||||
end
|
||||
|
||||
defp do_send(dest, msg) do
|
||||
try do
|
||||
send(dest, msg)
|
||||
:ok
|
||||
catch
|
||||
_, _ -> :ok
|
||||
end
|
||||
send(dest, msg)
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calls all servers locally registered as `name` at the specified `nodes`.
|
||||
|
||||
First, the `request` is sent to every node in `nodes`; then, the caller waits
|
||||
for the replies. This function returns a two-element tuple `{replies,
|
||||
bad_nodes}` where:
|
||||
The `request` is first sent to every node and then we wait for the
|
||||
replies. This function returns a tuple containing the node and its reply
|
||||
as first element and all bad nodes as second element. The bad nodes is a
|
||||
list of nodes that either did not exist, or where a server with the given
|
||||
`name` did not exist or did not reply.
|
||||
|
||||
* `replies` - is a list of `{node, reply}` tuples where `node` is the node
|
||||
that replied and `reply` is its reply
|
||||
* `bad_nodes` - is a list of nodes that either did not exist or where a
|
||||
server with the given `name` did not exist or did not reply
|
||||
|
||||
`nodes` is a list of node names to which the request is sent. The default
|
||||
value is the list of all known nodes (including this node).
|
||||
Nodes is a list of node names to which the request is sent. The default
|
||||
value is the list of all known nodes.
|
||||
|
||||
To avoid that late answers (after the timeout) pollute the caller's message
|
||||
queue, a middleman process is used to do the actual calls. Late answers will
|
||||
then be discarded when they arrive to a terminated process.
|
||||
|
||||
## Examples
|
||||
|
||||
Assuming the `Stack` GenServer mentioned in the docs for the `GenServer`
|
||||
module is registered as `Stack` in the `:"foo@my-machine"` and
|
||||
`:"bar@my-machine"` nodes:
|
||||
|
||||
GenServer.multi_call(Stack, :pop)
|
||||
#=> {[{:"foo@my-machine", :hello}, {:"bar@my-machine", :world}], []}
|
||||
|
||||
"""
|
||||
@spec multi_call([node], name :: atom, term, timeout) ::
|
||||
{replies :: [{node, term}], bad_nodes :: [node]}
|
||||
def multi_call(nodes \\ [node() | Node.list()], name, request, timeout \\ :infinity) do
|
||||
def multi_call(nodes \\ nodes(), name, request, timeout \\ :infinity) do
|
||||
:gen_server.multi_call(nodes, name, request, timeout)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Replies to a client.
|
||||
|
||||
This function can be used to explicitly send a reply to a client that called
|
||||
`call/3` or `multi_call/4` when the reply cannot be specified in the return
|
||||
value of `c:handle_call/3`.
|
||||
This function can be used by a server to explicitly send a reply to a
|
||||
client that called `call/3` or `multi_call/4`. When the reply cannot be
|
||||
defined in the return value of `handle_call/3`.
|
||||
|
||||
`client` must be the `from` argument (the second argument) accepted by
|
||||
`c:handle_call/3` callbacks. `reply` is an arbitrary term which will be given
|
||||
back to the client as the return value of the call.
|
||||
|
||||
Note that `reply/2` can be called from any process, not just the GenServer
|
||||
that originally received the call (as long as that GenServer communicated the
|
||||
`from` argument somehow).
|
||||
The `client` must be the `from` argument (the second argument) received
|
||||
in `handle_call/3` callbacks. Reply is an arbitrary term which will be
|
||||
given back to the client as the return value of the call.
|
||||
|
||||
This function always returns `:ok`.
|
||||
|
||||
## Examples
|
||||
|
||||
def handle_call(:reply_in_one_second, from, state) do
|
||||
Process.send_after(self(), {:reply, from}, 1_000)
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
def handle_info({:reply, from}, state) do
|
||||
GenServer.reply(from, :one_second_has_passed)
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
"""
|
||||
@spec reply(from, term) :: :ok
|
||||
def reply(client, reply)
|
||||
|
||||
def reply({to, tag}, reply) when is_pid(to) do
|
||||
def reply({to, tag}, reply) do
|
||||
try do
|
||||
send(to, {tag, reply})
|
||||
:ok
|
||||
@@ -890,12 +688,10 @@ defmodule GenServer do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the `pid` or `{name, node}` of a GenServer process, or `nil` if
|
||||
no process is associated with the given name.
|
||||
Returns the `pid` or `{name, node}` of a GenServer process.
|
||||
Returns `nil` if no process is associated with the given name.
|
||||
|
||||
## Examples
|
||||
|
||||
For example, to lookup a server process, monitor it and send a cast to it:
|
||||
For example, to lookup a server process, monitor it and send a cast:
|
||||
|
||||
process = GenServer.whereis(server)
|
||||
monitor = Process.monitor(process)
|
||||
@@ -925,4 +721,10 @@ defmodule GenServer do
|
||||
def whereis({name, node} = server) when is_atom(name) and is_atom(node) do
|
||||
server
|
||||
end
|
||||
|
||||
@compile {:inline, [nodes: 0]}
|
||||
|
||||
defp nodes do
|
||||
[node()|:erlang.nodes()]
|
||||
end
|
||||
end
|
||||
|
||||
+17
-19
@@ -5,8 +5,6 @@ defmodule HashDict do
|
||||
Use the `Map` module instead.
|
||||
"""
|
||||
|
||||
# TODO: Deprecate every function by 1.4
|
||||
|
||||
use Dict
|
||||
|
||||
@node_bitmap 0b111
|
||||
@@ -92,7 +90,7 @@ defmodule HashDict do
|
||||
defp do_fetch(node, key, hash) do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[^key | v] -> {:ok, v}
|
||||
[^key|v] -> {:ok, v}
|
||||
{^key, v, _} -> {:ok, v}
|
||||
{_, _, n} -> do_fetch(n, key, key_shift(hash))
|
||||
_ -> :error
|
||||
@@ -103,11 +101,11 @@ defmodule HashDict do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
{put_elem(node, index, [key | value]), 1}
|
||||
[^key | _] ->
|
||||
{put_elem(node, index, [key | value]), 0}
|
||||
[k | v] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [key | value])
|
||||
{put_elem(node, index, [key|value]), 1}
|
||||
[^key|_] ->
|
||||
{put_elem(node, index, [key|value]), 0}
|
||||
[k|v] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [key|value])
|
||||
{put_elem(node, index, {k, v, n}), 1}
|
||||
{^key, _, n} ->
|
||||
{put_elem(node, index, {key, value, n}), 0}
|
||||
@@ -121,11 +119,11 @@ defmodule HashDict do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
{put_elem(node, index, [key | initial.()]), 1}
|
||||
[^key | value] ->
|
||||
{put_elem(node, index, [key | fun.(value)]), 0}
|
||||
[k | v] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [key | initial.()])
|
||||
{put_elem(node, index, [key|initial.()]), 1}
|
||||
[^key|value] ->
|
||||
{put_elem(node, index, [key|fun.(value)]), 0}
|
||||
[k|v] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [key|initial.()])
|
||||
{put_elem(node, index, {k, v, n}), 1}
|
||||
{^key, value, n} ->
|
||||
{put_elem(node, index, {key, fun.(value), n}), 0}
|
||||
@@ -140,16 +138,16 @@ defmodule HashDict do
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
:error
|
||||
[^key | value] ->
|
||||
[^key|value] ->
|
||||
{put_elem(node, index, []), value}
|
||||
[_ | _] ->
|
||||
[_|_] ->
|
||||
:error
|
||||
{^key, value, n} ->
|
||||
{put_elem(node, index, do_compact_node(n)), value}
|
||||
{k, v, n} ->
|
||||
case do_delete(n, key, key_shift(hash)) do
|
||||
{@node_template, value} ->
|
||||
{put_elem(node, index, [k | v]), value}
|
||||
{put_elem(node, index, [k|v]), value}
|
||||
{n, value} ->
|
||||
{put_elem(node, index, {k, v, n}), value}
|
||||
:error ->
|
||||
@@ -161,9 +159,9 @@ defmodule HashDict do
|
||||
Enum.each 0..(@node_size - 1), fn index ->
|
||||
defp do_compact_node(node) when elem(node, unquote(index)) != [] do
|
||||
case elem(node, unquote(index)) do
|
||||
[k | v] ->
|
||||
[k|v] ->
|
||||
case put_elem(node, unquote(index), []) do
|
||||
@node_template -> [k | v]
|
||||
@node_template -> [k|v]
|
||||
n -> {k, v, n}
|
||||
end
|
||||
{k, v, n} ->
|
||||
@@ -186,7 +184,7 @@ defmodule HashDict do
|
||||
next.(acc)
|
||||
end
|
||||
|
||||
defp do_reduce_each([k | v], {:cont, acc}, fun, next) do
|
||||
defp do_reduce_each([k|v], {:cont, acc}, fun, next) do
|
||||
next.(fun.({k, v}, acc))
|
||||
end
|
||||
|
||||
|
||||
+20
-20
@@ -5,7 +5,7 @@ defmodule HashSet do
|
||||
Use the `MapSet` module instead.
|
||||
"""
|
||||
|
||||
# TODO: Deprecate every function by 1.4
|
||||
@behaviour Set
|
||||
|
||||
@node_bitmap 0b111
|
||||
@node_shift 3
|
||||
@@ -44,7 +44,7 @@ defmodule HashSet do
|
||||
end
|
||||
|
||||
def to_list(set) do
|
||||
set_fold(set, [], &[&1 | &2]) |> :lists.reverse
|
||||
set_fold(set, [], &[&1|&2]) |> :lists.reverse
|
||||
end
|
||||
|
||||
def equal?(%HashSet{size: size1} = set1, %HashSet{size: size2} = set2) do
|
||||
@@ -112,10 +112,10 @@ defmodule HashSet do
|
||||
defp do_member?(node, term, hash) do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[] -> false
|
||||
[^term | _] -> true
|
||||
[_] -> false
|
||||
[_ | n] -> do_member?(n, term, key_shift(hash))
|
||||
[] -> false
|
||||
[^term|_] -> true
|
||||
[_] -> false
|
||||
[_|n] -> do_member?(n, term, key_shift(hash))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -124,14 +124,14 @@ defmodule HashSet do
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
{put_elem(node, index, [term]), 1}
|
||||
[^term | _] ->
|
||||
[^term|_] ->
|
||||
{node, 0}
|
||||
[t] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [term])
|
||||
{put_elem(node, index, [t | n]), 1}
|
||||
[t | n] ->
|
||||
{put_elem(node, index, [t|n]), 1}
|
||||
[t|n] ->
|
||||
{n, counter} = do_put(n, term, key_shift(hash))
|
||||
{put_elem(node, index, [t | n]), counter}
|
||||
{put_elem(node, index, [t|n]), counter}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -144,14 +144,14 @@ defmodule HashSet do
|
||||
{:ok, put_elem(node, index, [])}
|
||||
[_] ->
|
||||
:error
|
||||
[^term | n] ->
|
||||
[^term|n] ->
|
||||
{:ok, put_elem(node, index, do_compact_node(n))}
|
||||
[t | n] ->
|
||||
[t|n] ->
|
||||
case do_delete(n, term, key_shift(hash)) do
|
||||
{:ok, @node_template} ->
|
||||
{:ok, put_elem(node, index, [t])}
|
||||
{:ok, n} ->
|
||||
{:ok, put_elem(node, index, [t | n])}
|
||||
{:ok, put_elem(node, index, [t|n])}
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
@@ -164,19 +164,19 @@ defmodule HashSet do
|
||||
[t] ->
|
||||
case put_elem(node, unquote(index), []) do
|
||||
@node_template -> [t]
|
||||
n -> [t | n]
|
||||
n -> [t|n]
|
||||
end
|
||||
[t | n] ->
|
||||
[t | put_elem(node, unquote(index), do_compact_node(n))]
|
||||
[t|n] ->
|
||||
[t|put_elem(node, unquote(index), do_compact_node(n))]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
## Set fold
|
||||
|
||||
defp do_fold_each([], acc, _fun), do: acc
|
||||
defp do_fold_each([t], acc, fun), do: fun.(t, acc)
|
||||
defp do_fold_each([t | n], acc, fun), do: do_fold(n, fun.(t, acc), fun, @node_size)
|
||||
defp do_fold_each([], acc, _fun), do: acc
|
||||
defp do_fold_each([t], acc, fun), do: fun.(t, acc)
|
||||
defp do_fold_each([t|n], acc, fun), do: do_fold(n, fun.(t, acc), fun, @node_size)
|
||||
|
||||
defp do_fold(node, acc, fun, count) when count > 0 do
|
||||
acc = do_fold_each(:erlang.element(count, node), acc, fun)
|
||||
@@ -205,7 +205,7 @@ defmodule HashSet do
|
||||
next.(fun.(t, acc))
|
||||
end
|
||||
|
||||
defp do_reduce_each([t | n], {:cont, acc}, fun, next) do
|
||||
defp do_reduce_each([t|n], {:cont, acc}, fun, next) do
|
||||
do_reduce(n, fun.(t, acc), fun, @node_size, next)
|
||||
end
|
||||
|
||||
|
||||
+126
-151
@@ -27,7 +27,7 @@ defprotocol Inspect do
|
||||
end
|
||||
end
|
||||
|
||||
The `concat/1` function comes from `Inspect.Algebra` and it
|
||||
The `concat` function comes from `Inspect.Algebra` and it
|
||||
concatenates algebra documents together. In the example above,
|
||||
it is concatenating the string `"MapSet<"` (all strings are
|
||||
valid algebra documents that keep their formatting when pretty
|
||||
@@ -48,27 +48,23 @@ defprotocol Inspect do
|
||||
implementation directly. For example, to test Inspect.MapSet above,
|
||||
you can invoke it as:
|
||||
|
||||
Inspect.MapSet.inspect(MapSet.new, %Inspect.Opts{})
|
||||
Inspect.MapSet.inspect(MapSet.new, Inspect.Opts.new)
|
||||
|
||||
"""
|
||||
|
||||
# Handle structs in Any
|
||||
@fallback_to_any true
|
||||
|
||||
def inspect(term, opts)
|
||||
def inspect(thing, opts)
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Atom do
|
||||
require Macro
|
||||
|
||||
def inspect(atom, opts) do
|
||||
color(inspect(atom), color_key(atom), opts)
|
||||
def inspect(atom, _opts) do
|
||||
inspect(atom)
|
||||
end
|
||||
|
||||
defp color_key(atom) when is_boolean(atom), do: :boolean
|
||||
defp color_key(nil), do: :nil
|
||||
defp color_key(_), do: :atom
|
||||
|
||||
def inspect(false), do: "false"
|
||||
def inspect(true), do: "true"
|
||||
def inspect(nil), do: "nil"
|
||||
@@ -92,7 +88,7 @@ defimpl Inspect, for: Atom do
|
||||
atom in Macro.binary_ops or atom in Macro.unary_ops ->
|
||||
":" <> binary
|
||||
true ->
|
||||
IO.iodata_to_binary [?:, ?", Inspect.BitString.escape(binary, ?"), ?"]
|
||||
<<?:, ?", Inspect.BitString.escape(binary, ?")::binary, ?">>
|
||||
end
|
||||
end
|
||||
|
||||
@@ -145,132 +141,130 @@ defimpl Inspect, for: Atom do
|
||||
end
|
||||
|
||||
defimpl Inspect, for: BitString do
|
||||
def inspect(term, %Inspect.Opts{binaries: bins, base: base} = opts) when is_binary(term) do
|
||||
if base == :decimal and (bins == :as_strings or (bins == :infer and String.printable?(term))) do
|
||||
inspected = IO.iodata_to_binary([?", escape(term, ?"), ?"])
|
||||
color(inspected, :string, opts)
|
||||
def inspect(thing, %Inspect.Opts{binaries: bins} = opts) when is_binary(thing) do
|
||||
if bins == :as_strings or (bins == :infer and String.printable?(thing)) do
|
||||
<<?", escape(thing, ?")::binary, ?">>
|
||||
else
|
||||
inspect_bitstring(term, opts)
|
||||
inspect_bitstring(thing, opts)
|
||||
end
|
||||
end
|
||||
|
||||
def inspect(term, opts) do
|
||||
inspect_bitstring(term, opts)
|
||||
def inspect(thing, opts) do
|
||||
inspect_bitstring(thing, opts)
|
||||
end
|
||||
|
||||
## Escaping
|
||||
|
||||
@doc false
|
||||
def escape(other, char) do
|
||||
escape(other, char, [])
|
||||
escape(other, char, <<>>)
|
||||
end
|
||||
|
||||
defp escape(<<char, t::binary>>, char, acc) do
|
||||
escape(t, char, [acc | [?\\, char]])
|
||||
defp escape(<<char, t::binary >>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, char>>)
|
||||
end
|
||||
defp escape(<<?#, ?{, t::binary>>, char, acc) do
|
||||
escape(t, char, [acc | '\\\#{'])
|
||||
defp escape(<<?#, ?{, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?#, ?{>>)
|
||||
end
|
||||
defp escape(<<?\a, t::binary>>, char, acc) do
|
||||
escape(t, char, [acc | '\\a'])
|
||||
defp escape(<<?\a, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?a>>)
|
||||
end
|
||||
defp escape(<<?\b, t::binary>>, char, acc) do
|
||||
escape(t, char, [acc | '\\b'])
|
||||
defp escape(<<?\b, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?b>>)
|
||||
end
|
||||
defp escape(<<?\d, t::binary>>, char, acc) do
|
||||
escape(t, char, [acc | '\\d'])
|
||||
defp escape(<<?\d, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?d>>)
|
||||
end
|
||||
defp escape(<<?\e, t::binary>>, char, acc) do
|
||||
escape(t, char, [acc | '\\e'])
|
||||
defp escape(<<?\e, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?e>>)
|
||||
end
|
||||
defp escape(<<?\f, t::binary>>, char, acc) do
|
||||
escape(t, char, [acc | '\\f'])
|
||||
defp escape(<<?\f, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?f>>)
|
||||
end
|
||||
defp escape(<<?\n, t::binary>>, char, acc) do
|
||||
escape(t, char, [acc | '\\n'])
|
||||
defp escape(<<?\n, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?n>>)
|
||||
end
|
||||
defp escape(<<?\r, t::binary>>, char, acc) do
|
||||
escape(t, char, [acc | '\\r'])
|
||||
defp escape(<<?\r, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?r>>)
|
||||
end
|
||||
defp escape(<<?\\, t::binary>>, char, acc) do
|
||||
escape(t, char, [acc | '\\\\'])
|
||||
defp escape(<<?\\, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?\\>>)
|
||||
end
|
||||
defp escape(<<?\t, t::binary>>, char, acc) do
|
||||
escape(t, char, [acc | '\\t'])
|
||||
defp escape(<<?\t, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?t>>)
|
||||
end
|
||||
defp escape(<<?\v, t::binary>>, char, acc) do
|
||||
escape(t, char, [acc | '\\v'])
|
||||
defp escape(<<?\v, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?v>>)
|
||||
end
|
||||
defp escape(<<h::utf8, t::binary>>, char, acc)
|
||||
when h in 0x20..0x7E
|
||||
when h in 0xA0..0xD7FF
|
||||
when h in 0xE000..0xFFFD
|
||||
when h in 0x10000..0x10FFFF do
|
||||
escape(t, char, [acc | <<h::utf8>>])
|
||||
defp escape(<<h::utf8, t::binary>>, char, binary) do
|
||||
head = <<h::utf8 >>
|
||||
if String.printable?(head) do
|
||||
escape(t, char, append(head, binary))
|
||||
else
|
||||
<<byte::8, h::binary >> = head
|
||||
t = <<h::binary, t::binary>>
|
||||
escape(t, char, <<binary::binary, escape_char(byte)::binary>>)
|
||||
end
|
||||
end
|
||||
defp escape(<<h, t::binary>>, char, acc) do
|
||||
escape(t, char, [acc | escape_char(h)])
|
||||
defp escape(<<h, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, escape_char(h)::binary>>)
|
||||
end
|
||||
defp escape(<<>>, _char, acc), do: acc
|
||||
defp escape(<<>>, _char, binary), do: binary
|
||||
|
||||
@doc false
|
||||
# Also used by Regex
|
||||
def escape_char(0) do
|
||||
'\\0'
|
||||
<<?\\, ?0>>
|
||||
end
|
||||
|
||||
def escape_char(char) when char < 0x100 do
|
||||
<<a::4, b::4>> = <<char::8>>
|
||||
['\\x', to_hex(a), to_hex(b)]
|
||||
<<?\\, ?x, to_hex(a), to_hex(b)>>
|
||||
end
|
||||
|
||||
def escape_char(char) when char < 0x10000 do
|
||||
<<a::4, b::4, c::4, d::4>> = <<char::16>>
|
||||
['\\x{', to_hex(a), to_hex(b), to_hex(c), to_hex(d), ?}]
|
||||
<<?\\, ?x, ?{, to_hex(a), to_hex(b), to_hex(c), to_hex(d), ?}>>
|
||||
end
|
||||
|
||||
def escape_char(char) when char < 0x1000000 do
|
||||
<<a::4, b::4, c::4, d::4, e::4, f::4>> = <<char::24>>
|
||||
['\\x{', to_hex(a), to_hex(b), to_hex(c),
|
||||
to_hex(d), to_hex(e), to_hex(f), ?}]
|
||||
<<?\\, ?x, ?{, to_hex(a), to_hex(b), to_hex(c),
|
||||
to_hex(d), to_hex(e), to_hex(f), ?}>>
|
||||
end
|
||||
|
||||
defp to_hex(c) when c in 0..9, do: ?0+c
|
||||
defp to_hex(c) when c in 10..15, do: ?A+c-10
|
||||
|
||||
defp append(<<h, t::binary>>, binary), do: append(t, <<binary::binary, h>>)
|
||||
defp append(<<>>, binary), do: binary
|
||||
|
||||
## Bitstrings
|
||||
|
||||
defp inspect_bitstring("", opts) do
|
||||
color("<<>>", :binary, opts)
|
||||
end
|
||||
|
||||
defp inspect_bitstring(bitstring, opts) do
|
||||
left = color("<<", :binary, opts)
|
||||
right = color(">>", :binary, opts)
|
||||
nest surround(left, each_bit(bitstring, opts.limit, opts), right), 1
|
||||
each_bit(bitstring, opts.limit, "<<") <> ">>"
|
||||
end
|
||||
|
||||
defp each_bit(_, 0, _) do
|
||||
"..."
|
||||
defp each_bit(_, 0, acc) do
|
||||
acc <> "..."
|
||||
end
|
||||
|
||||
defp each_bit(<<>>, _counter, _opts) do
|
||||
:doc_nil
|
||||
defp each_bit(<<h, t::bitstring>>, counter, acc) when t != <<>> do
|
||||
each_bit(t, decrement(counter), acc <> Integer.to_string(h) <> ", ")
|
||||
end
|
||||
|
||||
defp each_bit(<<h::8>>, _counter, opts) do
|
||||
Inspect.Integer.inspect(h, opts)
|
||||
defp each_bit(<<h::8>>, _counter, acc) do
|
||||
acc <> Integer.to_string(h)
|
||||
end
|
||||
|
||||
defp each_bit(<<h, t::bitstring>>, counter, opts) do
|
||||
glue(concat(Inspect.Integer.inspect(h, opts), ","),
|
||||
each_bit(t, decrement(counter), opts))
|
||||
defp each_bit(<<>>, _counter, acc) do
|
||||
acc
|
||||
end
|
||||
|
||||
defp each_bit(bitstring, _counter, opts) do
|
||||
defp each_bit(bitstring, _counter, acc) do
|
||||
size = bit_size(bitstring)
|
||||
<<h::size(size)>> = bitstring
|
||||
Inspect.Integer.inspect(h, opts) <> "::size(" <> Integer.to_string(size) <> ")"
|
||||
acc <> Integer.to_string(h) <> "::size(" <> Integer.to_string(size) <> ")"
|
||||
end
|
||||
|
||||
defp decrement(:infinity), do: :infinity
|
||||
@@ -278,47 +272,30 @@ defimpl Inspect, for: BitString do
|
||||
end
|
||||
|
||||
defimpl Inspect, for: List do
|
||||
def inspect([], opts) do
|
||||
color("[]", :list, opts)
|
||||
end
|
||||
|
||||
# TODO: Deprecate :char_lists and :as_char_lists keys in v1.5
|
||||
def inspect(term, %Inspect.Opts{charlists: lists, char_lists: lists_deprecated} = opts) do
|
||||
lists =
|
||||
if lists == :infer and lists_deprecated != :infer do
|
||||
case lists_deprecated do
|
||||
:as_char_lists ->
|
||||
:as_charlists
|
||||
_ ->
|
||||
lists_deprecated
|
||||
end
|
||||
else
|
||||
lists
|
||||
end
|
||||
|
||||
open = color("[", :list, opts)
|
||||
sep = color(",", :list, opts)
|
||||
close = color("]", :list, opts)
|
||||
def inspect([], _opts), do: "[]"
|
||||
|
||||
def inspect(thing, %Inspect.Opts{char_lists: lists} = opts) do
|
||||
cond do
|
||||
lists == :as_charlists or (lists == :infer and printable?(term)) ->
|
||||
IO.iodata_to_binary [?', Inspect.BitString.escape(IO.chardata_to_string(term), ?'), ?']
|
||||
keyword?(term) ->
|
||||
surround_many(open, term, close, opts, &keyword/2, sep)
|
||||
lists == :as_char_lists or (lists == :infer and printable?(thing)) ->
|
||||
<<?', Inspect.BitString.escape(IO.chardata_to_string(thing), ?')::binary, ?'>>
|
||||
keyword?(thing) ->
|
||||
surround_many("[", thing, "]", opts, &keyword/2)
|
||||
true ->
|
||||
surround_many(open, term, close, opts, &to_doc/2, sep)
|
||||
surround_many("[", thing, "]", opts, &to_doc/2)
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def keyword({key, value}, opts) do
|
||||
key = color(key_to_binary(key) <> ": ", :atom, opts)
|
||||
concat(key, to_doc(value, opts))
|
||||
concat(
|
||||
key_to_binary(key) <> ": ",
|
||||
to_doc(value, opts)
|
||||
)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def keyword?([{key, _value} | rest]) when is_atom(key) do
|
||||
case Atom.to_charlist(key) do
|
||||
case Atom.to_char_list(key) do
|
||||
'Elixir.' ++ _ -> false
|
||||
_ -> keyword?(rest)
|
||||
end
|
||||
@@ -328,15 +305,15 @@ defimpl Inspect, for: List do
|
||||
def keyword?(_other), do: false
|
||||
|
||||
@doc false
|
||||
def printable?([char | rest]) when char in 32..126, do: printable?(rest)
|
||||
def printable?([?\n | rest]), do: printable?(rest)
|
||||
def printable?([?\r | rest]), do: printable?(rest)
|
||||
def printable?([?\t | rest]), do: printable?(rest)
|
||||
def printable?([?\v | rest]), do: printable?(rest)
|
||||
def printable?([?\b | rest]), do: printable?(rest)
|
||||
def printable?([?\f | rest]), do: printable?(rest)
|
||||
def printable?([?\e | rest]), do: printable?(rest)
|
||||
def printable?([?\a | rest]), do: printable?(rest)
|
||||
def printable?([c|cs]) when is_integer(c) and c in 32..126, do: printable?(cs)
|
||||
def printable?([?\n|cs]), do: printable?(cs)
|
||||
def printable?([?\r|cs]), do: printable?(cs)
|
||||
def printable?([?\t|cs]), do: printable?(cs)
|
||||
def printable?([?\v|cs]), do: printable?(cs)
|
||||
def printable?([?\b|cs]), do: printable?(cs)
|
||||
def printable?([?\f|cs]), do: printable?(cs)
|
||||
def printable?([?\e|cs]), do: printable?(cs)
|
||||
def printable?([?\a|cs]), do: printable?(cs)
|
||||
def printable?([]), do: true
|
||||
def printable?(_), do: false
|
||||
|
||||
@@ -351,11 +328,10 @@ defimpl Inspect, for: List do
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Tuple do
|
||||
def inspect({}, _opts), do: "{}"
|
||||
|
||||
def inspect(tuple, opts) do
|
||||
open = color("{", :tuple, opts)
|
||||
sep = color(",", :tuple, opts)
|
||||
close = color("}", :tuple, opts)
|
||||
surround_many(open, Tuple.to_list(tuple), close, opts, &to_doc/2, sep)
|
||||
surround_many("{", Tuple.to_list(tuple), "}", opts, &to_doc/2)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -366,10 +342,7 @@ defimpl Inspect, for: Map do
|
||||
|
||||
def inspect(map, name, opts) do
|
||||
map = :maps.to_list(map)
|
||||
open = color("%" <> name <> "{", :map, opts)
|
||||
sep = color(",", :map, opts)
|
||||
close = color("}", :map, opts)
|
||||
surround_many(open, map, close, opts, traverse_fun(map), sep)
|
||||
surround_many("%" <> name <> "{", map, "}", opts, traverse_fun(map))
|
||||
end
|
||||
|
||||
defp traverse_fun(list) do
|
||||
@@ -389,9 +362,9 @@ defimpl Inspect, for: Map do
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Integer do
|
||||
def inspect(term, %Inspect.Opts{base: base} = opts) do
|
||||
inspected = Integer.to_string(term, base_to_value(base)) |> prepend_prefix(base)
|
||||
color(inspected, :number, opts)
|
||||
def inspect(thing, %Inspect.Opts{base: base}) do
|
||||
Integer.to_string(thing, base_to_value(base))
|
||||
|> prepend_prefix(base)
|
||||
end
|
||||
|
||||
defp base_to_value(base) do
|
||||
@@ -415,57 +388,60 @@ defimpl Inspect, for: Integer do
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Float do
|
||||
def inspect(term, opts) do
|
||||
inspected = IO.iodata_to_binary(:io_lib_format.fwrite_g(term))
|
||||
color(inspected, :number, opts)
|
||||
def inspect(thing, _opts) do
|
||||
IO.iodata_to_binary(:io_lib_format.fwrite_g(thing))
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Regex do
|
||||
def inspect(regex, opts) do
|
||||
source = IO.iodata_to_binary(['~r/', escape(regex.source, ?/), ?/, regex.opts])
|
||||
color(source, :regex, opts)
|
||||
def inspect(regex, _opts) do
|
||||
delim = ?/
|
||||
concat ["~r",
|
||||
<<delim, escape(regex.source, delim)::binary, delim>>,
|
||||
regex.opts]
|
||||
end
|
||||
|
||||
defp escape(bin, term),
|
||||
do: escape(bin, [], term)
|
||||
do: escape(bin, <<>>, term)
|
||||
|
||||
defp escape(<<?\\, term>> <> rest, buf, term),
|
||||
do: escape(rest, [buf | [?\\, term]], term)
|
||||
do: escape(rest, buf <> <<?\\, term>>, term)
|
||||
|
||||
defp escape(<<term>> <> rest, buf, term),
|
||||
do: escape(rest, [buf | [?\\, term]], term)
|
||||
do: escape(rest, buf <> <<?\\, term>>, term)
|
||||
|
||||
# The list of characters is from 'String.printable?' implementation
|
||||
# the list of characters is from "String.printable?" impl
|
||||
# minus characters treated specially by regex: \s, \d, \b, \e
|
||||
|
||||
defp escape(<<?\n>> <> rest, buf, term),
|
||||
do: escape(rest, [buf | '\\n'], term)
|
||||
do: escape(rest, <<buf::binary, ?\\, ?n>>, term)
|
||||
|
||||
defp escape(<<?\r>> <> rest, buf, term),
|
||||
do: escape(rest, [buf | '\\r'], term)
|
||||
do: escape(rest, <<buf::binary, ?\\, ?r>>, term)
|
||||
|
||||
defp escape(<<?\t>> <> rest, buf, term),
|
||||
do: escape(rest, [buf | '\\t'], term)
|
||||
do: escape(rest, <<buf::binary, ?\\, ?t>>, term)
|
||||
|
||||
defp escape(<<?\v>> <> rest, buf, term),
|
||||
do: escape(rest, [buf | '\\v'], term)
|
||||
do: escape(rest, <<buf::binary, ?\\, ?v>>, term)
|
||||
|
||||
defp escape(<<?\f>> <> rest, buf, term),
|
||||
do: escape(rest, [buf | '\\f'], term)
|
||||
do: escape(rest, <<buf::binary, ?\\, ?f>>, term)
|
||||
|
||||
defp escape(<<?\a>> <> rest, buf, term),
|
||||
do: escape(rest, [buf | '\\a'], term)
|
||||
do: escape(rest, <<buf::binary, ?\\, ?a>>, term)
|
||||
|
||||
defp escape(<<char::utf8, rest::binary>>, buf, term)
|
||||
when char in 0x20..0x7E
|
||||
when char in 0xA0..0xD7FF
|
||||
when char in 0xE000..0xFFFD
|
||||
when char in 0x10000..0x10FFFF,
|
||||
do: escape(rest, [buf | <<char::utf8>>], term)
|
||||
defp escape(<<c::utf8>> <> rest, buf, term) do
|
||||
charstr = <<c::utf8>>
|
||||
if String.printable?(charstr) and not c in [?\d, ?\b, ?\e] do
|
||||
escape(rest, buf <> charstr, term)
|
||||
else
|
||||
escape(rest, buf <> Inspect.BitString.escape_char(c), term)
|
||||
end
|
||||
end
|
||||
|
||||
defp escape(<<char, rest::binary>>, buf, term),
|
||||
do: escape(rest, [buf | Inspect.BitString.escape_char(char)], term)
|
||||
defp escape(<<c>> <> rest, buf, term),
|
||||
do: escape(rest, <<buf::binary, Inspect.BitString.escape_char(c)>>, term)
|
||||
|
||||
defp escape(<<>>, buf, _), do: buf
|
||||
end
|
||||
@@ -478,7 +454,7 @@ defimpl Inspect, for: Function do
|
||||
if fun_info[:type] == :external and fun_info[:env] == [] do
|
||||
"&#{Inspect.Atom.inspect(mod)}.#{fun_info[:name]}/#{fun_info[:arity]}"
|
||||
else
|
||||
case Atom.to_charlist(mod) do
|
||||
case Atom.to_char_list(mod) do
|
||||
'elixir_compiler_' ++ _ ->
|
||||
if function_exported?(mod, :__RELATIVE__, 0) do
|
||||
"#Function<#{uniq(fun_info)} in file:#{mod.__RELATIVE__}>"
|
||||
@@ -522,7 +498,7 @@ end
|
||||
|
||||
defimpl Inspect, for: Port do
|
||||
def inspect(port, _opts) do
|
||||
IO.iodata_to_binary(:erlang.port_to_list(port))
|
||||
IO.iodata_to_binary :erlang.port_to_list(port)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -543,8 +519,7 @@ defimpl Inspect, for: Any do
|
||||
dunder ->
|
||||
if :maps.keys(dunder) == :maps.keys(map) do
|
||||
pruned = :maps.remove(:__exception__, :maps.remove(:__struct__, map))
|
||||
colorless_opts = %{opts | syntax_colors: []}
|
||||
Inspect.Map.inspect(pruned, Inspect.Atom.inspect(struct, colorless_opts), opts)
|
||||
Inspect.Map.inspect(pruned, Inspect.Atom.inspect(struct, opts), opts)
|
||||
else
|
||||
Inspect.Map.inspect(map, opts)
|
||||
end
|
||||
|
||||
@@ -15,67 +15,51 @@ defmodule Inspect.Opts do
|
||||
When the default `:infer`, the binary will be printed as a string if it
|
||||
is printable, otherwise in bit syntax.
|
||||
|
||||
* `:charlists` - when `:as_charlists` all lists will be printed as char
|
||||
* `:char_lists` - when `:as_char_lists` all lists will be printed as char
|
||||
lists, non-printable elements will be escaped.
|
||||
|
||||
When `:as_lists` all lists will be printed as lists.
|
||||
|
||||
When the default `:infer`, the list will be printed as a charlist if it
|
||||
When the default `:infer`, the list will be printed as a char list if it
|
||||
is printable, otherwise as list.
|
||||
|
||||
* `:limit` - limits the number of items that are printed for tuples,
|
||||
bitstrings, and lists, does not apply to strings nor charlists, defaults
|
||||
bitstrings, and lists, does not apply to strings nor char lists, defaults
|
||||
to 50.
|
||||
|
||||
* `:pretty` - if set to `true` enables pretty printing, defaults to `false`.
|
||||
|
||||
* `:width` - defaults to 80 characters, used when pretty is `true` or when
|
||||
printing to IO devices. Set to 0 to force each item to be printed on its
|
||||
own line.
|
||||
* `:width` - defaults to the 80 characters, used when pretty is `true` or
|
||||
when printing to IO devices.
|
||||
|
||||
* `:base` - prints integers as `:binary`, `:octal`, `:decimal`, or `:hex`, defaults
|
||||
to `:decimal`. When inspecting binaries any `:base` other than `:decimal`
|
||||
implies `binaries: :as_binaries`.
|
||||
* `:base` - print integers as :binary, :octal, :decimal, or :hex, defaults
|
||||
to :decimal
|
||||
|
||||
* `:safe` - when `false`, failures while inspecting structs will be raised
|
||||
as errors instead of being wrapped in the `Inspect.Error` exception. This
|
||||
as errors instead of being wrapped in the Inspect.Error exception. This
|
||||
is useful when debugging failures and crashes for custom inspect
|
||||
implementations
|
||||
|
||||
* `:syntax_colors` - when set to a keyword list of colors the output will
|
||||
be colorized. The keys are types and the values are the colors to use for
|
||||
each type. e.g. `[number: :red, atom: :blue]`. Types can include
|
||||
`:number`, `:atom`, `regex`, `:tuple`, `:map`, `:list`, and `:reset`.
|
||||
Colors can be any `t:IO.ANSI.ansidata/0` as accepted by `IO.ANSI.format/1`.
|
||||
"""
|
||||
|
||||
# TODO: Deprecate char_lists key by v1.5
|
||||
defstruct structs: true,
|
||||
binaries: :infer,
|
||||
charlists: :infer,
|
||||
char_lists: :infer,
|
||||
limit: 50,
|
||||
width: 80,
|
||||
base: :decimal,
|
||||
pretty: false,
|
||||
safe: true,
|
||||
syntax_colors: []
|
||||
safe: true
|
||||
|
||||
@type color_key :: atom
|
||||
|
||||
# TODO: Deprecate char_lists key and :as_char_lists value by v1.5
|
||||
@type t :: %__MODULE__{
|
||||
structs: boolean,
|
||||
binaries: :infer | :as_binaries | :as_strings,
|
||||
charlists: :infer | :as_lists | :as_charlists,
|
||||
char_lists: :infer | :as_lists | :as_char_lists,
|
||||
limit: pos_integer | :infinity,
|
||||
width: pos_integer | :infinity,
|
||||
base: :decimal | :binary | :hex | :octal,
|
||||
pretty: boolean,
|
||||
safe: boolean,
|
||||
syntax_colors: [{color_key, IO.ANSI.ansidata}]
|
||||
}
|
||||
safe: boolean}
|
||||
end
|
||||
|
||||
defmodule Inspect.Error do
|
||||
@@ -93,7 +77,7 @@ defmodule Inspect.Algebra do
|
||||
This module implements the functionality described in
|
||||
["Strictly Pretty" (2000) by Christian Lindig][0] with small
|
||||
additions, like support for String nodes, and a custom
|
||||
rendering function that maximises horizontal space use.
|
||||
rendering function that maximises horizontal space use.
|
||||
|
||||
iex> Inspect.Algebra.empty
|
||||
:doc_nil
|
||||
@@ -110,7 +94,7 @@ defmodule Inspect.Algebra do
|
||||
|
||||
The functions `nest/2`, `space/2` and `line/2` help you put the
|
||||
document together into a rigid structure. However, the document
|
||||
algebra gets interesting when using functions like `break/1`, which
|
||||
algebra gets interesting when using functions like `break/2`, which
|
||||
converts the given string into a line break depending on how much space
|
||||
there is to print. Let's glue two docs together with a break and then
|
||||
render it:
|
||||
@@ -136,7 +120,7 @@ defmodule Inspect.Algebra do
|
||||
`:flat` (breaks as spaces) and `:break` (breaks as newlines).
|
||||
Implementing the same logic in a strict language such as Elixir leads
|
||||
to an exponential growth of possible documents, unless document groups
|
||||
are encoded explicitly as `:flat` or `:break`. Those groups are then reduced
|
||||
are encoded explictly as `:flat` or `:break`. Those groups are then reduced
|
||||
to a simple document, where the layout is already decided, per [Lindig][0].
|
||||
|
||||
This implementation slightly changes the semantic of Lindig's algorithm
|
||||
@@ -163,7 +147,7 @@ defmodule Inspect.Algebra do
|
||||
|
||||
# Functional interface to "doc" records
|
||||
|
||||
@type t :: :doc_nil | :doc_line | doc_cons | doc_nest | doc_break | doc_group | doc_color | binary
|
||||
@type t :: :doc_nil | :doc_line | doc_cons | doc_nest | doc_break | doc_group | binary
|
||||
|
||||
@typep doc_cons :: {:doc_cons, t, t}
|
||||
defmacrop doc_cons(left, right) do
|
||||
@@ -185,11 +169,6 @@ defmodule Inspect.Algebra do
|
||||
quote do: {:doc_group, unquote(group)}
|
||||
end
|
||||
|
||||
@typep doc_color :: {:doc_color, t, IO.ANSI.ansidata}
|
||||
defmacrop doc_color(doc, color) do
|
||||
quote do: {:doc_color, unquote(doc), unquote(color)}
|
||||
end
|
||||
|
||||
defmacrop is_doc(doc) do
|
||||
if Macro.Env.in_guard?(__CALLER__) do
|
||||
do_is_doc(doc)
|
||||
@@ -207,17 +186,15 @@ defmodule Inspect.Algebra do
|
||||
is_binary(unquote(doc)) or
|
||||
unquote(doc) in [:doc_nil, :doc_line] or
|
||||
(is_tuple(unquote(doc)) and
|
||||
elem(unquote(doc), 0) in [:doc_cons, :doc_nest, :doc_break, :doc_group, :doc_color])
|
||||
elem(unquote(doc), 0) in [:doc_cons, :doc_nest, :doc_break, :doc_group])
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts an Elixir term to an algebra document
|
||||
according to the `Inspect` protocol.
|
||||
Converts an Elixir structure to an algebra document
|
||||
according to the inspect protocol.
|
||||
"""
|
||||
@spec to_doc(any, Inspect.Opts.t) :: t
|
||||
def to_doc(term, opts)
|
||||
|
||||
def to_doc(%{__struct__: struct} = map, %Inspect.Opts{} = opts) when is_atom(struct) do
|
||||
if opts.structs do
|
||||
try do
|
||||
@@ -278,50 +255,30 @@ defmodule Inspect.Algebra do
|
||||
def empty, do: :doc_nil
|
||||
|
||||
@doc """
|
||||
Concatenates two document entities returning a new document.
|
||||
Concatenates two document entities.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.concat("hello", "world")
|
||||
iex> doc = Inspect.Algebra.concat "hello", "world"
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["hello", "world"]
|
||||
|
||||
"""
|
||||
@spec concat(t, t) :: t
|
||||
def concat(doc1, doc2) when is_doc(doc1) and is_doc(doc2) do
|
||||
doc_cons(doc1, doc2)
|
||||
@spec concat(t, t) :: doc_cons
|
||||
def concat(x, y) when is_doc(x) and is_doc(y) do
|
||||
doc_cons(x, y)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Concatenates a list of documents returning a new document.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.concat(["a", "b", "c"])
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["a", "b", "c"]
|
||||
|
||||
Concatenates a list of documents.
|
||||
"""
|
||||
@spec concat([t]) :: t
|
||||
def concat(docs) when is_list(docs) do
|
||||
@spec concat([t]) :: doc_cons
|
||||
def concat(docs) do
|
||||
fold_doc(docs, &concat(&1, &2))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Colors a document if the `color_key` has a color in the options.
|
||||
"""
|
||||
@spec color(t, Inspect.Opts.color_key, Inspect.Opts.t) :: doc_color
|
||||
def color(doc, color_key, %Inspect.Opts{syntax_colors: syntax_colors}) when is_doc(doc) do
|
||||
if precolor = Keyword.get(syntax_colors, color_key) do
|
||||
postcolor = Keyword.get(syntax_colors, :reset, :reset)
|
||||
concat(doc_color(doc, precolor), doc_color(empty(), postcolor))
|
||||
else
|
||||
doc
|
||||
end
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Nests the given document at the given `level`.
|
||||
Nests document entity `x` positions deep.
|
||||
|
||||
Nesting will be appended to the line breaks.
|
||||
|
||||
@@ -333,88 +290,58 @@ defmodule Inspect.Algebra do
|
||||
|
||||
"""
|
||||
@spec nest(t, non_neg_integer) :: doc_nest
|
||||
def nest(doc, level)
|
||||
|
||||
def nest(doc, 0) when is_doc(doc) do
|
||||
doc
|
||||
def nest(x, 0) when is_doc(x) do
|
||||
x
|
||||
end
|
||||
|
||||
def nest(doc, level) when is_doc(doc) and is_integer(level) and level > 0 do
|
||||
doc_nest(doc, level)
|
||||
def nest(x, i) when is_doc(x) and is_integer(i) do
|
||||
doc_nest(x, i)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Returns a document entity representing a break based on the given
|
||||
`string`.
|
||||
Document entity representing a break.
|
||||
|
||||
This break can be rendered as a linebreak or as the given `string`,
|
||||
This break can be rendered as a linebreak or as spaces,
|
||||
depending on the `mode` of the chosen layout or the provided
|
||||
separator.
|
||||
|
||||
## Examples
|
||||
|
||||
Let's create a document by concatenating two strings with a break between
|
||||
them:
|
||||
Let's glue two docs together with a break and then render it:
|
||||
|
||||
iex> doc = Inspect.Algebra.concat(["a", Inspect.Algebra.break("\t"), "b"])
|
||||
iex> doc = Inspect.Algebra.glue("a", " ", "b")
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["a", "\t", "b"]
|
||||
["a", " ", "b"]
|
||||
|
||||
Notice the break was represented with the given string, because we didn't
|
||||
reach a line limit. Once we do, it is replaced by a newline:
|
||||
Notice the break was represented as is, because we haven't reached
|
||||
a line limit. Once we do, it is replaced by a newline:
|
||||
|
||||
iex> break = Inspect.Algebra.break("\t")
|
||||
iex> doc = Inspect.Algebra.concat([String.duplicate("a", 20), break, "b"])
|
||||
iex> doc = Inspect.Algebra.glue(String.duplicate("a", 20), " ", "b")
|
||||
iex> Inspect.Algebra.format(doc, 10)
|
||||
["aaaaaaaaaaaaaaaaaaaa", "\n", "b"]
|
||||
|
||||
"""
|
||||
@spec break(binary) :: doc_break
|
||||
def break(string) when is_binary(string), do: doc_break(string)
|
||||
def break(s) when is_binary(s), do: doc_break(s)
|
||||
|
||||
@doc """
|
||||
Returns a document entity representing the default break.
|
||||
|
||||
Same as calling `break/1` with the default break.
|
||||
"""
|
||||
@spec break() :: doc_break
|
||||
def break(), do: doc_break(@break)
|
||||
|
||||
@doc """
|
||||
Glues two documents together inserting the default break between them.
|
||||
|
||||
The break that is inserted between `left` and `right` is the one returned by
|
||||
`break/0`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.glue("hello", "world")
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["hello", " ", "world"]
|
||||
|
||||
Inserts a break between two docs. See `break/1` for more info.
|
||||
"""
|
||||
@spec glue(t, t) :: t
|
||||
def glue(doc1, doc2), do: concat(doc1, concat(break(), doc2))
|
||||
@spec glue(t, t) :: doc_cons
|
||||
def glue(x, y), do: concat(x, concat(break, y))
|
||||
|
||||
@doc """
|
||||
Glues two documents (`doc1` and `doc2`) together inserting the given
|
||||
break `break_string` between them.
|
||||
|
||||
For more information on how the break is inserted, see `break/1`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.glue("hello", "\t", "world")
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["hello", "\t", "world"]
|
||||
|
||||
Inserts a break, passed as the second argument, between two docs,
|
||||
the first and the third arguments.
|
||||
"""
|
||||
@spec glue(t, binary, t) :: t
|
||||
def glue(doc1, break_string, doc2) when is_binary(break_string),
|
||||
do: concat(doc1, concat(break(break_string), doc2))
|
||||
@spec glue(t, binary, t) :: doc_cons
|
||||
def glue(x, g, y) when is_binary(g), do: concat(x, concat(break(g), y))
|
||||
|
||||
@doc ~S"""
|
||||
Returns a group containing the specified document `doc`.
|
||||
Returns a group containing the specified document.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -441,91 +368,83 @@ defmodule Inspect.Algebra do
|
||||
|
||||
"""
|
||||
@spec group(t) :: doc_group
|
||||
def group(doc) when is_doc(doc) do
|
||||
doc_group(doc)
|
||||
def group(d) when is_doc(d) do
|
||||
doc_group(d)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Inserts a mandatory single space between two documents.
|
||||
Inserts a mandatory single space between two document entities.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.space("Hughes", "Wadler")
|
||||
iex> Inspect.Algebra.format(doc, 5)
|
||||
iex> doc = Inspect.Algebra.space "Hughes", "Wadler"
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["Hughes", " ", "Wadler"]
|
||||
|
||||
"""
|
||||
@spec space(t, t) :: t
|
||||
def space(doc1, doc2), do: concat(doc1, concat(" ", doc2))
|
||||
@spec space(t, t) :: doc_cons
|
||||
def space(x, y), do: concat(x, concat(" ", y))
|
||||
|
||||
@doc ~S"""
|
||||
Inserts a mandatory linebreak between two documents.
|
||||
Inserts a mandatory linebreak between two document entities.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.line("Hughes", "Wadler")
|
||||
iex> doc = Inspect.Algebra.line "Hughes", "Wadler"
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["Hughes", "\n", "Wadler"]
|
||||
|
||||
"""
|
||||
@spec line(t, t) :: t
|
||||
def line(doc1, doc2), do: concat(doc1, concat(:doc_line, doc2))
|
||||
@spec line(t, t) :: doc_cons
|
||||
def line(x, y), do: concat(x, concat(:doc_line, y))
|
||||
|
||||
@doc """
|
||||
Folds a list of documents into a document using the given folder function.
|
||||
|
||||
The list of documents is folded "from the right"; in that, this function is
|
||||
similar to `List.foldr/3`, except that it doesn't expect an initial
|
||||
accumulator and uses the last element of `docs` as the initial accumulator.
|
||||
Folds a list of document entities into a document entity
|
||||
using a function that is passed as the first argument.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> docs = ["A", "B", "C"]
|
||||
iex> docs = Inspect.Algebra.fold_doc(docs, fn(doc, acc) ->
|
||||
...> Inspect.Algebra.concat([doc, "!", acc])
|
||||
iex> doc = ["A", "B"]
|
||||
iex> doc = Inspect.Algebra.fold_doc(doc, fn(x, y) ->
|
||||
...> Inspect.Algebra.concat [x, "!", y]
|
||||
...> end)
|
||||
iex> Inspect.Algebra.format(docs, 80)
|
||||
["A", "!", "B", "!", "C"]
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["A", "!", "B"]
|
||||
|
||||
"""
|
||||
@spec fold_doc([t], ((t, t) -> t)) :: t
|
||||
def fold_doc(docs, folder_fun)
|
||||
|
||||
def fold_doc([], _folder_fun),
|
||||
do: empty()
|
||||
def fold_doc([doc], _folder_fun),
|
||||
do: doc
|
||||
def fold_doc([doc | docs], folder_fun) when is_function(folder_fun, 2),
|
||||
do: folder_fun.(doc, fold_doc(docs, folder_fun))
|
||||
def fold_doc(list, fun)
|
||||
def fold_doc([], _), do: empty
|
||||
def fold_doc([doc], _), do: doc
|
||||
def fold_doc([d|ds], fun), do: fun.(d, fold_doc(ds, fun))
|
||||
|
||||
# Elixir conveniences
|
||||
|
||||
@doc ~S"""
|
||||
Surrounds a document with characters.
|
||||
|
||||
Puts the given document `doc` between the `left` and `right` documents enclosing
|
||||
and nesting it. The document is marked as a group, to show the maximum as
|
||||
possible concisely together.
|
||||
Puts the document between left and right enclosing and nesting it.
|
||||
The document is marked as a group, to show the maximum as possible
|
||||
concisely together.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.surround("[", Inspect.Algebra.glue("a", "b"), "]")
|
||||
iex> doc = Inspect.Algebra.surround "[", Inspect.Algebra.glue("a", "b"), "]"
|
||||
iex> Inspect.Algebra.format(doc, 3)
|
||||
["[", "a", "\n ", "b", "]"]
|
||||
|
||||
"""
|
||||
@spec surround(t, t, t) :: t
|
||||
def surround(left, doc, right) when is_doc(left) and is_doc(doc) and is_doc(right) do
|
||||
group(concat(left, concat(nest(doc, @nesting), right)))
|
||||
@spec surround(binary, t, binary) :: t
|
||||
def surround(left, doc, right) do
|
||||
group concat left, concat(nest(doc, @nesting), right)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Maps and glues a collection of items.
|
||||
|
||||
It uses the given `left` and `right` documents as surrounding and the
|
||||
separator document `separator` to separate items in `docs`. A limit can be
|
||||
passed: when this limit is reached, this function stops gluing and outputs
|
||||
`"..."` instead.
|
||||
It uses the given left and right as surrounding and a separator for
|
||||
each item. A limit can be passed which, once reached, stops gluing
|
||||
and outputs "..." instead.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -543,11 +462,9 @@ defmodule Inspect.Algebra do
|
||||
...> %Inspect.Opts{limit: 3}, fn i, _opts -> to_string(i) end, "!")
|
||||
iex> Inspect.Algebra.format(doc, 20) |> IO.iodata_to_binary
|
||||
"[1! 2! 3! ...]"
|
||||
|
||||
"""
|
||||
@spec surround_many(t, [any], t, Inspect.Opts.t, (term, Inspect.Opts.t -> t), t) :: t
|
||||
def surround_many(left, docs, right, %Inspect.Opts{} = opts, fun, separator \\ @surround_separator)
|
||||
when is_doc(left) and is_list(docs) and is_doc(right) and is_function(fun, 2) and is_doc(separator) do
|
||||
@spec surround_many(binary, [any], binary, Inspect.Opts.t, (term, Inspect.Opts.t -> t), binary) :: t
|
||||
def surround_many(left, docs, right, opts, fun, separator \\ @surround_separator) do
|
||||
do_surround_many(left, docs, right, opts.limit, opts, fun, separator)
|
||||
end
|
||||
|
||||
@@ -571,14 +488,14 @@ defmodule Inspect.Algebra do
|
||||
fun.(h, %{opts | limit: limit})
|
||||
end
|
||||
|
||||
defp do_surround_many([h | t], limit, opts, fun, sep) when is_list(t) do
|
||||
defp do_surround_many([h|t], limit, opts, fun, sep) when is_list(t) do
|
||||
limit = decrement(limit)
|
||||
h = fun.(h, %{opts | limit: limit})
|
||||
t = do_surround_many(t, limit, opts, fun, sep)
|
||||
do_join(h, t, sep)
|
||||
end
|
||||
|
||||
defp do_surround_many([h | t], limit, opts, fun, _sep) do
|
||||
defp do_surround_many([h|t], limit, opts, fun, _sep) do
|
||||
limit = decrement(limit)
|
||||
h = fun.(h, %{opts | limit: limit})
|
||||
t = fun.(t, %{opts | limit: limit})
|
||||
@@ -593,25 +510,16 @@ defmodule Inspect.Algebra do
|
||||
defp decrement(:infinity), do: :infinity
|
||||
defp decrement(counter), do: counter - 1
|
||||
|
||||
@doc ~S"""
|
||||
Formats a given document for a given width.
|
||||
@doc """
|
||||
The formatting function.
|
||||
|
||||
Takes the maximum width and a document to print as its arguments
|
||||
and returns an IO data representation of the best layout for the
|
||||
document to fit in the given width.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.glue("hello", " ", "world")
|
||||
iex> Inspect.Algebra.format(doc, 30) |> IO.iodata_to_binary()
|
||||
"hello world"
|
||||
iex> Inspect.Algebra.format(doc, 10) |> IO.iodata_to_binary()
|
||||
"hello\nworld"
|
||||
|
||||
"""
|
||||
@spec format(t, non_neg_integer | :infinity) :: iodata
|
||||
def format(doc, width) when is_doc(doc) and (width == :infinity or width >= 0) do
|
||||
format(width, 0, [{0, default_mode(width), doc_group(doc)}])
|
||||
def format(d, w) do
|
||||
format(w, 0, [{0, default_mode(w), doc_group(d)}])
|
||||
end
|
||||
|
||||
defp default_mode(:infinity), do: :flat
|
||||
@@ -626,11 +534,10 @@ defmodule Inspect.Algebra do
|
||||
defp fits?(_, [{_, _, :doc_line} | _]), do: true
|
||||
defp fits?(w, [{_, _, :doc_nil} | t]), do: fits?(w, t)
|
||||
defp fits?(w, [{i, m, doc_cons(x, y)} | t]), do: fits?(w, [{i, m, x} | [{i, m, y} | t]])
|
||||
defp fits?(w, [{i, m, doc_color(x, _)} | t]), do: fits?(w, [{i, m, x} | t])
|
||||
defp fits?(w, [{i, m, doc_nest(x, j)} | t]), do: fits?(w, [{i + j, m, x} | t])
|
||||
defp fits?(w, [{i, _, doc_group(x)} | t]), do: fits?(w, [{i, :flat, x} | t])
|
||||
defp fits?(w, [{_, _, s} | t]) when is_binary(s), do: fits?((w - byte_size(s)), t)
|
||||
defp fits?(w, [{_, :flat, doc_break(s)} | t]), do: fits?((w - byte_size(s)), t)
|
||||
defp fits?(w, [{_, _, s} | t]) when is_binary(s), do: fits?((w - byte_size s), t)
|
||||
defp fits?(w, [{_, :flat, doc_break(s)} | t]), do: fits?((w - byte_size s), t)
|
||||
defp fits?(_, [{_, :break, doc_break(_)} | _]), do: true
|
||||
|
||||
@spec format(integer | :infinity, integer, [{integer, mode, t}]) :: [binary]
|
||||
@@ -640,9 +547,8 @@ defmodule Inspect.Algebra do
|
||||
defp format(w, k, [{i, m, doc_cons(x, y)} | t]), do: format(w, k, [{i, m, x} | [{i, m, y} | t]])
|
||||
defp format(w, k, [{i, m, doc_nest(x, j)} | t]), do: format(w, k, [{i + j, m, x} | t])
|
||||
defp format(w, k, [{i, m, doc_group(x)} | t]), do: format(w, k, [{i, m, x} | t])
|
||||
defp format(w, k, [{i, m, doc_color(x, c)} | t]), do: [ansi(c) | format(w, k, [{i, m, x} | t])]
|
||||
defp format(w, k, [{_, _, s} | t]) when is_binary(s), do: [s | format(w, (k + byte_size(s)), t)]
|
||||
defp format(w, k, [{_, :flat, doc_break(s)} | t]), do: [s | format(w, (k + byte_size(s)), t)]
|
||||
defp format(w, k, [{_, _, s} | t]) when is_binary(s), do: [s | format(w, (k + byte_size s), t)]
|
||||
defp format(w, k, [{_, :flat, doc_break(s)} | t]), do: [s | format(w, (k + byte_size s), t)]
|
||||
defp format(w, k, [{i, :break, doc_break(s)} | t]) do
|
||||
k = k + byte_size(s)
|
||||
|
||||
@@ -653,10 +559,6 @@ defmodule Inspect.Algebra do
|
||||
end
|
||||
end
|
||||
|
||||
defp ansi(color) do
|
||||
IO.ANSI.format_fragment(color, true)
|
||||
end
|
||||
|
||||
defp indent(0), do: @newline
|
||||
defp indent(i), do: @newline <> :binary.copy(" ", i)
|
||||
end
|
||||
|
||||
+71
-215
@@ -6,37 +6,28 @@ defmodule Integer do
|
||||
import Bitwise
|
||||
|
||||
@doc """
|
||||
Determines if `integer` is odd.
|
||||
Determines if an integer is odd.
|
||||
|
||||
Returns `true` if the given `integer` is an odd number,
|
||||
otherwise it returns `false`.
|
||||
Returns `true` if `n` is an odd number, otherwise `false`.
|
||||
|
||||
Allowed in guard clauses.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.is_odd(5)
|
||||
iex> Integer.is_odd(3)
|
||||
true
|
||||
|
||||
iex> Integer.is_odd(6)
|
||||
iex> Integer.is_odd(4)
|
||||
false
|
||||
|
||||
iex> Integer.is_odd(-5)
|
||||
true
|
||||
|
||||
iex> Integer.is_odd(0)
|
||||
false
|
||||
|
||||
"""
|
||||
defmacro is_odd(integer) do
|
||||
quote do: (unquote(integer) &&& 1) == 1
|
||||
defmacro is_odd(n) do
|
||||
quote do: (unquote(n) &&& 1) == 1
|
||||
end
|
||||
|
||||
@doc """
|
||||
Determines if an `integer` is even.
|
||||
Determines if an integer is even.
|
||||
|
||||
Returns `true` if the given `integer` is an even number,
|
||||
otherwise it returns `false`.
|
||||
Returns `true` if `n` is an even number, otherwise `false`.
|
||||
|
||||
Allowed in guard clauses.
|
||||
|
||||
@@ -47,164 +38,72 @@ defmodule Integer do
|
||||
|
||||
iex> Integer.is_even(5)
|
||||
false
|
||||
|
||||
iex> Integer.is_even(-10)
|
||||
true
|
||||
|
||||
iex> Integer.is_even(0)
|
||||
true
|
||||
|
||||
"""
|
||||
defmacro is_even(integer) do
|
||||
quote do: (unquote(integer) &&& 1) == 0
|
||||
defmacro is_even(n) do
|
||||
quote do: (unquote(n) &&& 1) == 0
|
||||
end
|
||||
|
||||
@doc """
|
||||
Computes the modulo remainder of an integer division.
|
||||
Returns the ordered digits for the given non-negative integer.
|
||||
|
||||
`Integer.mod/2` uses floored division, which means that
|
||||
the result will always have the sign of the `divisor`.
|
||||
|
||||
Raises an `ArithmeticError` exception if one of the arguments is not an
|
||||
integer, or when the `divisor` is `0`.
|
||||
An optional base value may be provided representing the radix for the returned
|
||||
digits.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.mod(5, 2)
|
||||
1
|
||||
iex> Integer.mod(6, -4)
|
||||
-2
|
||||
iex> Integer.digits(101)
|
||||
[1, 0, 1]
|
||||
|
||||
iex> Integer.digits(58127, 2)
|
||||
[1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]
|
||||
|
||||
"""
|
||||
@spec mod(integer, neg_integer | pos_integer) :: integer
|
||||
def mod(dividend, divisor) do
|
||||
remainder = rem(dividend, divisor)
|
||||
if remainder * divisor < 0 do
|
||||
remainder + divisor
|
||||
else
|
||||
remainder
|
||||
end
|
||||
@spec digits(non_neg_integer, pos_integer) :: [non_neg_integer]
|
||||
def digits(n, base \\ 10) when is_integer(n) and n >= 0
|
||||
and is_integer(base) and base >= 2 do
|
||||
do_digits(n, base, [])
|
||||
end
|
||||
|
||||
defp do_digits(0, _base, []), do: [0]
|
||||
defp do_digits(0, _base, acc), do: acc
|
||||
defp do_digits(n, base, acc) do
|
||||
do_digits div(n, base), base, [rem(n, base) | acc]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Performs a floored integer division.
|
||||
Returns the integer represented by the ordered digits.
|
||||
|
||||
Raises an `ArithmeticError` exception if one of the arguments is not an
|
||||
integer, or when the `divisor` is `0`.
|
||||
|
||||
`Integer.floor_div/2` performs *floored* integer division. This means that
|
||||
the result is always rounded towards negative infinity.
|
||||
|
||||
If you want to perform truncated integer division (rounding towards zero),
|
||||
use `Kernel.div/2` instead.
|
||||
An optional base value may be provided representing the radix for the digits.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.floor_div(5, 2)
|
||||
2
|
||||
iex> Integer.floor_div(6, -4)
|
||||
-2
|
||||
iex> Integer.floor_div(-99, 2)
|
||||
-50
|
||||
|
||||
"""
|
||||
@spec floor_div(integer, neg_integer | pos_integer) :: integer
|
||||
def floor_div(dividend, divisor) do
|
||||
if (dividend * divisor < 0) and rem(dividend, divisor) != 0 do
|
||||
div(dividend, divisor) - 1
|
||||
else
|
||||
div(dividend, divisor)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the ordered digits for the given `integer`.
|
||||
|
||||
An optional `base` value may be provided representing the radix for the returned
|
||||
digits. This one must be an integer >= 2.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.digits(123)
|
||||
[1, 2, 3]
|
||||
|
||||
iex> Integer.digits(170, 2)
|
||||
[1, 0, 1, 0, 1, 0, 1, 0]
|
||||
|
||||
iex> Integer.digits(-170, 2)
|
||||
[-1, 0, -1, 0, -1, 0, -1, 0]
|
||||
|
||||
"""
|
||||
@spec digits(integer, pos_integer) :: [integer, ...]
|
||||
def digits(integer, base \\ 10)
|
||||
when is_integer(integer) and is_integer(base) and base >= 2 do
|
||||
do_digits(integer, base, [])
|
||||
end
|
||||
|
||||
defp do_digits(digit, base, []) when abs(digit) < base,
|
||||
do: [digit]
|
||||
defp do_digits(digit, base, []) when digit == -base,
|
||||
do: [-1, 0]
|
||||
defp do_digits(base, base, []),
|
||||
do: [1, 0]
|
||||
defp do_digits(0, _base, acc),
|
||||
do: acc
|
||||
defp do_digits(integer, base, acc),
|
||||
do: do_digits(div(integer, base), base, [rem(integer, base) | acc])
|
||||
|
||||
@doc """
|
||||
Returns the integer represented by the ordered `digits`.
|
||||
|
||||
An optional `base` value may be provided representing the radix for the `digits`.
|
||||
This one can be an integer >= 2.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.undigits([1, 2, 3])
|
||||
123
|
||||
iex> Integer.undigits([1, 0, 1])
|
||||
101
|
||||
|
||||
iex> Integer.undigits([1, 4], 16)
|
||||
20
|
||||
|
||||
iex> Integer.undigits([])
|
||||
0
|
||||
|
||||
"""
|
||||
@spec undigits([integer], integer) :: integer
|
||||
def undigits(digits, base \\ 10) when is_list(digits) and is_integer(base) and base >= 2 do
|
||||
def undigits(digits, base \\ 10) when is_integer(base) do
|
||||
do_undigits(digits, base, 0)
|
||||
end
|
||||
|
||||
defp do_undigits([], _base, 0),
|
||||
do: 0
|
||||
defp do_undigits([digit], base, 0) when is_integer(digit) and digit < base,
|
||||
do: digit
|
||||
defp do_undigits([1, 0], base, 0),
|
||||
do: base
|
||||
defp do_undigits([0 | tail], base, 0),
|
||||
do: do_undigits(tail, base, 0)
|
||||
|
||||
defp do_undigits([], _base, acc),
|
||||
do: acc
|
||||
defp do_undigits([digit | _], base, _) when is_integer(digit) and digit >= base,
|
||||
do: raise ArgumentError, "invalid digit #{digit} in base #{base}"
|
||||
defp do_undigits([digit | tail], base, acc) when is_integer(digit),
|
||||
do: do_undigits(tail, base, acc * base + digit)
|
||||
defp do_undigits([], _base, acc), do: acc
|
||||
defp do_undigits([digit | tail], base, acc) do
|
||||
do_undigits(tail, base, acc * base + digit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses a text representation of an integer.
|
||||
Converts a binary from a text representation of an integer
|
||||
in an optional base `base` to the corresponding integer.
|
||||
|
||||
An optional `base` to the corresponding integer can be provided.
|
||||
If `base` is not given, 10 will be used.
|
||||
If the base `base` is not given, base 10 will be used.
|
||||
|
||||
If successful, returns a tuple in the form of `{integer, remainder_of_binary}`.
|
||||
If successful, returns a tuple of the form `{integer, remainder_of_binary}`.
|
||||
Otherwise `:error`.
|
||||
|
||||
Raises an error if `base` is less than 2 or more than 36.
|
||||
|
||||
If you want to convert a string-formatted integer directly to a integer,
|
||||
`String.to_integer/1` or `String.to_integer/2` can be used instead.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.parse("34")
|
||||
@@ -235,21 +134,18 @@ defmodule Integer do
|
||||
@spec parse(binary, 2..36) :: {integer, binary} | :error | no_return
|
||||
def parse(binary, base \\ 10)
|
||||
|
||||
def parse("", base) when base in 2..36,
|
||||
do: :error
|
||||
|
||||
def parse(binary, base) when is_binary(binary) and base in 2..36 do
|
||||
def parse(binary, base) when is_integer(base) and base in 2..36 do
|
||||
parse_in_base(binary, base)
|
||||
end
|
||||
|
||||
def parse(binary, base) when is_binary(binary) do
|
||||
def parse(_, base) do
|
||||
raise ArgumentError, "invalid base #{base}"
|
||||
end
|
||||
|
||||
defp parse_in_base("-" <> bin, base) do
|
||||
case do_parse(bin, base) do
|
||||
{number, remainder} -> {-number, remainder}
|
||||
:error -> :error
|
||||
{number, remainder} -> {-number, remainder}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -257,27 +153,25 @@ defmodule Integer do
|
||||
do_parse(bin, base)
|
||||
end
|
||||
|
||||
defp parse_in_base(binary, base) when is_binary(binary) do
|
||||
do_parse(binary, base)
|
||||
defp parse_in_base(bin, base) when is_binary(bin) do
|
||||
do_parse(bin, base)
|
||||
end
|
||||
|
||||
defp do_parse(<<char, rest::binary>>, base) do
|
||||
if valid_digit_in_base?(char, base) do
|
||||
do_parse(rest, base, parse_digit(char))
|
||||
do_parse(rest, base, parse_digit(char, base))
|
||||
else
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
defp do_parse(_, _) do
|
||||
:error
|
||||
end
|
||||
defp do_parse(_, _), do: :error
|
||||
|
||||
defp do_parse(<<char, rest::binary>> = bin, base, acc) do
|
||||
defp do_parse(<<char, rest::binary>>, base, acc) do
|
||||
if valid_digit_in_base?(char, base) do
|
||||
do_parse(rest, base, base * acc + parse_digit(char))
|
||||
do_parse(rest, base, base * acc + parse_digit(char, base))
|
||||
else
|
||||
{acc, bin}
|
||||
{acc, <<char, rest::binary>>}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -285,7 +179,7 @@ defmodule Integer do
|
||||
{acc, bitstring}
|
||||
end
|
||||
|
||||
defp parse_digit(char) do
|
||||
defp parse_digit(char, _) do
|
||||
cond do
|
||||
char in ?0..?9 -> char - ?0
|
||||
char in ?A..?Z -> char - ?A + 10
|
||||
@@ -303,7 +197,7 @@ defmodule Integer do
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `integer`.
|
||||
of `some_integer`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -312,26 +206,15 @@ defmodule Integer do
|
||||
iex> Integer.to_string(123)
|
||||
"123"
|
||||
|
||||
iex> Integer.to_string(+456)
|
||||
"456"
|
||||
|
||||
iex> Integer.to_string(-789)
|
||||
"-789"
|
||||
|
||||
iex> Integer.to_string(0123)
|
||||
"123"
|
||||
|
||||
"""
|
||||
@spec to_string(integer) :: String.t
|
||||
def to_string(integer) do
|
||||
:erlang.integer_to_binary(integer)
|
||||
def to_string(some_integer) do
|
||||
:erlang.integer_to_binary(some_integer)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `integer` in the given `base`.
|
||||
|
||||
`base` can be an integer between 2 and 36.
|
||||
of `some_integer` in base `base`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -340,69 +223,42 @@ defmodule Integer do
|
||||
iex> Integer.to_string(100, 16)
|
||||
"64"
|
||||
|
||||
iex> Integer.to_string(-100, 16)
|
||||
"-64"
|
||||
|
||||
iex> Integer.to_string(882681651, 36)
|
||||
"ELIXIR"
|
||||
|
||||
"""
|
||||
@spec to_string(integer, 2..36) :: String.t
|
||||
def to_string(integer, base) do
|
||||
:erlang.integer_to_binary(integer, base)
|
||||
def to_string(some_integer, base) do
|
||||
:erlang.integer_to_binary(some_integer, base)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the text representation of the given `integer`.
|
||||
Returns a char list which corresponds to the text representation of the given integer.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.to_charlist(123)
|
||||
'123'
|
||||
|
||||
iex> Integer.to_charlist(+456)
|
||||
'456'
|
||||
|
||||
iex> Integer.to_charlist(-789)
|
||||
'-789'
|
||||
|
||||
iex> Integer.to_charlist(0123)
|
||||
'123'
|
||||
iex> Integer.to_char_list(7)
|
||||
'7'
|
||||
|
||||
"""
|
||||
@spec to_charlist(integer) :: charlist
|
||||
def to_charlist(integer) do
|
||||
:erlang.integer_to_list(integer)
|
||||
@spec to_char_list(integer) :: char_list
|
||||
def to_char_list(number) do
|
||||
:erlang.integer_to_list(number)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the text representation of `integer` in the given `base`.
|
||||
|
||||
`base` can be an integer between 2 and 36.
|
||||
Returns a char list which corresponds to the text representation of the
|
||||
given integer in the given base.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.to_charlist(100, 16)
|
||||
'64'
|
||||
|
||||
iex> Integer.to_charlist(-100, 16)
|
||||
'-64'
|
||||
|
||||
iex> Integer.to_charlist(882681651, 36)
|
||||
'ELIXIR'
|
||||
iex> Integer.to_char_list(1023, 16)
|
||||
'3FF'
|
||||
|
||||
"""
|
||||
@spec to_charlist(integer, 2..36) :: charlist
|
||||
def to_charlist(integer, base) do
|
||||
:erlang.integer_to_list(integer, base)
|
||||
@spec to_char_list(integer, 2..36) :: char_list
|
||||
def to_char_list(number, base) do
|
||||
:erlang.integer_to_list(number, base)
|
||||
end
|
||||
|
||||
# TODO: Deprecate by v1.5
|
||||
@doc false
|
||||
@spec to_char_list(integer) :: charlist
|
||||
def to_char_list(integer), do: Integer.to_charlist(integer)
|
||||
end
|
||||
|
||||
+98
-189
@@ -1,25 +1,25 @@
|
||||
defmodule IO do
|
||||
@moduledoc """
|
||||
Functions handling input/output (IO).
|
||||
Functions handling IO.
|
||||
|
||||
Many functions in this module expect an IO device as an argument.
|
||||
An IO device must be a PID or an atom representing a process.
|
||||
An IO device must be a pid or an atom representing a process.
|
||||
For convenience, Elixir provides `:stdio` and `:stderr` as
|
||||
shortcuts to Erlang's `:standard_io` and `:standard_error`.
|
||||
|
||||
The majority of the functions expect chardata, i.e. strings or
|
||||
The majority of the functions expect char data, i.e. strings or
|
||||
lists of characters and strings. In case another type is given,
|
||||
functions will convert to string via the `String.Chars` protocol
|
||||
(as shown in typespecs).
|
||||
|
||||
The functions starting with `bin` expect iodata as an argument,
|
||||
The functions starting with `bin*` expect iodata as an argument,
|
||||
i.e. binaries or lists of bytes and binaries.
|
||||
|
||||
## IO devices
|
||||
|
||||
An IO device may be an atom or a PID. In case it is an atom,
|
||||
An IO device may be an atom or a pid. In case it is an atom,
|
||||
the atom must be the name of a registered process. In addition,
|
||||
Elixir provides two shortcuts:
|
||||
Elixir provides two shorcuts:
|
||||
|
||||
* `:stdio` - a shortcut for `:standard_io`, which maps to
|
||||
the current `Process.group_leader/0` in Erlang
|
||||
@@ -38,6 +38,8 @@ defmodule IO do
|
||||
@type nodata :: {:error, term} | :eof
|
||||
@type chardata() :: :unicode.chardata()
|
||||
|
||||
import :erlang, only: [group_leader: 0]
|
||||
|
||||
defmacrop is_iodata(data) do
|
||||
quote do
|
||||
is_list(unquote(data)) or is_binary(unquote(data))
|
||||
@@ -45,15 +47,12 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads from the IO `device`.
|
||||
|
||||
The `device` is iterated by the given number of characters or line by line if
|
||||
`:line` is given.
|
||||
Alternatively, if `:all` is given, then whole `device` is returned.
|
||||
Reads `count` characters from the IO device, a whole
|
||||
`:line` or the whole device with `:all`.
|
||||
|
||||
It returns:
|
||||
|
||||
* `data` - the output characters
|
||||
* `data` - the input characters
|
||||
|
||||
* `:eof` - end of file was encountered
|
||||
|
||||
@@ -65,7 +64,7 @@ defmodule IO do
|
||||
empty string in case the device has reached EOF.
|
||||
"""
|
||||
@spec read(device, :all | :line | non_neg_integer) :: chardata | nodata
|
||||
def read(device \\ :stdio, line_or_chars)
|
||||
def read(device \\ group_leader, chars_or_line)
|
||||
|
||||
def read(device, :all) do
|
||||
do_read_all(map_dev(device), "")
|
||||
@@ -75,7 +74,7 @@ defmodule IO do
|
||||
:io.get_line(map_dev(device), '')
|
||||
end
|
||||
|
||||
def read(device, count) when is_integer(count) and count >= 0 do
|
||||
def read(device, count) when count >= 0 do
|
||||
:io.get_chars(map_dev(device), '', count)
|
||||
end
|
||||
|
||||
@@ -88,15 +87,12 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads from the IO `device`. The operation is Unicode unsafe.
|
||||
|
||||
The `device` is iterated by the given number of bytes or line by line if
|
||||
`:line` is given.
|
||||
Alternatively, if `:all` is given, then whole `device` is returned.
|
||||
Reads `count` characters from the IO device, a whole
|
||||
`:line` or the whole device with `:all`.
|
||||
|
||||
It returns:
|
||||
|
||||
* `data` - the output bytes
|
||||
* `data` - the input characters
|
||||
|
||||
* `:eof` - end of file was encountered
|
||||
|
||||
@@ -107,11 +103,11 @@ defmodule IO do
|
||||
If `:all` is given, `:eof` is never returned, but an
|
||||
empty string in case the device has reached EOF.
|
||||
|
||||
Note: do not use this function on IO devices in Unicode mode
|
||||
Note: do not use this function on IO devices in unicode mode
|
||||
as it will return the wrong result.
|
||||
"""
|
||||
@spec binread(device, :all | :line | non_neg_integer) :: iodata | nodata
|
||||
def binread(device \\ :stdio, line_or_chars)
|
||||
def binread(device \\ group_leader, chars_or_line)
|
||||
|
||||
def binread(device, :all) do
|
||||
do_binread_all(map_dev(device), "")
|
||||
@@ -124,7 +120,7 @@ defmodule IO do
|
||||
end
|
||||
end
|
||||
|
||||
def binread(device, count) when is_integer(count) and count >= 0 do
|
||||
def binread(device, count) when count >= 0 do
|
||||
case :file.read(map_dev(device), count) do
|
||||
{:ok, data} -> data
|
||||
other -> other
|
||||
@@ -141,189 +137,87 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes `item` to the given `device`.
|
||||
Writes the given argument to the given device.
|
||||
|
||||
By default the `device` is the standard output.
|
||||
By default the device is the standard output.
|
||||
It returns `:ok` if it succeeds.
|
||||
|
||||
## Examples
|
||||
|
||||
IO.write "sample"
|
||||
#=> sample
|
||||
#=> "sample"
|
||||
|
||||
IO.write :stderr, "error"
|
||||
#=> error
|
||||
#=> "error"
|
||||
|
||||
"""
|
||||
@spec write(device, chardata | String.Chars.t) :: :ok
|
||||
def write(device \\ :stdio, item) do
|
||||
def write(device \\ group_leader(), item) do
|
||||
:io.put_chars map_dev(device), to_chardata(item)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes `item` as a binary to the given `device`.
|
||||
No Unicode conversion happens.
|
||||
The operation is Unicode unsafe.
|
||||
Writes the given argument to the given device
|
||||
as a binary, no unicode conversion happens.
|
||||
|
||||
Check `write/2` for more information.
|
||||
|
||||
Note: do not use this function on IO devices in Unicode mode
|
||||
Note: do not use this function on IO devices in unicode mode
|
||||
as it will return the wrong result.
|
||||
"""
|
||||
@spec binwrite(device, iodata) :: :ok | {:error, term}
|
||||
def binwrite(device \\ :stdio, item) when is_iodata(item) do
|
||||
def binwrite(device \\ group_leader(), item) when is_iodata(item) do
|
||||
:file.write map_dev(device), item
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes `item` to the given `device`, similar to `write/2`,
|
||||
but adds a newline at the end.
|
||||
Writes the argument to the device, similar to `write/2`,
|
||||
but adds a newline at the end. The argument is expected
|
||||
to be a chardata.
|
||||
"""
|
||||
@spec puts(device, chardata | String.Chars.t) :: :ok
|
||||
def puts(device \\ :stdio, item) do
|
||||
:io.put_chars map_dev(device), [to_chardata(item), ?\n]
|
||||
def puts(device \\ group_leader(), item) do
|
||||
erl_dev = map_dev(device)
|
||||
:io.put_chars erl_dev, [to_chardata(item), ?\n]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes a `message` to stderr, along with the given `stacktrace`.
|
||||
|
||||
This function also notifies the compiler a warning was printed
|
||||
(in case --warnings-as-errors was enabled). It returns `:ok`
|
||||
if it succeeds.
|
||||
|
||||
An empty list can be passed to avoid stacktrace printing.
|
||||
|
||||
## Examples
|
||||
|
||||
stacktrace = [{MyApp, :main, 1, [file: 'my_app.ex', line: 4]}]
|
||||
IO.warn "variable bar is unused", stacktrace
|
||||
#=> warning: variable bar is unused
|
||||
#=> my_app.ex:4: MyApp.main/1
|
||||
|
||||
"""
|
||||
@spec warn(chardata | String.Chars.t, Exception.stacktrace) :: :ok
|
||||
def warn(message, []) do
|
||||
:elixir_errors.warn([to_chardata(message), ?\n])
|
||||
end
|
||||
def warn(message, stacktrace) when is_list(stacktrace) do
|
||||
formatted = Enum.map_join(stacktrace, "\n ", &Exception.format_stacktrace_entry(&1))
|
||||
:elixir_errors.warn([to_chardata(message), ?\n, " ", formatted, ?\n])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes a `message` to stderr, along with the current stacktrace.
|
||||
|
||||
It returns `:ok` if it succeeds.
|
||||
|
||||
## Examples
|
||||
|
||||
IO.warn "variable bar is unused"
|
||||
#=> warning: variable bar is unused
|
||||
#=> (iex) evaluator.ex:108: IEx.Evaluator.eval/4
|
||||
|
||||
"""
|
||||
@spec warn(chardata | String.Chars.t) :: :ok
|
||||
def warn(message) do
|
||||
{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
|
||||
warn(message, Enum.drop(stacktrace, 2))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Inspects and writes the given `item` to the device.
|
||||
|
||||
It's important to note that it returns the given `item` unchanged.
|
||||
This makes it possible to "spy" on values by inserting an
|
||||
`IO.inspect/2` call almost anywhere in your code, for example,
|
||||
in the middle of a pipeline.
|
||||
Inspects and writes the given argument to the device.
|
||||
|
||||
It enables pretty printing by default with width of
|
||||
80 characters. The width can be changed by explicitly
|
||||
passing the `:width` option.
|
||||
|
||||
The output can be decorated with a label, by providing the `:label`
|
||||
option to easily distinguish it from other `IO.inspect/2` calls.
|
||||
The label will be printed before the inspected `item`.
|
||||
|
||||
See `Inspect.Opts` for a full list of remaining formatting options.
|
||||
See `Inspect.Opts` for a full list of options.
|
||||
|
||||
## Examples
|
||||
|
||||
IO.inspect <<0, 1, 2>>, width: 40
|
||||
|
||||
Prints:
|
||||
|
||||
<<0, 1, 2>>
|
||||
|
||||
We can use the `:label` option to decorate the output:
|
||||
|
||||
IO.inspect 1..100, label: "a wonderful range"
|
||||
|
||||
Prints:
|
||||
|
||||
a wonderful range: 1..100
|
||||
|
||||
The `:label` option is especially useful with pipelines:
|
||||
|
||||
[1, 2, 3]
|
||||
|> IO.inspect(label: "before")
|
||||
|> Enum.map(&(&1 * 2))
|
||||
|> IO.inspect(label: "after")
|
||||
|> Enum.sum
|
||||
|
||||
Prints:
|
||||
|
||||
before: [1, 2, 3]
|
||||
after: [2, 4, 6]
|
||||
IO.inspect Process.list, width: 40
|
||||
|
||||
"""
|
||||
@spec inspect(item, Keyword.t) :: item when item: var
|
||||
def inspect(item, opts \\ []) do
|
||||
inspect :stdio, item, opts
|
||||
inspect group_leader(), item, opts
|
||||
end
|
||||
|
||||
@doc """
|
||||
Inspects `item` according to the given options using the IO `device`.
|
||||
Inspects the item with options using the given device.
|
||||
|
||||
See `inspect/2` for a full list of options.
|
||||
See `Inspect.Opts` for a full list of options.
|
||||
"""
|
||||
@spec inspect(device, item, Keyword.t) :: item when item: var
|
||||
def inspect(device, item, opts) when is_list(opts) do
|
||||
label = if (label = opts[:label]), do: [to_chardata(label), ": "], else: []
|
||||
opts = struct(Inspect.Opts, opts)
|
||||
chardata = Inspect.Algebra.format(Inspect.Algebra.to_doc(item, opts), opts.width)
|
||||
puts device, [label, chardata]
|
||||
opts = struct(Inspect.Opts, opts)
|
||||
iodata = Inspect.Algebra.format(Inspect.Algebra.to_doc(item, opts), opts.width)
|
||||
puts device, iodata
|
||||
item
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a number of bytes from IO device `:stdio`.
|
||||
|
||||
If `:stdio` is a Unicode device, `count` implies
|
||||
the number of Unicode codepoints to be retrieved.
|
||||
Gets a number of bytes from the io device. If the
|
||||
io device is a unicode device, `count` implies
|
||||
the number of unicode codepoints to be retrieved.
|
||||
Otherwise, `count` is the number of raw bytes to be retrieved.
|
||||
|
||||
See `IO.getn/3` for a description of return values.
|
||||
|
||||
"""
|
||||
@spec getn(chardata | String.Chars.t, pos_integer) :: chardata | nodata
|
||||
@spec getn(device, chardata | String.Chars.t) :: chardata | nodata
|
||||
def getn(prompt, count \\ 1)
|
||||
|
||||
def getn(prompt, count) when is_integer(count) and count > 0 do
|
||||
getn(:stdio, prompt, count)
|
||||
end
|
||||
|
||||
def getn(device, prompt) when not is_integer(prompt) do
|
||||
getn(device, prompt, 1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a number of bytes from the IO `device`.
|
||||
|
||||
If the IO `device` is a Unicode device, `count` implies
|
||||
the number of Unicode codepoints to be retrieved.
|
||||
Otherwise, `count` is the number of raw bytes to be retrieved.
|
||||
|
||||
It returns:
|
||||
|
||||
* `data` - the input characters
|
||||
@@ -333,15 +227,32 @@ defmodule IO do
|
||||
* `{:error, reason}` - other (rare) error condition;
|
||||
for instance, `{:error, :estale}` if reading from an
|
||||
NFS volume
|
||||
"""
|
||||
@spec getn(chardata | String.Chars.t, pos_integer) :: chardata | nodata
|
||||
@spec getn(device, chardata | String.Chars.t) :: chardata | nodata
|
||||
def getn(prompt, count \\ 1)
|
||||
|
||||
def getn(prompt, count) when is_integer(count) do
|
||||
getn(group_leader, prompt, count)
|
||||
end
|
||||
|
||||
def getn(device, prompt) do
|
||||
getn(device, prompt, 1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a number of bytes from the io device. If the
|
||||
io device is a unicode device, `count` implies
|
||||
the number of unicode codepoints to be retrieved.
|
||||
Otherwise, `count` is the number of raw bytes to be retrieved.
|
||||
"""
|
||||
@spec getn(device, chardata | String.Chars.t, pos_integer) :: chardata | nodata
|
||||
def getn(device, prompt, count) when is_integer(count) and count > 0 do
|
||||
def getn(device, prompt, count) do
|
||||
:io.get_chars(map_dev(device), to_chardata(prompt), count)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Reads a line from the IO `device`.
|
||||
@doc """
|
||||
Reads a line from the IO device.
|
||||
|
||||
It returns:
|
||||
|
||||
@@ -358,25 +269,24 @@ defmodule IO do
|
||||
|
||||
To display "What is your name?" as a prompt and await user input:
|
||||
|
||||
IO.gets "What is your name?\n"
|
||||
|
||||
IO.gets "What is your name?"
|
||||
"""
|
||||
@spec gets(device, chardata | String.Chars.t) :: chardata | nodata
|
||||
def gets(device \\ :stdio, prompt) do
|
||||
def gets(device \\ group_leader(), prompt) do
|
||||
:io.get_line(map_dev(device), to_chardata(prompt))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the IO `device` into an `IO.Stream`.
|
||||
Converts the io device into a `IO.Stream`.
|
||||
|
||||
An `IO.Stream` implements both `Enumerable` and
|
||||
`Collectable`, allowing it to be used for both read
|
||||
and write.
|
||||
|
||||
The `device` is iterated by the given number of characters or line by line if
|
||||
`:line` is given.
|
||||
The device is iterated line by line if `:line` is given or
|
||||
by a given number of codepoints.
|
||||
|
||||
This reads from the IO as UTF-8. Check out
|
||||
This reads the IO as utf-8. Check out
|
||||
`IO.binstream/2` to handle the IO as a raw binary.
|
||||
|
||||
Note that an IO stream has side effects and every time
|
||||
@@ -391,34 +301,28 @@ defmodule IO do
|
||||
|
||||
"""
|
||||
@spec stream(device, :line | pos_integer) :: Enumerable.t
|
||||
def stream(device, line_or_codepoints)
|
||||
when line_or_codepoints == :line
|
||||
when is_integer(line_or_codepoints) and line_or_codepoints > 0 do
|
||||
def stream(device, line_or_codepoints) do
|
||||
IO.Stream.__build__(map_dev(device), false, line_or_codepoints)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the IO `device` into an `IO.Stream`. The operation is Unicode unsafe.
|
||||
Converts the IO device into a `IO.Stream`.
|
||||
|
||||
An `IO.Stream` implements both `Enumerable` and
|
||||
`Collectable`, allowing it to be used for both read
|
||||
and write.
|
||||
|
||||
The `device` is iterated by the given number of bytes or line by line if
|
||||
`:line` is given.
|
||||
This reads from the IO device as a raw binary.
|
||||
The device is iterated line by line or by a number of bytes.
|
||||
This reads the IO device as a raw binary.
|
||||
|
||||
Note that an IO stream has side effects and every time
|
||||
you go over the stream you may get different results.
|
||||
|
||||
Finally, do not use this function on IO devices in Unicode
|
||||
Finally, do not use this function on IO devices in unicode
|
||||
mode as it will return the wrong result.
|
||||
|
||||
"""
|
||||
@spec binstream(device, :line | pos_integer) :: Enumerable.t
|
||||
def binstream(device, line_or_bytes)
|
||||
when line_or_bytes == :line
|
||||
when is_integer(line_or_bytes) and line_or_bytes > 0 do
|
||||
def binstream(device, line_or_bytes) do
|
||||
IO.Stream.__build__(map_dev(device), true, line_or_bytes)
|
||||
end
|
||||
|
||||
@@ -426,8 +330,8 @@ defmodule IO do
|
||||
Converts chardata (a list of integers representing codepoints,
|
||||
lists and strings) into a string.
|
||||
|
||||
In case the conversion fails, it raises an `UnicodeConversionError`.
|
||||
If a string is given, it returns the string itself.
|
||||
In case the conversion fails, it raises a `UnicodeConversionError`.
|
||||
If a string is given, returns the string itself.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -437,9 +341,6 @@ defmodule IO do
|
||||
iex> IO.chardata_to_string([0x0061, "bc"])
|
||||
"abc"
|
||||
|
||||
iex> IO.chardata_to_string("string")
|
||||
"string"
|
||||
|
||||
"""
|
||||
@spec chardata_to_string(chardata) :: String.t | no_return
|
||||
def chardata_to_string(string) when is_binary(string) do
|
||||
@@ -447,17 +348,25 @@ defmodule IO do
|
||||
end
|
||||
|
||||
def chardata_to_string(list) when is_list(list) do
|
||||
List.to_string(list)
|
||||
case :unicode.characters_to_binary(list) do
|
||||
result when is_binary(result) ->
|
||||
result
|
||||
|
||||
{:error, encoded, rest} ->
|
||||
raise UnicodeConversionError, encoded: encoded, rest: rest, kind: :invalid
|
||||
|
||||
{:incomplete, encoded, rest} ->
|
||||
raise UnicodeConversionError, encoded: encoded, rest: rest, kind: :incomplete
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts iodata (a list of integers representing bytes, lists
|
||||
and binaries) into a binary.
|
||||
The operation is Unicode unsafe.
|
||||
|
||||
Notice that this function treats lists of integers as raw bytes
|
||||
and does not perform any kind of encoding conversion. If you want
|
||||
to convert from a charlist to a string (UTF-8 encoded), please
|
||||
to convert from a char list to a string (UTF-8 encoded), please
|
||||
use `chardata_to_string/1` instead.
|
||||
|
||||
If this function receives a binary, the same binary is returned.
|
||||
@@ -469,7 +378,7 @@ defmodule IO do
|
||||
iex> bin1 = <<1, 2, 3>>
|
||||
iex> bin2 = <<4, 5>>
|
||||
iex> bin3 = <<6>>
|
||||
iex> IO.iodata_to_binary([bin1, 1, [2, 3, bin2], 4 | bin3])
|
||||
iex> IO.iodata_to_binary([bin1, 1, [2, 3, bin2], 4|bin3])
|
||||
<<1, 2, 3, 1, 2, 3, 4, 5, 4, 6>>
|
||||
|
||||
iex> bin = <<1, 2, 3>>
|
||||
@@ -489,7 +398,7 @@ defmodule IO do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> IO.iodata_length([1, 2 | <<3, 4>>])
|
||||
iex> IO.iodata_length([1, 2|<<3, 4>>])
|
||||
4
|
||||
|
||||
"""
|
||||
@@ -499,8 +408,8 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc false
|
||||
def each_stream(device, line_or_codepoints) do
|
||||
case read(device, line_or_codepoints) do
|
||||
def each_stream(device, what) do
|
||||
case read(device, what) do
|
||||
:eof ->
|
||||
{:halt, device}
|
||||
{:error, reason} ->
|
||||
@@ -511,8 +420,8 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc false
|
||||
def each_binstream(device, line_or_chars) do
|
||||
case binread(device, line_or_chars) do
|
||||
def each_binstream(device, what) do
|
||||
case binread(device, what) do
|
||||
:eof ->
|
||||
{:halt, device}
|
||||
{:error, reason} ->
|
||||
@@ -524,7 +433,7 @@ defmodule IO do
|
||||
|
||||
@compile {:inline, map_dev: 1, to_chardata: 1}
|
||||
|
||||
# Map the Elixir names for standard IO and error to Erlang names
|
||||
# Map the Elixir names for standard io and error to Erlang names
|
||||
defp map_dev(:stdio), do: :standard_io
|
||||
defp map_dev(:stderr), do: :standard_error
|
||||
defp map_dev(other) when is_atom(other) or is_pid(other) or is_tuple(other), do: other
|
||||
|
||||
+45
-57
@@ -25,9 +25,9 @@ defmodule IO.ANSI do
|
||||
|
||||
import IO.ANSI.Sequence
|
||||
|
||||
@typep ansicode :: atom
|
||||
@typep ansilist :: maybe_improper_list(char | ansicode | binary | ansilist, binary | ansicode | [])
|
||||
@type ansidata :: ansilist | ansicode | binary
|
||||
@typep ansicode :: atom()
|
||||
@typep ansilist :: maybe_improper_list(char() | ansicode() | binary() | ansilist(), binary() | ansicode() | [])
|
||||
@type ansidata :: ansilist() | ansicode() | binary()
|
||||
|
||||
@doc """
|
||||
Checks if ANSI coloring is supported and enabled on this machine.
|
||||
@@ -42,7 +42,7 @@ defmodule IO.ANSI do
|
||||
Application.get_env(:elixir, :ansi_enabled, false)
|
||||
end
|
||||
|
||||
@doc "Sets foreground color."
|
||||
@doc "Sets foreground color"
|
||||
@spec color(0..255) :: String.t
|
||||
def color(code) when code in 0..255, do: "\e[38;5;#{code}m"
|
||||
|
||||
@@ -56,7 +56,7 @@ defmodule IO.ANSI do
|
||||
color(16 + (36 * r) + (6 * g) + b)
|
||||
end
|
||||
|
||||
@doc "Sets background color."
|
||||
@doc "Sets background color"
|
||||
@spec color_background(0..255) :: String.t
|
||||
def color_background(code) when code in 0..255, do: "\e[48;5;#{code}m"
|
||||
|
||||
@@ -70,109 +70,97 @@ defmodule IO.ANSI do
|
||||
color_background(16 + (36 * r) + (6 * g) + b)
|
||||
end
|
||||
|
||||
@doc "Resets all attributes."
|
||||
@doc "Resets all attributes"
|
||||
defsequence :reset, 0
|
||||
|
||||
@doc "Bright (increased intensity) or bold."
|
||||
@doc "Bright (increased intensity) or Bold"
|
||||
defsequence :bright, 1
|
||||
|
||||
@doc "Faint (decreased intensity). Not widely supported."
|
||||
@doc "Faint (decreased intensity), not widely supported"
|
||||
defsequence :faint, 2
|
||||
|
||||
@doc "Italic: on. Not widely supported. Sometimes treated as inverse."
|
||||
@doc "Italic: on. Not widely supported. Sometimes treated as inverse"
|
||||
defsequence :italic, 3
|
||||
|
||||
@doc "Underline: single."
|
||||
@doc "Underline: Single"
|
||||
defsequence :underline, 4
|
||||
|
||||
@doc "Blink: slow. Less than 150 per minute."
|
||||
@doc "Blink: Slow. Less than 150 per minute"
|
||||
defsequence :blink_slow, 5
|
||||
|
||||
@doc "Blink: rapid. MS-DOS ANSI.SYS; 150 per minute or more; not widely supported."
|
||||
@doc "Blink: Rapid. MS-DOS ANSI.SYS; 150 per minute or more; not widely supported"
|
||||
defsequence :blink_rapid, 6
|
||||
|
||||
@doc "Image: negative. Swap foreground and background."
|
||||
@doc "Image: Negative. Swap foreground and background"
|
||||
defsequence :inverse, 7
|
||||
|
||||
@doc "Image: negative. Swap foreground and background."
|
||||
@doc "Image: Negative. Swap foreground and background"
|
||||
defsequence :reverse, 7
|
||||
|
||||
@doc "Conceal. Not widely supported."
|
||||
@doc "Conceal. Not widely supported"
|
||||
defsequence :conceal, 8
|
||||
|
||||
@doc "Crossed-out. Characters legible, but marked for deletion. Not widely supported."
|
||||
@doc "Crossed-out. Characters legible, but marked for deletion. Not widely supported"
|
||||
defsequence :crossed_out, 9
|
||||
|
||||
@doc "Sets primary (default) font."
|
||||
@doc "Sets primary (default) font"
|
||||
defsequence :primary_font, 10
|
||||
|
||||
for font_n <- [1, 2, 3, 4, 5, 6, 7, 8, 9] do
|
||||
@doc "Sets alternative font #{font_n}."
|
||||
@doc "Sets alternative font #{font_n}"
|
||||
defsequence :"font_#{font_n}", font_n + 10
|
||||
end
|
||||
|
||||
@doc "Normal color or intensity."
|
||||
@doc "Normal color or intensity"
|
||||
defsequence :normal, 22
|
||||
|
||||
@doc "Not italic."
|
||||
@doc "Not italic"
|
||||
defsequence :not_italic, 23
|
||||
|
||||
@doc "Underline: none."
|
||||
@doc "Underline: None"
|
||||
defsequence :no_underline, 24
|
||||
|
||||
@doc "Blink: off."
|
||||
@doc "Blink: off"
|
||||
defsequence :blink_off, 25
|
||||
|
||||
@doc "Image: positive. Normal foreground and background."
|
||||
defsequence :inverse_off, 27
|
||||
|
||||
@doc "Image: positive. Normal foreground and background."
|
||||
defsequence :reverse_off, 27
|
||||
|
||||
colors = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
|
||||
|
||||
for {color, code} <- Enum.with_index(colors) do
|
||||
@doc "Sets foreground color to #{color}."
|
||||
@doc "Sets foreground color to #{color}"
|
||||
defsequence color, code + 30
|
||||
|
||||
@doc "Sets foreground color to light #{color}."
|
||||
defsequence :"light_#{color}", code + 90
|
||||
|
||||
@doc "Sets background color to #{color}."
|
||||
@doc "Sets background color to #{color}"
|
||||
defsequence :"#{color}_background", code + 40
|
||||
|
||||
@doc "Sets background color to light #{color}."
|
||||
defsequence :"light_#{color}_background", code + 100
|
||||
end
|
||||
|
||||
@doc "Default text color."
|
||||
@doc "Default text color"
|
||||
defsequence :default_color, 39
|
||||
|
||||
@doc "Default background color."
|
||||
@doc "Default background color"
|
||||
defsequence :default_background, 49
|
||||
|
||||
@doc "Framed."
|
||||
@doc "Framed"
|
||||
defsequence :framed, 51
|
||||
|
||||
@doc "Encircled."
|
||||
@doc "Encircled"
|
||||
defsequence :encircled, 52
|
||||
|
||||
@doc "Overlined."
|
||||
@doc "Overlined"
|
||||
defsequence :overlined, 53
|
||||
|
||||
@doc "Not framed or encircled."
|
||||
@doc "Not framed or encircled"
|
||||
defsequence :not_framed_encircled, 54
|
||||
|
||||
@doc "Not overlined."
|
||||
@doc "Not overlined"
|
||||
defsequence :not_overlined, 55
|
||||
|
||||
@doc "Sends cursor home."
|
||||
@doc "Sends cursor home"
|
||||
defsequence :home, "", "H"
|
||||
|
||||
@doc "Clears screen."
|
||||
@doc "Clears screen"
|
||||
defsequence :clear, "2", "J"
|
||||
|
||||
@doc "Clears line."
|
||||
@doc "Clears line"
|
||||
defsequence :clear_line, "2", "K"
|
||||
|
||||
defp format_sequence(other) do
|
||||
@@ -198,8 +186,8 @@ defmodule IO.ANSI do
|
||||
[[[[[[], "Hello, "] | "\e[31m"] | "\e[1m"], "world!"] | "\e[0m"]
|
||||
|
||||
"""
|
||||
def format(chardata, emit? \\ enabled?()) when is_boolean(emit?) do
|
||||
do_format(chardata, [], [], emit?, :maybe)
|
||||
def format(chardata, emit \\ enabled?) when is_boolean(emit) do
|
||||
do_format(chardata, [], [], emit, :maybe)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@@ -218,12 +206,12 @@ defmodule IO.ANSI do
|
||||
[[[[[[] | "\e[1m"], 87], 111], 114], 100]
|
||||
|
||||
"""
|
||||
def format_fragment(chardata, emit? \\ enabled?()) when is_boolean(emit?) do
|
||||
do_format(chardata, [], [], emit?, false)
|
||||
def format_fragment(chardata, emit \\ enabled?) when is_boolean(emit) do
|
||||
do_format(chardata, [], [], emit, false)
|
||||
end
|
||||
|
||||
defp do_format([term | rest], rem, acc, emit?, append_reset) do
|
||||
do_format(term, [rest | rem], acc, emit?, append_reset)
|
||||
defp do_format([term | rest], rem, acc, emit, append_reset) do
|
||||
do_format(term, [rest | rem], acc, emit, append_reset)
|
||||
end
|
||||
|
||||
defp do_format(term, rem, acc, true, append_reset) when is_atom(term) do
|
||||
@@ -234,19 +222,19 @@ defmodule IO.ANSI do
|
||||
do_format([], rem, acc, false, append_reset)
|
||||
end
|
||||
|
||||
defp do_format(term, rem, acc, emit?, append_reset) when not is_list(term) do
|
||||
do_format([], rem, [acc, term], emit?, append_reset)
|
||||
defp do_format(term, rem, acc, emit, append_reset) when not is_list(term) do
|
||||
do_format([], rem, [acc | [term]], emit, append_reset)
|
||||
end
|
||||
|
||||
defp do_format([], [next | rest], acc, emit?, append_reset) do
|
||||
do_format(next, rest, acc, emit?, append_reset)
|
||||
defp do_format([], [next | rest], acc, emit, append_reset) do
|
||||
do_format(next, rest, acc, emit, append_reset)
|
||||
end
|
||||
|
||||
defp do_format([], [], acc, true, true) do
|
||||
[acc | IO.ANSI.reset]
|
||||
end
|
||||
|
||||
defp do_format([], [], acc, _emit?, _append_reset) do
|
||||
defp do_format([], [], acc, _emit, _append_reset) do
|
||||
acc
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,11 +10,11 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
* `:enabled` - toggles coloring on and off (true)
|
||||
* `:doc_bold` - bold text (bright)
|
||||
* `:doc_code` - code blocks (cyan)
|
||||
* `:doc_headings` - h1, h2, h3, h4, h5, h6 headings (yellow)
|
||||
* `:doc_code` - code blocks (cyan, bright)
|
||||
* `:doc_headings` - h1 and h2 headings (yellow, bright)
|
||||
* `:doc_inline_code` - inline code (cyan)
|
||||
* `:doc_table_heading` - style for table headings
|
||||
* `:doc_title` - top level heading (reverse, yellow)
|
||||
* `:doc_title` - top level heading (reverse, yellow, bright)
|
||||
* `:doc_underline` - underlined text (underline)
|
||||
* `:width` - the width to format the text (80)
|
||||
|
||||
@@ -24,7 +24,7 @@ defmodule IO.ANSI.Docs do
|
||||
def default_options do
|
||||
[enabled: true,
|
||||
doc_bold: [:bright],
|
||||
doc_code: [:cyan],
|
||||
doc_code: [:cyan, :bright],
|
||||
doc_headings: [:yellow],
|
||||
doc_inline_code: [:cyan],
|
||||
doc_table_heading: [:reverse],
|
||||
@@ -40,12 +40,12 @@ defmodule IO.ANSI.Docs do
|
||||
"""
|
||||
def print_heading(heading, options \\ []) do
|
||||
IO.puts IO.ANSI.reset
|
||||
options = Keyword.merge(default_options(), options)
|
||||
options = Keyword.merge(default_options, options)
|
||||
width = options[:width]
|
||||
padding = div(width + String.length(heading), 2)
|
||||
heading = heading |> String.pad_leading(padding) |> String.pad_trailing(width)
|
||||
heading = heading |> String.rjust(padding) |> String.ljust(width)
|
||||
write(:doc_title, heading, options)
|
||||
newline_after_block()
|
||||
newline_after_block
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -55,10 +55,10 @@ defmodule IO.ANSI.Docs do
|
||||
defined in `default_options/1`.
|
||||
"""
|
||||
def print(doc, options \\ []) do
|
||||
options = Keyword.merge(default_options(), options)
|
||||
options = Keyword.merge(default_options, options)
|
||||
doc
|
||||
|> String.split(["\r\n", "\n"], trim: false)
|
||||
|> Enum.map(&String.trim_trailing/1)
|
||||
|> Enum.map(&String.rstrip/1)
|
||||
|> process([], "", options)
|
||||
end
|
||||
|
||||
@@ -66,23 +66,22 @@ defmodule IO.ANSI.Docs do
|
||||
write_text(text, indent, options)
|
||||
end
|
||||
|
||||
defp process(["# " <> _ = heading | rest], text, indent, options) do
|
||||
write_heading(heading, rest, text, indent, options)
|
||||
defp process(["# " <> heading | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
write_h1(String.strip(heading), options)
|
||||
process(rest, [], "", options)
|
||||
end
|
||||
defp process(["## " <> _ = heading | rest], text, indent, options) do
|
||||
write_heading(heading, rest, text, indent, options)
|
||||
|
||||
defp process(["## " <> heading | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
write_h2(String.strip(heading), options)
|
||||
process(rest, [], "", options)
|
||||
end
|
||||
defp process(["### " <> _ = heading | rest], text, indent, options) do
|
||||
write_heading(heading, rest, text, indent, options)
|
||||
end
|
||||
defp process(["#### " <> _ = heading | rest], text, indent, options) do
|
||||
write_heading(heading, rest, text, indent, options)
|
||||
end
|
||||
defp process(["##### " <> _ = heading | rest], text, indent, options) do
|
||||
write_heading(heading, rest, text, indent, options)
|
||||
end
|
||||
defp process(["###### " <> _ = heading | rest], text, indent, options) do
|
||||
write_heading(heading, rest, text, indent, options)
|
||||
|
||||
defp process(["### " <> heading | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
write_h3(String.strip(heading), indent, options)
|
||||
process(rest, [], "", options)
|
||||
end
|
||||
|
||||
defp process(["" | rest], text, indent, options) do
|
||||
@@ -119,11 +118,19 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
## Headings
|
||||
|
||||
defp write_heading(heading, rest, text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
defp write_h1(heading, options) do
|
||||
write_h2(String.upcase(heading), options)
|
||||
end
|
||||
|
||||
defp write_h2(heading, options) do
|
||||
write(:doc_headings, heading, options)
|
||||
newline_after_block()
|
||||
process(rest, [], "", options)
|
||||
newline_after_block
|
||||
end
|
||||
|
||||
defp write_h3(heading, indent, options) do
|
||||
IO.write(indent)
|
||||
write(:doc_headings, heading, options)
|
||||
newline_after_block
|
||||
end
|
||||
|
||||
## Lists
|
||||
@@ -146,7 +153,7 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp process_list(entry, line, rest, count, indent, options) do
|
||||
# The first list always win some extra padding
|
||||
entry = if indent == "", do: " " <> entry, else: entry
|
||||
if indent == "", do: entry = " " <> entry
|
||||
new_indent = indent <> String.duplicate(" ", String.length(entry))
|
||||
|
||||
{contents, rest, done} = process_list_next(rest, count, byte_size(new_indent), [])
|
||||
@@ -190,7 +197,7 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp write_text(text, indent, options) do
|
||||
case Enum.reverse(text) do
|
||||
[:no_wrap | rest] -> write_text(rest, indent, options, true)
|
||||
[:no_wrap|rest] -> write_text(rest, indent, options, true)
|
||||
rest -> write_text(rest, indent, options, false)
|
||||
end
|
||||
end
|
||||
@@ -222,7 +229,7 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
|
||||
defp process_code([" " <> line | rest], code, indent, options) do
|
||||
process_code(rest, [line | code], indent, options)
|
||||
process_code(rest, [line|code], indent, options)
|
||||
end
|
||||
|
||||
defp process_code(rest, code, indent, options) do
|
||||
@@ -243,13 +250,13 @@ defmodule IO.ANSI.Docs do
|
||||
if line === delimiter do
|
||||
process_code(rest, code, indent, options)
|
||||
else
|
||||
process_fenced_code(rest, [line | code], indent, options, delimiter)
|
||||
process_fenced_code(rest, [line|code], indent, options, delimiter)
|
||||
end
|
||||
end
|
||||
|
||||
defp write_code(code, indent, options) do
|
||||
write(:doc_code, "#{indent} #{Enum.join(Enum.reverse(code), "\n#{indent} ")}", options)
|
||||
newline_after_block()
|
||||
write(:doc_code, "#{indent}┃ #{Enum.join(Enum.reverse(code), "\n#{indent}┃ ")}", options)
|
||||
newline_after_block
|
||||
end
|
||||
|
||||
## Tables
|
||||
@@ -257,7 +264,7 @@ defmodule IO.ANSI.Docs do
|
||||
defp process_table(lines, indent, options) do
|
||||
{table, rest} = Enum.split_while(lines, &table_line?/1)
|
||||
table_lines(table, options)
|
||||
newline_after_block()
|
||||
newline_after_block
|
||||
process(rest, [], indent, options)
|
||||
end
|
||||
|
||||
@@ -278,8 +285,8 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp split_into_columns(line, options) do
|
||||
line
|
||||
|> String.trim("|")
|
||||
|> String.trim()
|
||||
|> String.strip(?|)
|
||||
|> String.strip()
|
||||
|> String.split(~r/\s\|\s/)
|
||||
|> Enum.map(&render_column(&1, options))
|
||||
end
|
||||
@@ -372,13 +379,13 @@ defmodule IO.ANSI.Docs do
|
||||
write_with_wrap(rest, available, indent, false)
|
||||
end
|
||||
|
||||
defp take_words([word | words], available, acc) do
|
||||
defp take_words([word|words], available, acc) do
|
||||
available = available - length_without_escape(word, 0)
|
||||
|
||||
cond do
|
||||
# It fits, take one for space and continue decreasing
|
||||
available > 0 ->
|
||||
take_words(words, available - 1, [word | acc])
|
||||
take_words(words, available - 1, [word|acc])
|
||||
|
||||
# No space but we got no words
|
||||
acc == [] ->
|
||||
@@ -386,7 +393,7 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
# Otherwise
|
||||
true ->
|
||||
{Enum.reverse(acc), [word | words]}
|
||||
{Enum.reverse(acc), [word|words]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -459,80 +466,80 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp handle_inline(<<delimiter, ?*, ?*, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and delimiter in @delimiters do
|
||||
handle_inline(rest, ?d, ["**"], [delimiter, Enum.reverse(buffer) | acc], options)
|
||||
handle_inline(rest, ?d, ["**"], [delimiter, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<delimiter, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and delimiter in @delimiters and mark in @single do
|
||||
handle_inline(rest, mark, [<<mark>>], [delimiter, Enum.reverse(buffer) | acc], options)
|
||||
handle_inline(rest, mark, [<<mark>>], [delimiter, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?`, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" do
|
||||
handle_inline(rest, ?`, ["`"], [Enum.reverse(buffer) | acc], options)
|
||||
handle_inline(rest, ?`, ["`"], [Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
# Clauses for handling escape
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, ?*, ?*, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" do
|
||||
handle_inline(rest, ?d, ["**"], [?\\, Enum.reverse(buffer) | acc], options)
|
||||
handle_inline(rest, ?d, ["**"], [?\\, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and mark in @single do
|
||||
handle_inline(rest, mark, [<<mark>>], [?\\, Enum.reverse(buffer) | acc], options)
|
||||
handle_inline(rest, mark, [<<mark>>], [?\\, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, rest::binary>>, limit, buffer, acc, options) do
|
||||
handle_inline(rest, limit, [?\\ | buffer], acc, options)
|
||||
handle_inline(rest, limit, [?\\|buffer], acc, options)
|
||||
end
|
||||
|
||||
# An escape is not valid inside `
|
||||
defp handle_inline(<<?\\, mark, rest::binary>>, limit, buffer, acc, options)
|
||||
when not(mark == limit and mark == ?`) do
|
||||
handle_inline(rest, limit, [mark | buffer], acc, options)
|
||||
handle_inline(rest, limit, [mark|buffer], acc, options)
|
||||
end
|
||||
|
||||
# Inline end
|
||||
|
||||
defp handle_inline(<<?*, ?*, delimiter, rest::binary>>, ?d, buffer, acc, options)
|
||||
when delimiter in @delimiters do
|
||||
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, delimiter, rest::binary>>, mark, buffer, acc, options)
|
||||
when delimiter in @delimiters and mark in @single do
|
||||
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?*, ?*, rest::binary>>, ?d, buffer, acc, options)
|
||||
when rest == "" do
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, rest::binary>>, mark, buffer, acc, options)
|
||||
when rest == "" and mark in @single do
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?`, rest::binary>>, ?`, buffer, acc, options) do
|
||||
handle_inline(rest, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
handle_inline(rest, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
# Catch all
|
||||
|
||||
defp handle_inline(<<char, rest::binary>>, mark, buffer, acc, options) do
|
||||
handle_inline(rest, mark, [char | buffer], acc, options)
|
||||
handle_inline(rest, mark, [char|buffer], acc, options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<>>, _mark, buffer, acc, _options) do
|
||||
IO.iodata_to_binary Enum.reverse([Enum.reverse(buffer) | acc])
|
||||
IO.iodata_to_binary Enum.reverse([Enum.reverse(buffer)|acc])
|
||||
end
|
||||
|
||||
defp inline_buffer(buffer, options) do
|
||||
[h | t] = Enum.reverse([IO.ANSI.reset | buffer])
|
||||
[color_for(h, options) | t]
|
||||
[h|t] = Enum.reverse([IO.ANSI.reset|buffer])
|
||||
[color_for(h, options)|t]
|
||||
end
|
||||
|
||||
defp color_for(mark, colors) do
|
||||
|
||||
@@ -18,9 +18,6 @@ defmodule IO.Stream do
|
||||
* `raw` - a boolean indicating if bin functions should be used
|
||||
* `line_or_bytes` - if reading should read lines or a given amount of bytes
|
||||
|
||||
It is worth noting that an IO stream has side effects and every time you go
|
||||
over the stream you may get different results.
|
||||
|
||||
"""
|
||||
|
||||
defstruct device: nil, raw: true, line_or_bytes: :line
|
||||
|
||||
+493
-975
File diff suppressed because it is too large
Load Diff
@@ -76,22 +76,22 @@ defmodule Kernel.CLI do
|
||||
fun.(elem(res, 1))
|
||||
catch
|
||||
:exit, {:shutdown, int} when is_integer(int) ->
|
||||
send parent, {self(), {:shutdown, int}}
|
||||
send parent, {self, {:shutdown, int}}
|
||||
exit({:shutdown, int})
|
||||
:exit, reason
|
||||
when reason == :normal
|
||||
when reason == :shutdown
|
||||
when tuple_size(reason) == 2 and elem(reason, 0) == :shutdown ->
|
||||
send parent, {self(), {:shutdown, 0}}
|
||||
send parent, {self, {:shutdown, 0}}
|
||||
exit(reason)
|
||||
kind, reason ->
|
||||
stack = System.stacktrace
|
||||
print_error(kind, reason, stack)
|
||||
send parent, {self(), {:shutdown, 1}}
|
||||
send parent, {self, {:shutdown, 1}}
|
||||
exit(to_exit(kind, reason, stack))
|
||||
else
|
||||
_ ->
|
||||
send parent, {self(), res}
|
||||
send parent, {self, res}
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -111,7 +111,7 @@ defmodule Kernel.CLI do
|
||||
|
||||
defp shared_option?(list, config, callback) do
|
||||
case parse_shared(list, config) do
|
||||
{[h | hs], _} when h == hd(list) ->
|
||||
{[h|hs], _} when h == hd(list) ->
|
||||
new_config = %{config | errors: ["#{h} : Unknown option" | config.errors]}
|
||||
callback.(hs, new_config)
|
||||
{new_list, new_config} ->
|
||||
@@ -125,18 +125,18 @@ defmodule Kernel.CLI do
|
||||
|
||||
@elixir_internals [:elixir, :elixir_exp, :elixir_compiler, :elixir_module, :elixir_clauses,
|
||||
:elixir_translator, :elixir_expand, :elixir_lexical, :elixir_exp_clauses,
|
||||
:elixir_def, :elixir_map]
|
||||
:elixir_def]
|
||||
|
||||
defp prune_stacktrace([{mod, _, _, _} | t]) when mod in @elixir_internals do
|
||||
defp prune_stacktrace([{mod, _, _, _}|t]) when mod in @elixir_internals do
|
||||
prune_stacktrace(t)
|
||||
end
|
||||
|
||||
defp prune_stacktrace([{__MODULE__, :wrapper, 1, _} | _]) do
|
||||
defp prune_stacktrace([{__MODULE__, :wrapper, 1, _}|_]) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp prune_stacktrace([h | t]) do
|
||||
[h | prune_stacktrace(t)]
|
||||
defp prune_stacktrace([h|t]) do
|
||||
[h|prune_stacktrace(t)]
|
||||
end
|
||||
|
||||
defp prune_stacktrace([]) do
|
||||
@@ -145,54 +145,59 @@ defmodule Kernel.CLI do
|
||||
|
||||
# Parse shared options
|
||||
|
||||
defp parse_shared([opt | _t], _config) when opt in ["-v", "--version"] do
|
||||
defp parse_shared([opt|_t], _config) when opt in ["-v", "--version"] do
|
||||
if function_exported?(IEx, :started?, 0) and IEx.started? do
|
||||
IO.puts "IEx " <> System.build_info[:build]
|
||||
IO.puts "IEx #{System.version}"
|
||||
else
|
||||
IO.puts :erlang.system_info(:system_version)
|
||||
IO.puts "Elixir " <> System.build_info[:build]
|
||||
{:ok, v} = Version.parse(System.version)
|
||||
|
||||
case v.pre do
|
||||
[] -> IO.puts "Elixir #{System.version}"
|
||||
_ -> IO.puts "Elixir #{System.version} (#{System.build_info().revision})"
|
||||
end
|
||||
end
|
||||
|
||||
System.halt 0
|
||||
end
|
||||
|
||||
defp parse_shared(["-pa", h | t], config) do
|
||||
defp parse_shared(["-pa", h|t], config) do
|
||||
paths = expand_code_path(h)
|
||||
Enum.each(paths, &:code.add_patha/1)
|
||||
parse_shared t, %{config | pa: config.pa ++ paths}
|
||||
end
|
||||
|
||||
defp parse_shared(["-pz", h | t], config) do
|
||||
defp parse_shared(["-pz", h|t], config) do
|
||||
paths = expand_code_path(h)
|
||||
Enum.each(paths, &:code.add_pathz/1)
|
||||
parse_shared t, %{config | pz: config.pz ++ paths}
|
||||
end
|
||||
|
||||
defp parse_shared(["--app", h | t], config) do
|
||||
defp parse_shared(["--app", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:app, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared(["--no-halt" | t], config) do
|
||||
defp parse_shared(["--no-halt"|t], config) do
|
||||
parse_shared t, %{config | halt: false}
|
||||
end
|
||||
|
||||
defp parse_shared(["-e", h | t], config) do
|
||||
defp parse_shared(["-e", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:eval, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared(["-r", h | t], config) do
|
||||
defp parse_shared(["-r", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:require, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared(["-pr", h | t], config) do
|
||||
defp parse_shared(["-pr", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:parallel_require, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared([erl, _ | t], config) when erl in ["--erl", "--sname", "--name", "--cookie", "--logger-otp-reports", "--logger-sasl-reports"] do
|
||||
defp parse_shared([erl, _|t], config) when erl in ["--erl", "--sname", "--name", "--cookie"] do
|
||||
parse_shared t, config
|
||||
end
|
||||
|
||||
defp parse_shared([erl | t], config) when erl in ["--detached", "--hidden", "--werl"] do
|
||||
defp parse_shared([erl|t], config) when erl in ["--detached", "--hidden", "--werl"] do
|
||||
parse_shared t, config
|
||||
end
|
||||
|
||||
@@ -203,30 +208,30 @@ defmodule Kernel.CLI do
|
||||
defp expand_code_path(path) do
|
||||
path = Path.expand(path)
|
||||
case Path.wildcard(path) do
|
||||
[] -> [to_charlist(path)]
|
||||
list -> Enum.map(list, &to_charlist/1)
|
||||
[] -> [to_char_list(path)]
|
||||
list -> Enum.map(list, &to_char_list/1)
|
||||
end
|
||||
end
|
||||
|
||||
# Process init options
|
||||
|
||||
defp parse_argv(["--" | t], config) do
|
||||
defp parse_argv(["--"|t], config) do
|
||||
{config, t}
|
||||
end
|
||||
|
||||
defp parse_argv(["+elixirc" | t], config) do
|
||||
defp parse_argv(["+elixirc"|t], config) do
|
||||
parse_compiler t, config
|
||||
end
|
||||
|
||||
defp parse_argv(["+iex" | t], config) do
|
||||
defp parse_argv(["+iex"|t], config) do
|
||||
parse_iex t, config
|
||||
end
|
||||
|
||||
defp parse_argv(["-S", h | t], config) do
|
||||
defp parse_argv(["-S", h|t], config) do
|
||||
{%{config | commands: [{:script, h} | config.commands]}, t}
|
||||
end
|
||||
|
||||
defp parse_argv([h | t] = list, config) do
|
||||
defp parse_argv([h|t] = list, config) do
|
||||
case h do
|
||||
"-" <> _ ->
|
||||
shared_option? list, config, &parse_argv(&1, &2)
|
||||
@@ -245,35 +250,35 @@ defmodule Kernel.CLI do
|
||||
|
||||
# Parse compiler options
|
||||
|
||||
defp parse_compiler(["--" | t], config) do
|
||||
defp parse_compiler(["--"|t], config) do
|
||||
{config, t}
|
||||
end
|
||||
|
||||
defp parse_compiler(["-o", h | t], config) do
|
||||
defp parse_compiler(["-o", h|t], config) do
|
||||
parse_compiler t, %{config | output: h}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--no-docs" | t], config) do
|
||||
defp parse_compiler(["--no-docs"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:docs, false} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--no-debug-info" | t], config) do
|
||||
defp parse_compiler(["--no-debug-info"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:debug_info, false} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--ignore-module-conflict" | t], config) do
|
||||
defp parse_compiler(["--ignore-module-conflict"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:ignore_module_conflict, true} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--warnings-as-errors" | t], config) do
|
||||
defp parse_compiler(["--warnings-as-errors"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:warnings_as_errors, true} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--verbose" | t], config) do
|
||||
defp parse_compiler(["--verbose"|t], config) do
|
||||
parse_compiler t, %{config | verbose_compile: true}
|
||||
end
|
||||
|
||||
defp parse_compiler([h | t] = list, config) do
|
||||
defp parse_compiler([h|t] = list, config) do
|
||||
case h do
|
||||
"-" <> _ ->
|
||||
shared_option? list, config, &parse_compiler(&1, &2)
|
||||
@@ -284,30 +289,30 @@ defmodule Kernel.CLI do
|
||||
end
|
||||
|
||||
defp parse_compiler([], config) do
|
||||
{%{config | commands: [{:compile, config.compile} | config.commands]}, []}
|
||||
{%{config | commands: [{:compile, config.compile}|config.commands]}, []}
|
||||
end
|
||||
|
||||
# Parse IEx options
|
||||
# Parse iex options
|
||||
|
||||
defp parse_iex(["--" | t], config) do
|
||||
defp parse_iex(["--"|t], config) do
|
||||
{config, t}
|
||||
end
|
||||
|
||||
# This clause is here so that Kernel.CLI does not
|
||||
# error out with "unknown option"
|
||||
defp parse_iex(["--dot-iex", _ | t], config) do
|
||||
defp parse_iex(["--dot-iex", _|t], config) do
|
||||
parse_iex t, config
|
||||
end
|
||||
|
||||
defp parse_iex([opt, _ | t], config) when opt in ["--remsh"] do
|
||||
defp parse_iex([opt, _|t], config) when opt in ["--remsh"] do
|
||||
parse_iex t, config
|
||||
end
|
||||
|
||||
defp parse_iex(["-S", h | t], config) do
|
||||
defp parse_iex(["-S", h|t], config) do
|
||||
{%{config | commands: [{:script, h} | config.commands]}, t}
|
||||
end
|
||||
|
||||
defp parse_iex([h | t] = list, config) do
|
||||
defp parse_iex([h|t] = list, config) do
|
||||
case h do
|
||||
"-" <> _ ->
|
||||
shared_option? list, config, &parse_iex(&1, &2)
|
||||
@@ -390,13 +395,8 @@ defmodule Kernel.CLI do
|
||||
{:ok, files} ->
|
||||
wrapper fn ->
|
||||
Code.compiler_options(config.compiler_options)
|
||||
opts =
|
||||
if config.verbose_compile do
|
||||
[each_long_compilation: &IO.puts("Compiling #{&1} (it's taking more than 5s)")]
|
||||
else
|
||||
[]
|
||||
end
|
||||
Kernel.ParallelCompiler.files_to_path(files, config.output, opts)
|
||||
Kernel.ParallelCompiler.files_to_path(files, config.output,
|
||||
each_file: fn file -> if config.verbose_compile do IO.puts "Compiled #{file}" end end)
|
||||
end
|
||||
{:missing, missing} ->
|
||||
{:error, "No files matched pattern(s) #{Enum.join(missing, ",")}"}
|
||||
|
||||
@@ -3,40 +3,38 @@
|
||||
defmodule Kernel.ErrorHandler do
|
||||
@moduledoc false
|
||||
|
||||
@spec undefined_function(module, atom, list) :: term
|
||||
def undefined_function(module, fun, args) do
|
||||
ensure_loaded(module) or ensure_compiled(module, :module)
|
||||
ensure_loaded(module)
|
||||
:error_handler.undefined_function(module, fun, args)
|
||||
end
|
||||
|
||||
@spec undefined_lambda(module, fun, list) :: term
|
||||
def undefined_lambda(module, fun, args) do
|
||||
ensure_loaded(module) or ensure_compiled(module, :module)
|
||||
ensure_loaded(module)
|
||||
:error_handler.undefined_lambda(module, fun, args)
|
||||
end
|
||||
|
||||
@spec ensure_loaded(module) :: boolean
|
||||
def ensure_loaded(module) do
|
||||
case :code.ensure_loaded(module) do
|
||||
{:module, _} -> true
|
||||
{:error, _} -> false
|
||||
end
|
||||
def release() do
|
||||
# On release, no longer allow elixir_ensure_compiled
|
||||
# directives and revert to the original error handler.
|
||||
# Note we should not delete the elixir_compiler_pid though,
|
||||
# as we still want to send notifications to the compiler.
|
||||
:erlang.erase(:elixir_ensure_compiled)
|
||||
:erlang.process_flag(:error_handler, :error_handler)
|
||||
:ok
|
||||
end
|
||||
|
||||
@spec ensure_compiled(module, atom) :: boolean
|
||||
# Never wait on nil because it should never be defined.
|
||||
def ensure_compiled(nil, _kind) do
|
||||
false
|
||||
end
|
||||
|
||||
def ensure_compiled(module, kind) do
|
||||
parent = :erlang.get(:elixir_compiler_pid)
|
||||
ref = :erlang.make_ref
|
||||
send parent, {:waiting, kind, self(), ref, module, :elixir_module.compiler_modules()}
|
||||
:erlang.garbage_collect(self())
|
||||
receive do
|
||||
{^ref, :found} -> true
|
||||
{^ref, :not_found} -> false
|
||||
defp ensure_loaded(module) do
|
||||
case Code.ensure_loaded(module) do
|
||||
{:module, _} -> :ok
|
||||
{:error, _} ->
|
||||
parent = :erlang.get(:elixir_compiler_pid)
|
||||
ref = :erlang.make_ref
|
||||
send parent, {:waiting, :module, self(), ref, module}
|
||||
:erlang.garbage_collect(self)
|
||||
receive do
|
||||
{^ref, :ready} -> :ok
|
||||
{^ref, :release} -> release()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
# This is an Elixir module responsible for tracking references
|
||||
# to modules, remote dispatches, and the usage of
|
||||
# aliases/imports/requires in the Elixir scope.
|
||||
# This is an Elixir module responsible for tracking
|
||||
# the usage of aliases, imports and requires in the Elixir scope.
|
||||
#
|
||||
# The implementation simply stores dispatch information in an
|
||||
# ETS table and then consults this table once compilation is done.
|
||||
#
|
||||
# Note that since this is required for bootstrap, we can't use
|
||||
# any of the `GenServer.Behaviour` conveniences.
|
||||
@@ -10,18 +12,20 @@ defmodule Kernel.LexicalTracker do
|
||||
@behaviour :gen_server
|
||||
|
||||
@doc """
|
||||
Returns all remotes referenced in this lexical scope.
|
||||
Returns all remotes linked to in this lexical scope.
|
||||
"""
|
||||
def remote_references(arg) do
|
||||
:gen_server.call(to_pid(arg), :remote_references, @timeout)
|
||||
def remotes(arg) do
|
||||
:gen_server.call(to_pid(arg), :ets, @timeout)
|
||||
|> :ets.match({{:mode, :'$1'}, :'$2'})
|
||||
|> partition([], [])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all remote dispatches in this lexical scope.
|
||||
"""
|
||||
def remote_dispatches(arg) do
|
||||
:gen_server.call(to_pid(arg), :remote_dispatches, @timeout)
|
||||
end
|
||||
defp partition([[remote, :compile]|t], compile, runtime),
|
||||
do: partition(t, [remote|compile], runtime)
|
||||
defp partition([[remote, :runtime]|t], compile, runtime),
|
||||
do: partition(t, compile, [remote|runtime])
|
||||
defp partition([], compile, runtime),
|
||||
do: {compile, runtime}
|
||||
|
||||
@doc """
|
||||
Gets the destination the lexical scope is meant to
|
||||
@@ -40,7 +44,7 @@ defmodule Kernel.LexicalTracker do
|
||||
|
||||
# Internal API
|
||||
|
||||
# Starts the tracker and returns its PID.
|
||||
# Starts the tracker and returns its pid.
|
||||
@doc false
|
||||
def start_link(dest) do
|
||||
:gen_server.start_link(__MODULE__, dest, [])
|
||||
@@ -52,32 +56,27 @@ defmodule Kernel.LexicalTracker do
|
||||
end
|
||||
|
||||
@doc false
|
||||
def add_import(pid, module, fas, line, warn) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:add_import, module, fas, line, warn})
|
||||
def add_import(pid, module, line, warn) do
|
||||
:gen_server.cast(pid, {:add_import, module, line, warn})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def add_alias(pid, module, line, warn) when is_atom(module) do
|
||||
def add_alias(pid, module, line, warn) do
|
||||
:gen_server.cast(pid, {:add_alias, module, line, warn})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def remote_reference(pid, module, mode) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:remote_reference, module, mode})
|
||||
def remote_dispatch(pid, module, mode) do
|
||||
:gen_server.cast(pid, {:remote_dispatch, module, mode})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def remote_dispatch(pid, module, fa, line, mode) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:remote_dispatch, module, fa, line, mode})
|
||||
def import_dispatch(pid, module) do
|
||||
:gen_server.cast(pid, {:import_dispatch, module})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def import_dispatch(pid, module, fa, line, mode) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:import_dispatch, module, fa, line, mode})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def alias_dispatch(pid, module) when is_atom(module) do
|
||||
def alias_dispatch(pid, module) do
|
||||
:gen_server.cast(pid, {:alias_dispatch, module})
|
||||
end
|
||||
|
||||
@@ -92,87 +91,61 @@ defmodule Kernel.LexicalTracker do
|
||||
end
|
||||
|
||||
defp unused(pid, tag) do
|
||||
:gen_server.call(pid, {:unused, tag}, @timeout)
|
||||
:gen_server.call(pid, :ets, @timeout)
|
||||
|> :ets.select([{{{tag, :"$1"}, :"$2"}, [is_integer: :"$2"], [{{:"$1", :"$2"}}]}])
|
||||
|> Enum.sort
|
||||
end
|
||||
|
||||
# Callbacks
|
||||
|
||||
def init(dest) do
|
||||
{:ok, %{directives: %{}, references: %{}, compile: %{},
|
||||
runtime: %{}, dest: dest}}
|
||||
{:ok, {:ets.new(__MODULE__, [:protected]), dest}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_call({:unused, tag}, _from, state) do
|
||||
directives =
|
||||
for {{^tag, module_or_mfa}, marker} <- state.directives,
|
||||
is_integer(marker),
|
||||
do: {module_or_mfa, marker}
|
||||
|
||||
{:reply, Enum.sort(directives), state}
|
||||
def handle_call(:ets, _from, {d, dest}) do
|
||||
{:reply, d, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_call(:remote_references, _from, state) do
|
||||
{:reply, partition(Enum.to_list(state.references), [], []), state}
|
||||
def handle_call(:dest, _from, {d, dest}) do
|
||||
{:reply, dest, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_call(:remote_dispatches, _from, state) do
|
||||
{:reply, {state.compile, state.runtime}, state}
|
||||
def handle_cast({:remote_dispatch, module, mode}, {d, dest}) do
|
||||
add_compile(d, module, mode)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_call(:dest, _from, state) do
|
||||
{:reply, state.dest, state}
|
||||
def handle_cast({:import_dispatch, module}, {d, dest}) do
|
||||
add_dispatch(d, module, :import)
|
||||
# Always compile time because we depend
|
||||
# on the module at compile time
|
||||
add_compile(d, module, :compile)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:remote_reference, module, mode}, state) do
|
||||
{:noreply, %{state | references: add_reference(state.references, module, mode)}}
|
||||
def handle_cast({:alias_dispatch, module}, {d, dest}) do
|
||||
add_dispatch(d, module, :alias)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:remote_dispatch, module, fa, line, mode}, state) do
|
||||
references = add_reference(state.references, module, mode)
|
||||
state = add_remote_dispatch(state, module, fa, line, mode)
|
||||
{:noreply, %{state | references: references}}
|
||||
def handle_cast({:add_import, module, line, warn}, {d, dest}) do
|
||||
add_directive(d, module, line, warn, :import)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:import_dispatch, module, {function, arity} = fa, line, mode}, state) do
|
||||
state =
|
||||
state
|
||||
|> add_import_dispatch(module, function, arity)
|
||||
|> add_remote_dispatch(module, fa, line, mode)
|
||||
|
||||
{:noreply, state}
|
||||
def handle_cast({:add_alias, module, line, warn}, {d, dest}) do
|
||||
add_directive(d, module, line, warn, :alias)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:alias_dispatch, module}, state) do
|
||||
{:noreply, %{state | directives: add_dispatch(state.directives, module, :alias)}}
|
||||
end
|
||||
|
||||
def handle_cast({:add_import, module, fas, line, warn}, state) do
|
||||
directives =
|
||||
state.directives
|
||||
|> Enum.reject(&match?({{:import, {^module, _, _}}, _}, &1))
|
||||
|> :maps.from_list
|
||||
|> add_directive(module, line, warn, :import)
|
||||
|
||||
directives =
|
||||
Enum.reduce(fas, directives, fn {function, arity}, directives ->
|
||||
add_directive(directives, {module, function, arity}, line, warn, :import)
|
||||
end)
|
||||
|
||||
{:noreply, %{state | directives: directives}}
|
||||
end
|
||||
|
||||
def handle_cast({:add_alias, module, line, warn}, state) do
|
||||
{:noreply, %{state | directives: add_directive(state.directives, module, line, warn, :alias)}}
|
||||
end
|
||||
|
||||
def handle_cast(:stop, state) do
|
||||
{:stop, :normal, state}
|
||||
def handle_cast(:stop, {d, dest}) do
|
||||
{:stop, :normal, {d, dest}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_info(_msg, state) do
|
||||
{:noreply, state}
|
||||
def handle_info(_msg, {d, dest}) do
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -185,62 +158,21 @@ defmodule Kernel.LexicalTracker do
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
defp partition([{remote, :compile} | t], compile, runtime),
|
||||
do: partition(t, [remote | compile], runtime)
|
||||
defp partition([{remote, :runtime} | t], compile, runtime),
|
||||
do: partition(t, compile, [remote | runtime])
|
||||
defp partition([], compile, runtime),
|
||||
do: {compile, runtime}
|
||||
|
||||
# Callbacks helpers
|
||||
|
||||
defp add_reference(references, module, :runtime) when is_atom(module),
|
||||
do: map_put_new(module, :runtime, references)
|
||||
defp add_reference(references, module, :compile) when is_atom(module),
|
||||
do: :maps.put(module, :compile, references)
|
||||
|
||||
defp add_remote_dispatch(state, module, fa, line, mode) when is_atom(module) do
|
||||
map_update mode, %{module => %{fa => [line]}}, state, fn mode_dispatches ->
|
||||
map_update module, %{fa => [line]}, mode_dispatches, fn module_dispatches ->
|
||||
map_update fa, [line], module_dispatches, &[line | List.delete(&1, line)]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp add_import_dispatch(state, module, function, arity) do
|
||||
directives =
|
||||
add_dispatch(state.directives, module, :import)
|
||||
|> add_dispatch({module, function, arity}, :import)
|
||||
# Always compile time because we depend
|
||||
# on the module at compile time
|
||||
references = add_reference(state.references, module, :compile)
|
||||
|
||||
%{state | directives: directives, references: references}
|
||||
end
|
||||
|
||||
# In the map we keep imports and aliases.
|
||||
# In the table we keep imports and aliases.
|
||||
# If the value is false, it was not imported/aliased
|
||||
# If the value is a line, it was imported/aliased and has a pending warning
|
||||
# If the value is true, it was imported/aliased and used
|
||||
defp add_directive(directives, module_or_mfa, line, warn, tag) do
|
||||
defp add_dispatch(d, module, tag) do
|
||||
:ets.insert(d, {{tag, module}, true})
|
||||
end
|
||||
|
||||
defp add_compile(d, module, :runtime), do: :ets.insert_new(d, {{:mode, module}, :runtime})
|
||||
defp add_compile(d, module, :compile), do: :ets.insert(d, {{:mode, module}, :compile})
|
||||
|
||||
defp add_directive(d, module, line, warn, tag) do
|
||||
marker = if warn, do: line, else: true
|
||||
:maps.put({tag, module_or_mfa}, marker, directives)
|
||||
end
|
||||
|
||||
defp add_dispatch(directives, module_or_mfa, tag) do
|
||||
:maps.put({tag, module_or_mfa}, true, directives)
|
||||
end
|
||||
|
||||
defp map_update(key, initial, map, fun) do
|
||||
case :maps.find(key, map) do
|
||||
{:ok, val} -> :maps.put(key, fun.(val), map)
|
||||
:error -> :maps.put(key, initial, map)
|
||||
end
|
||||
end
|
||||
|
||||
defp map_put_new(key, value, map) do
|
||||
case :maps.find(key, map) do
|
||||
{:ok, _} -> map
|
||||
:error -> :maps.put(key, value, map)
|
||||
end
|
||||
:ets.insert(d, {{tag, module}, marker})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -20,18 +20,11 @@ defmodule Kernel.ParallelCompiler do
|
||||
* `:each_file` - for each file compiled, invokes the callback passing the
|
||||
file
|
||||
|
||||
* `:each_long_compilation` - for each file that takes more than a given
|
||||
timeout (see the `:long_compilation_threshold` option) to compile, invoke
|
||||
this callback passing the file as its argument
|
||||
|
||||
* `:long_compilation_threshold` - the timeout (in seconds) after the
|
||||
`:each_long_compilation` callback is invoked; defaults to `10`
|
||||
|
||||
* `:each_module` - for each module compiled, invokes the callback passing
|
||||
the file, module and the module bytecode
|
||||
|
||||
* `:dest` - the destination directory for the BEAM files. When using `files/2`,
|
||||
this information is only used to properly annotate the BEAM files before
|
||||
* `:dest` - the destination directory for the beam files. When using `files/2`,
|
||||
this information is only used to properly annotate the beam files before
|
||||
they are loaded into memory. If you want a file to actually be written to
|
||||
`dest`, use `files_to_path/3` instead.
|
||||
|
||||
@@ -59,16 +52,7 @@ defmodule Kernel.ParallelCompiler do
|
||||
:elixir_code_server.cast({:reset_warnings, compiler_pid})
|
||||
schedulers = max(:erlang.system_info(:schedulers_online), 2)
|
||||
|
||||
result = spawn_compilers(%{
|
||||
entries: files,
|
||||
original: files,
|
||||
output: path,
|
||||
options: options,
|
||||
waiting: [],
|
||||
queued: [],
|
||||
schedulers: schedulers,
|
||||
result: [],
|
||||
})
|
||||
result = spawn_compilers(files, files, path, options, [], [], schedulers, [])
|
||||
|
||||
# In case --warning-as-errors is enabled and there was a warning,
|
||||
# compilation status will be set to error.
|
||||
@@ -81,113 +65,81 @@ defmodule Kernel.ParallelCompiler do
|
||||
end
|
||||
end
|
||||
|
||||
# We already have n=schedulers currently running, don't spawn new ones
|
||||
defp spawn_compilers(%{queued: queued, waiting: waiting, schedulers: schedulers} = state)
|
||||
when length(queued) - length(waiting) >= schedulers do
|
||||
wait_for_messages(state)
|
||||
# We already have 4 currently running, don't spawn new ones
|
||||
defp spawn_compilers(entries, original, output, options, waiting, queued, schedulers, result) when
|
||||
length(queued) - length(waiting) >= schedulers do
|
||||
wait_for_messages(entries, original, output, options, waiting, queued, schedulers, result)
|
||||
end
|
||||
|
||||
# Release waiting processes
|
||||
defp spawn_compilers(%{entries: [{ref, found} | t], waiting: waiting} = state) do
|
||||
waiting =
|
||||
case List.keytake(waiting, ref, 2) do
|
||||
{{_kind, pid, ^ref, _on, _defining}, waiting} ->
|
||||
send pid, {ref, found}
|
||||
waiting
|
||||
nil ->
|
||||
waiting
|
||||
end
|
||||
spawn_compilers(%{state | entries: t, waiting: waiting})
|
||||
defp spawn_compilers([h|t], original, output, options, waiting, queued, schedulers, result) when is_pid(h) do
|
||||
{_kind, ^h, ref, _module} = List.keyfind(waiting, h, 1)
|
||||
send h, {ref, :ready}
|
||||
waiting = List.keydelete(waiting, h, 1)
|
||||
spawn_compilers(t, original, output, options, waiting, queued, schedulers, result)
|
||||
end
|
||||
|
||||
defp spawn_compilers(%{entries: [file | files], queued: queued, output: output, options: options} = state) do
|
||||
# Spawn a compiler for each file in the list until we reach the limit
|
||||
defp spawn_compilers([h|t], original, output, options, waiting, queued, schedulers, result) do
|
||||
parent = self()
|
||||
|
||||
{pid, ref} =
|
||||
:erlang.spawn_monitor fn ->
|
||||
# Notify Code.ensure_compiled/2 that we should
|
||||
# attempt to compile the module by doing a dispatch.
|
||||
:erlang.put(:elixir_ensure_compiled, true)
|
||||
|
||||
# Set the elixir_compiler_pid used by our custom Kernel.ErrorHandler.
|
||||
:erlang.put(:elixir_compiler_pid, parent)
|
||||
:erlang.put(:elixir_compiler_file, file)
|
||||
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
|
||||
|
||||
exit(try do
|
||||
_ = if output do
|
||||
:elixir_compiler.file_to_path(file, output)
|
||||
:elixir_compiler.file_to_path(h, output)
|
||||
else
|
||||
:elixir_compiler.file(file, Keyword.get(options, :dest))
|
||||
:elixir_compiler.file(h, Keyword.get(options, :dest))
|
||||
end
|
||||
{:shutdown, file}
|
||||
{:shutdown, h}
|
||||
catch
|
||||
kind, reason ->
|
||||
{:failure, kind, reason, System.stacktrace}
|
||||
end)
|
||||
end
|
||||
|
||||
timeout = Keyword.get(options, :long_compilation_threshold, 10) * 1_000
|
||||
timer_ref = Process.send_after(self(), {:timed_out, pid}, timeout)
|
||||
|
||||
new_queued = [{pid, ref, file, timer_ref} | queued]
|
||||
spawn_compilers(%{state | entries: files, queued: new_queued})
|
||||
spawn_compilers(t, original, output, options, waiting,
|
||||
[{pid, ref, h}|queued], schedulers, result)
|
||||
end
|
||||
|
||||
# No more files, nothing waiting, queue is empty, we are done
|
||||
defp spawn_compilers(%{entries: [], waiting: [], queued: [], result: result}) do
|
||||
defp spawn_compilers([], _original, _output, _options, [], [], _schedulers, result) do
|
||||
for {:module, mod} <- result, do: mod
|
||||
end
|
||||
|
||||
# Queued x, waiting for x: POSSIBLE ERROR! Release processes so we get the failures
|
||||
defp spawn_compilers(%{entries: [], waiting: waiting, queued: queued} = state) when length(waiting) == length(queued) do
|
||||
entries = for {pid, _, _, _} <- queued,
|
||||
entry = waiting_on_without_definition(waiting, pid),
|
||||
{_, _, ref, on, _} = entry,
|
||||
do: {on, {ref, :not_found}}
|
||||
|
||||
# Instead of releasing all files at once, we release them in groups
|
||||
# based on the module they are waiting on. We pick the module being
|
||||
# depended on with less edges, as it is the mostly likely source of
|
||||
# error (for example, someone made a typo). This may not always be
|
||||
# true though: for example, if there is a macro injecting code into
|
||||
# multiple modules and such code becomes faulty, now multiple modules
|
||||
# are waiting on the same module required by the faulty code. However,
|
||||
# since we need to pick something to be first, the one with fewer edges
|
||||
# sounds like a sane choice.
|
||||
entries =
|
||||
entries
|
||||
|> Enum.group_by(&elem(&1, 0), &elem(&1, 1))
|
||||
|> Enum.sort_by(&length(elem(&1, 1)))
|
||||
|> Enum.find_value([], &elem(&1, 1))
|
||||
|
||||
case entries do
|
||||
[] -> handle_deadlock(waiting, queued)
|
||||
_ -> spawn_compilers(%{state | entries: entries})
|
||||
defp spawn_compilers([], original, output, options, waiting, queued, schedulers, result) when length(waiting) == length(queued) do
|
||||
Enum.each queued, fn {child, _, _} ->
|
||||
{_kind, ^child, ref, _module} = List.keyfind(waiting, child, 1)
|
||||
send child, {ref, :release}
|
||||
end
|
||||
wait_for_messages([], original, output, options, waiting, queued, schedulers, result)
|
||||
end
|
||||
|
||||
# No more files, but queue and waiting are not full or do not match
|
||||
defp spawn_compilers(%{entries: []} = state) do
|
||||
wait_for_messages(state)
|
||||
end
|
||||
|
||||
defp waiting_on_without_definition(waiting, pid) do
|
||||
{_, ^pid, _, on, _} = entry = List.keyfind(waiting, pid, 1)
|
||||
if Enum.any?(waiting, fn {_, _, _, _, defining} -> on in defining end) do
|
||||
nil
|
||||
else
|
||||
entry
|
||||
end
|
||||
defp spawn_compilers([], original, output, options, waiting, queued, schedulers, result) do
|
||||
wait_for_messages([], original, output, options, waiting, queued, schedulers, result)
|
||||
end
|
||||
|
||||
# Wait for messages from child processes
|
||||
defp wait_for_messages(state) do
|
||||
%{entries: entries, options: options, waiting: waiting, queued: queued, result: result} = state
|
||||
|
||||
defp wait_for_messages(entries, original, output, options, waiting, queued, schedulers, result) do
|
||||
receive do
|
||||
{:struct_available, module} ->
|
||||
available = for {:struct, _, ref, waiting_module, _defining} <- waiting,
|
||||
available = for {:struct, pid, _, waiting_module} <- waiting,
|
||||
module == waiting_module,
|
||||
do: {ref, :found}
|
||||
not pid in entries,
|
||||
do: pid
|
||||
|
||||
spawn_compilers(%{state | entries: available ++ entries, result: [{:struct, module} | result]})
|
||||
spawn_compilers(available ++ entries, original, output, options,
|
||||
waiting, queued, schedulers, [{:struct, module}|result])
|
||||
|
||||
{:module_available, child, ref, file, module, binary} ->
|
||||
if callback = Keyword.get(options, :each_module) do
|
||||
@@ -197,104 +149,63 @@ defmodule Kernel.ParallelCompiler do
|
||||
# Release the module loader which is waiting for an ack
|
||||
send child, {ref, :ack}
|
||||
|
||||
available = for {:module, _, ref, waiting_module, _defining} <- waiting,
|
||||
available = for {_kind, pid, _, waiting_module} <- waiting,
|
||||
module == waiting_module,
|
||||
do: {ref, :found}
|
||||
not pid in entries,
|
||||
do: pid
|
||||
|
||||
cancel_waiting_timer(queued, child)
|
||||
spawn_compilers(available ++ entries, original, output, options,
|
||||
waiting, queued, schedulers, [{:module, module}|result])
|
||||
|
||||
spawn_compilers(%{state | entries: available ++ entries, result: [{:module, module} | result]})
|
||||
{:waiting, kind, child, ref, on} ->
|
||||
defined = fn {k, m} -> on == m and k in [kind, :module] end
|
||||
|
||||
{:waiting, kind, child, ref, on, defining} ->
|
||||
# Oops, we already got it, do not put it on waiting.
|
||||
# Alternatively, we're waiting on ourselves,
|
||||
# send :found so that we can crash with a better error.
|
||||
waiting =
|
||||
if :lists.any(&match?({^kind, ^on}, &1), result) or on in defining do
|
||||
send child, {ref, :found}
|
||||
waiting
|
||||
else
|
||||
[{kind, child, ref, on, defining} | waiting]
|
||||
end
|
||||
|
||||
spawn_compilers(%{state | waiting: waiting})
|
||||
|
||||
{:timed_out, child} ->
|
||||
callback = Keyword.get(options, :each_long_compilation)
|
||||
case List.keyfind(queued, child, 0) do
|
||||
{^child, _, file, _} when not is_nil(callback) ->
|
||||
callback.(file)
|
||||
_ ->
|
||||
:ok
|
||||
if :lists.any(defined, result) do
|
||||
send child, {ref, :ready}
|
||||
else
|
||||
waiting = [{kind, child, ref, on}|waiting]
|
||||
end
|
||||
spawn_compilers(state)
|
||||
|
||||
spawn_compilers(entries, original, output, options, waiting, queued, schedulers, result)
|
||||
|
||||
{:DOWN, _down_ref, :process, down_pid, {:shutdown, file}} ->
|
||||
if callback = Keyword.get(options, :each_file) do
|
||||
callback.(file)
|
||||
end
|
||||
|
||||
cancel_waiting_timer(queued, down_pid)
|
||||
|
||||
# Sometimes we may have spurious entries in the waiting
|
||||
# list because someone invoked try/rescue UndefinedFunctionError
|
||||
new_entries = List.delete(entries, down_pid)
|
||||
new_queued = List.keydelete(queued, down_pid, 0)
|
||||
new_waiting = List.keydelete(waiting, down_pid, 1)
|
||||
spawn_compilers(%{state | entries: new_entries, waiting: new_waiting, queued: new_queued})
|
||||
spawn_compilers(new_entries, original, output, options, new_waiting, new_queued, schedulers, result)
|
||||
|
||||
{:DOWN, down_ref, :process, _down_pid, reason} ->
|
||||
handle_failure(down_ref, reason, queued)
|
||||
wait_for_messages(state)
|
||||
handle_failure(down_ref, reason, entries, waiting, queued)
|
||||
wait_for_messages(entries, original, output, options, waiting, queued, schedulers, result)
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_deadlock(waiting, queued) do
|
||||
deadlock =
|
||||
for {pid, _, file, _} <- queued do
|
||||
{:current_stacktrace, stacktrace} = Process.info(pid, :current_stacktrace)
|
||||
Process.exit(pid, :kill)
|
||||
|
||||
{_kind, ^pid, _, on, _} = List.keyfind(waiting, pid, 1)
|
||||
error = CompileError.exception(description: "deadlocked waiting on module #{inspect on}",
|
||||
file: nil, line: nil)
|
||||
print_failure(file, {:failure, :error, error, stacktrace})
|
||||
|
||||
{file, on}
|
||||
end
|
||||
|
||||
IO.puts """
|
||||
|
||||
Compilation failed because of a deadlock between files.
|
||||
The following files depended on the following modules:
|
||||
"""
|
||||
|
||||
max =
|
||||
deadlock
|
||||
|> Enum.map(& &1 |> elem(0) |> String.length)
|
||||
|> Enum.max
|
||||
|
||||
for {file, mod} <- deadlock do
|
||||
IO.puts [" ", String.pad_leading(file, max), " => " | inspect(mod)]
|
||||
end
|
||||
|
||||
IO.puts ""
|
||||
exit({:shutdown, 1})
|
||||
end
|
||||
|
||||
defp handle_failure(ref, reason, queued) do
|
||||
defp handle_failure(ref, reason, entries, waiting, queued) do
|
||||
if file = find_failure(ref, queued) do
|
||||
print_failure(file, reason)
|
||||
for {pid, _, _, _} <- queued do
|
||||
Process.exit(pid, :kill)
|
||||
|
||||
if all_missing?(entries, waiting, queued) do
|
||||
collect_failures(queued, length(queued) - 1)
|
||||
end
|
||||
|
||||
Enum.each queued, fn {child, _, _} ->
|
||||
Process.exit(child, :kill)
|
||||
end
|
||||
|
||||
exit({:shutdown, 1})
|
||||
end
|
||||
end
|
||||
|
||||
defp find_failure(ref, queued) do
|
||||
case List.keyfind(queued, ref, 1) do
|
||||
{_child, ^ref, file, _timer_ref} -> file
|
||||
{_child, ^ref, file} -> file
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
@@ -315,33 +226,39 @@ defmodule Kernel.ParallelCompiler do
|
||||
|
||||
@elixir_internals [:elixir, :elixir_exp, :elixir_compiler, :elixir_module, :elixir_clauses,
|
||||
:elixir_translator, :elixir_expand, :elixir_lexical, :elixir_exp_clauses,
|
||||
:elixir_def, :elixir_map, Kernel.ErrorHandler]
|
||||
:elixir_def]
|
||||
|
||||
defp prune_stacktrace([{mod, _, _, _} | t]) when mod in @elixir_internals do
|
||||
defp prune_stacktrace([{mod, _, _, _}|t]) when mod in @elixir_internals do
|
||||
prune_stacktrace(t)
|
||||
end
|
||||
|
||||
defp prune_stacktrace([h | t]) do
|
||||
[h | prune_stacktrace(t)]
|
||||
defp prune_stacktrace([h|t]) do
|
||||
[h|prune_stacktrace(t)]
|
||||
end
|
||||
|
||||
defp prune_stacktrace([]) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp cancel_waiting_timer(queued, child_pid) do
|
||||
case List.keyfind(queued, child_pid, 0) do
|
||||
{^child_pid, _ref, _file, timer_ref} ->
|
||||
Process.cancel_timer(timer_ref)
|
||||
# Let's flush the message in case it arrived before we canceled the
|
||||
# timeout.
|
||||
receive do
|
||||
{:timed_out, ^child_pid} -> :ok
|
||||
after
|
||||
0 -> :ok
|
||||
defp all_missing?(entries, waiting, queued) do
|
||||
entries == [] and waiting != [] and
|
||||
length(waiting) == length(queued)
|
||||
end
|
||||
|
||||
defp collect_failures(_queued, 0), do: :ok
|
||||
|
||||
defp collect_failures(queued, remaining) do
|
||||
receive do
|
||||
{:DOWN, down_ref, :process, _down_pid, reason} ->
|
||||
if file = find_failure(down_ref, queued) do
|
||||
print_failure(file, reason)
|
||||
collect_failures(queued, remaining - 1)
|
||||
else
|
||||
collect_failures(queued, remaining)
|
||||
end
|
||||
nil ->
|
||||
:ok
|
||||
after
|
||||
# Give up if no failure appears in 5 seconds
|
||||
5000 -> :ok
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,25 +6,16 @@ defmodule Kernel.ParallelRequire do
|
||||
@doc """
|
||||
Requires the given files.
|
||||
|
||||
A callback that will be invoked with each file, or a keyword list of `callbacks` can be provided:
|
||||
|
||||
* `:each_file` - invoked with each file
|
||||
|
||||
* `:each_module` - invoked with file, module name, and binary code
|
||||
A callback that is invoked every time a file is required
|
||||
can be optionally given as argument.
|
||||
|
||||
Returns the modules generated by each required file.
|
||||
"""
|
||||
def files(files, callbacks \\ [])
|
||||
|
||||
def files(files, callback) when is_function(callback, 1) do
|
||||
files(files, [each_file: callback])
|
||||
end
|
||||
|
||||
def files(files, callbacks) when is_list(callbacks) do
|
||||
def files(files, callback \\ fn x -> x end) do
|
||||
compiler_pid = self()
|
||||
:elixir_code_server.cast({:reset_warnings, compiler_pid})
|
||||
schedulers = max(:erlang.system_info(:schedulers_online), 2)
|
||||
result = spawn_requires(files, [], callbacks, schedulers, [])
|
||||
result = spawn_requires(files, [], callback, schedulers, [])
|
||||
|
||||
# In case --warning-as-errors is enabled and there was a warning,
|
||||
# compilation status will be set to error.
|
||||
@@ -32,78 +23,63 @@ defmodule Kernel.ParallelRequire do
|
||||
:ok ->
|
||||
result
|
||||
:error ->
|
||||
IO.puts :stderr, "\nExecution failed due to warnings while using the --warnings-as-errors option"
|
||||
IO.puts :stderr, "Compilation failed due to warnings while using the --warnings-as-errors option"
|
||||
exit({:shutdown, 1})
|
||||
end
|
||||
end
|
||||
|
||||
defp spawn_requires([], [], _callbacks, _schedulers, result), do: result
|
||||
defp spawn_requires([], [], _callback, _schedulers, result), do: result
|
||||
|
||||
defp spawn_requires([], waiting, callbacks, schedulers, result) do
|
||||
wait_for_messages([], waiting, callbacks, schedulers, result)
|
||||
defp spawn_requires([], waiting, callback, schedulers, result) do
|
||||
wait_for_messages([], waiting, callback, schedulers, result)
|
||||
end
|
||||
|
||||
defp spawn_requires(files, waiting, callbacks, schedulers, result) when length(waiting) >= schedulers do
|
||||
wait_for_messages(files, waiting, callbacks, schedulers, result)
|
||||
defp spawn_requires(files, waiting, callback, schedulers, result) when length(waiting) >= schedulers do
|
||||
wait_for_messages(files, waiting, callback, schedulers, result)
|
||||
end
|
||||
|
||||
defp spawn_requires([file | files], waiting, callbacks, schedulers, result) do
|
||||
defp spawn_requires([h|t], waiting, callback, schedulers, result) do
|
||||
parent = self()
|
||||
{pid, ref} = :erlang.spawn_monitor fn ->
|
||||
:erlang.put(:elixir_compiler_pid, parent)
|
||||
:erlang.put(:elixir_compiler_file, file)
|
||||
|
||||
exit(try do
|
||||
new = Code.require_file(file) || []
|
||||
{:required, Enum.map(new, &elem(&1, 0)), file}
|
||||
new = Code.require_file(h) || []
|
||||
{:required, Enum.map(new, &elem(&1, 0)), h}
|
||||
catch
|
||||
kind, reason ->
|
||||
{:failure, kind, reason, System.stacktrace}
|
||||
end)
|
||||
end
|
||||
|
||||
spawn_requires(files, [{pid, ref} | waiting], callbacks, schedulers, result)
|
||||
spawn_requires(t, [{pid, ref}|waiting], callback, schedulers, result)
|
||||
end
|
||||
|
||||
defp wait_for_messages(files, waiting, callbacks, schedulers, result) do
|
||||
defp wait_for_messages(files, waiting, callback, schedulers, result) do
|
||||
receive do
|
||||
{:DOWN, ref, :process, pid, status} ->
|
||||
tuple = {pid, ref}
|
||||
if tuple in waiting do
|
||||
waiting = List.delete(waiting, tuple)
|
||||
|
||||
case status do
|
||||
{:required, mods, file} ->
|
||||
if each_file_callback = callbacks[:each_file] do
|
||||
each_file_callback.(file)
|
||||
end
|
||||
|
||||
spawn_requires(files, waiting, callbacks, schedulers, mods ++ result)
|
||||
|
||||
callback.(file)
|
||||
result = mods ++ result
|
||||
waiting = List.delete(waiting, tuple)
|
||||
{:failure, kind, reason, stacktrace} ->
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
|
||||
other ->
|
||||
:erlang.raise(:exit, other, [])
|
||||
end
|
||||
else
|
||||
spawn_requires(files, waiting, callbacks, schedulers, result)
|
||||
end
|
||||
|
||||
{:module_available, child, ref, file, module, binary} ->
|
||||
if each_module_callback = callbacks[:each_module] do
|
||||
each_module_callback.(file, module, binary)
|
||||
end
|
||||
|
||||
spawn_requires(files, waiting, callback, schedulers, result)
|
||||
{:module_available, child, ref, _, _, _} ->
|
||||
send(child, {ref, :ack})
|
||||
spawn_requires(files, waiting, callbacks, schedulers, result)
|
||||
|
||||
spawn_requires(files, waiting, callback, schedulers, result)
|
||||
{:struct_available, _} ->
|
||||
spawn_requires(files, waiting, callbacks, schedulers, result)
|
||||
|
||||
{:waiting, _, child, ref, _, _} ->
|
||||
send(child, {ref, :not_found})
|
||||
spawn_requires(files, waiting, callbacks, schedulers, result)
|
||||
spawn_requires(files, waiting, callback, schedulers, result)
|
||||
{:waiting, :struct, child, ref, _} ->
|
||||
send(child, {ref, :release})
|
||||
spawn_requires(files, waiting, callback, schedulers, result)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -92,12 +92,6 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
end
|
||||
|
||||
defmacro defoptional_callbacks(callbacks) do
|
||||
quote do
|
||||
Module.store_typespec(__ENV__.module, :optional_callbacks, {__ENV__.line, unquote(callbacks)})
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Defines a `type`, `typep` or `opaque` by receiving a typespec expression.
|
||||
"""
|
||||
@@ -108,7 +102,6 @@ defmodule Kernel.Typespec do
|
||||
@doc """
|
||||
Defines a `spec` by receiving a typespec expression.
|
||||
"""
|
||||
@spec define_spec(atom, Macro.t, Macro.Env.t) :: Keyword.t
|
||||
def define_spec(kind, expr, env) do
|
||||
defspec(kind, expr, env)
|
||||
end
|
||||
@@ -118,9 +111,7 @@ defmodule Kernel.Typespec do
|
||||
(private, opaque or not). This function is only available
|
||||
for modules being compiled.
|
||||
"""
|
||||
@spec defines_type?(module, atom, arity) :: boolean
|
||||
def defines_type?(module, name, arity)
|
||||
when is_atom(module) and is_atom(name) and arity in 0..255 do
|
||||
def defines_type?(module, name, arity) do
|
||||
finder = fn {_kind, expr, _caller} ->
|
||||
type_to_signature(expr) == {name, arity}
|
||||
end
|
||||
@@ -132,9 +123,7 @@ defmodule Kernel.Typespec do
|
||||
Returns `true` if the current module defines a given spec.
|
||||
This function is only available for modules being compiled.
|
||||
"""
|
||||
@spec defines_spec?(module, atom, arity) :: boolean
|
||||
def defines_spec?(module, name, arity)
|
||||
when is_atom(module) and is_atom(name) and arity in 0..255 do
|
||||
def defines_spec?(module, name, arity) do
|
||||
finder = fn {_kind, expr, _caller} ->
|
||||
spec_to_signature(expr) == {name, arity}
|
||||
end
|
||||
@@ -145,9 +134,7 @@ defmodule Kernel.Typespec do
|
||||
Returns `true` if the current module defines a callback.
|
||||
This function is only available for modules being compiled.
|
||||
"""
|
||||
@spec defines_callback?(module, atom, arity) :: boolean
|
||||
def defines_callback?(module, name, arity)
|
||||
when is_atom(module) and is_atom(name) and arity in 0..255 do
|
||||
def defines_callback?(module, name, arity) do
|
||||
finder = fn {_kind, expr, _caller} ->
|
||||
spec_to_signature(expr) == {name, arity}
|
||||
end
|
||||
@@ -157,10 +144,8 @@ defmodule Kernel.Typespec do
|
||||
@doc """
|
||||
Converts a spec clause back to Elixir AST.
|
||||
"""
|
||||
@spec spec_to_ast(atom, tuple) :: {atom, Keyword.t, [Macro.t]}
|
||||
def spec_to_ast(name, spec)
|
||||
def spec_to_ast(name, {:type, line, :fun, [{:type, _, :product, args}, result]})
|
||||
when is_atom(name) do
|
||||
def spec_to_ast(name, {:type, line, :fun, [{:type, _, :product, args}, result]}) do
|
||||
meta = [line: line]
|
||||
body = {name, meta, Enum.map(args, &typespec_to_ast/1)}
|
||||
|
||||
@@ -178,12 +163,11 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
end
|
||||
|
||||
def spec_to_ast(name, {:type, line, :fun, []}) when is_atom(name) do
|
||||
def spec_to_ast(name, {:type, line, :fun, []}) do
|
||||
{:::, [line: line], [{name, [line: line], []}, quote(do: term)]}
|
||||
end
|
||||
|
||||
def spec_to_ast(name, {:type, line, :bounded_fun, [{:type, _, :fun, [{:type, _, :product, args}, result]}, constraints]})
|
||||
when is_atom(name) do
|
||||
def spec_to_ast(name, {:type, line, :bounded_fun, [{:type, _, :fun, [{:type, _, :product, args}, result]}, constraints]}) do
|
||||
guards =
|
||||
for {:type, _, :constraint, [{:atom, _, :is_subtype}, [{:var, _, var}, type]]} <- constraints do
|
||||
{var, typespec_to_ast(type)}
|
||||
@@ -212,32 +196,31 @@ defmodule Kernel.Typespec do
|
||||
def type_to_ast({{:record, record}, fields, args}) when is_atom(record) do
|
||||
fields = for field <- fields, do: typespec_to_ast(field)
|
||||
args = for arg <- args, do: typespec_to_ast(arg)
|
||||
type = {:{}, [], [record | fields]}
|
||||
type = {:{}, [], [record|fields]}
|
||||
quote do: unquote(record)(unquote_splicing(args)) :: unquote(type)
|
||||
end
|
||||
|
||||
def type_to_ast({name, type, args}) when is_atom(name) do
|
||||
def type_to_ast({name, type, args}) do
|
||||
args = for arg <- args, do: typespec_to_ast(arg)
|
||||
quote do: unquote(name)(unquote_splicing(args)) :: unquote(typespec_to_ast(type))
|
||||
end
|
||||
|
||||
# TODO: Deprecate by 1.2
|
||||
# TODO: Remove by 2.0
|
||||
@doc false
|
||||
# TODO: Remove on v2.0
|
||||
def beam_typedocs(module) when is_atom(module) or is_binary(module) do
|
||||
IO.write :stderr, "Kernel.Typespec.beam_typedocs/1 is deprecated, please use Code.get_docs/2 instead\n" <>
|
||||
Exception.format_stacktrace
|
||||
if docs = Code.get_docs(module, :type_docs) do
|
||||
for {tuple, _, _, doc} <- docs, do: {tuple, doc}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all types available from the module's BEAM code.
|
||||
Returns all types available from the module's beam code.
|
||||
|
||||
The result is returned as a list of tuples where the first
|
||||
element is the type (`:typep`, `:type` and `:opaque`).
|
||||
|
||||
The module must have a corresponding BEAM file which can be
|
||||
The module must have a corresponding beam file which can be
|
||||
located by the runtime system.
|
||||
"""
|
||||
@spec beam_types(module | binary) :: [tuple] | nil
|
||||
@@ -260,12 +243,12 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all specs available from the module's BEAM code.
|
||||
Returns all specs available from the module's beam code.
|
||||
|
||||
The result is returned as a list of tuples where the first
|
||||
element is spec name and arity and the second is the spec.
|
||||
|
||||
The module must have a corresponding BEAM file which can be
|
||||
The module must have a corresponding beam file which can be
|
||||
located by the runtime system.
|
||||
"""
|
||||
@spec beam_specs(module | binary) :: [tuple] | nil
|
||||
@@ -274,12 +257,12 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all callbacks available from the module's BEAM code.
|
||||
Returns all callbacks available from the module's beam code.
|
||||
|
||||
The result is returned as a list of tuples where the first
|
||||
element is spec name and arity and the second is the spec.
|
||||
|
||||
The module must have a corresponding BEAM file
|
||||
The module must have a corresponding beam file
|
||||
which can be located by the runtime system.
|
||||
"""
|
||||
@spec beam_callbacks(module | binary) :: [tuple] | nil
|
||||
@@ -358,7 +341,7 @@ defmodule Kernel.Typespec do
|
||||
|
||||
defp get_doc_info(table, attr, caller) do
|
||||
case :ets.take(table, attr) do
|
||||
[{^attr, {line, doc}, _, _}] -> {line, doc}
|
||||
[{^attr, {line, doc}}] -> {line, doc}
|
||||
[] -> {caller.line, nil}
|
||||
end
|
||||
end
|
||||
@@ -424,11 +407,7 @@ defmodule Kernel.Typespec do
|
||||
|
||||
defp elixir_builtin_type?(:as_boolean, 1), do: true
|
||||
defp elixir_builtin_type?(:struct, 0), do: true
|
||||
defp elixir_builtin_type?(:charlist, 0), do: true
|
||||
# TODO: Deprecate char_list type by v1.5
|
||||
defp elixir_builtin_type?(:char_list, 0), do: true
|
||||
defp elixir_builtin_type?(:keyword, 0), do: true
|
||||
defp elixir_builtin_type?(:keyword, 1), do: true
|
||||
defp elixir_builtin_type?(_, _), do: false
|
||||
|
||||
@doc false
|
||||
@@ -440,9 +419,32 @@ defmodule Kernel.Typespec do
|
||||
translate_spec(kind, spec, [], caller)
|
||||
end
|
||||
|
||||
defp translate_spec(kind, {:::, meta, [{name, _, args}, return]}, guard, caller)
|
||||
when is_atom(name) and name != ::: do
|
||||
translate_spec(kind, meta, name, args, return, guard, caller)
|
||||
defp translate_spec(kind, {:::, meta, [{name, _, args}, return]}, guard, caller) when is_atom(name) and name != ::: do
|
||||
if is_atom(args), do: args = []
|
||||
|
||||
if kind == :macrocallback do
|
||||
kind = :callback
|
||||
name = :"MACRO-#{name}"
|
||||
args = [quote(do: env :: Macro.Env.t)|args]
|
||||
end
|
||||
|
||||
ensure_no_defaults! args
|
||||
|
||||
unless Keyword.keyword?(guard) do
|
||||
guard = Macro.to_string(guard)
|
||||
compile_error caller, "expected keywords as guard in type specification, got: #{guard}"
|
||||
end
|
||||
|
||||
vars = Keyword.keys(guard)
|
||||
constraints = guard_to_constraints(guard, vars, meta, caller)
|
||||
|
||||
spec = {:type, line(meta), :fun, fn_args(meta, args, return, vars, caller)}
|
||||
if constraints != [] do
|
||||
spec = {:type, line(meta), :bounded_fun, [spec, constraints]}
|
||||
end
|
||||
|
||||
arity = length(args)
|
||||
{{kind, {name, arity}, spec}, caller.line}
|
||||
end
|
||||
|
||||
defp translate_spec(_kind, {name, _meta, _args} = spec, _guard, caller) when is_atom(name) and name != ::: do
|
||||
@@ -455,35 +457,6 @@ defmodule Kernel.Typespec do
|
||||
compile_error caller, "invalid type specification: #{spec}"
|
||||
end
|
||||
|
||||
defp translate_spec(kind, meta, name, args, return, guard, caller) when is_atom(args),
|
||||
do: translate_spec(kind, meta, name, [], return, guard, caller)
|
||||
defp translate_spec(:macrocallback, meta, name, args, return, guard, caller),
|
||||
do: translate_spec(:callback, meta, :"MACRO-#{name}", macro_args(args), return, guard, caller)
|
||||
defp translate_spec(kind, meta, name, args, return, guard, caller) do
|
||||
ensure_no_defaults!(args)
|
||||
|
||||
unless Keyword.keyword?(guard) do
|
||||
compile_error caller, "expected keywords as guard in type specification, " <>
|
||||
"got: #{Macro.to_string(guard)}"
|
||||
end
|
||||
|
||||
vars = Keyword.keys(guard)
|
||||
spec = {:type, line(meta), :fun, fn_args(meta, args, return, vars, caller)}
|
||||
|
||||
spec =
|
||||
case guard_to_constraints(guard, vars, meta, caller) do
|
||||
[] -> spec
|
||||
constraints -> {:type, line(meta), :bounded_fun, [spec, constraints]}
|
||||
end
|
||||
|
||||
arity = length(args)
|
||||
{{kind, {name, arity}, spec}, caller.line}
|
||||
end
|
||||
|
||||
defp macro_args(args) do
|
||||
[quote(do: {line :: Macro.Env.line, env :: Macro.Env.t}) | args]
|
||||
end
|
||||
|
||||
defp ensure_no_defaults!(args) do
|
||||
:lists.foreach fn
|
||||
{:::, _, [left, right]} ->
|
||||
@@ -511,7 +484,7 @@ defmodule Kernel.Typespec do
|
||||
{name, type}, acc ->
|
||||
constraint = [{:atom, line, :is_subtype}, [{:var, line, name}, typespec(type, vars, caller)]]
|
||||
type = {:type, line, :constraint, constraint}
|
||||
[type | acc]
|
||||
[type|acc]
|
||||
end, [], guard) |> :lists.reverse
|
||||
end
|
||||
|
||||
@@ -587,14 +560,12 @@ defmodule Kernel.Typespec do
|
||||
|
||||
defp typespec_to_ast({:type, line, :map, fields}) do
|
||||
fields = Enum.map fields, fn
|
||||
{:type, _, :map_field_assoc, :any} ->
|
||||
{{:optional, [], [{:any, [], []}]}, {:any, [], []}}
|
||||
{:type, _, :map_field_exact, [{:atom, _, k}, v]} ->
|
||||
{k, typespec_to_ast(v)}
|
||||
{:type, _, :map_field_exact, [k, v]} ->
|
||||
{{:required, [], [typespec_to_ast(k)]}, typespec_to_ast(v)}
|
||||
# OTP 18
|
||||
{:type, _, :map_field_assoc, [k, v]} ->
|
||||
{{:optional, [], [typespec_to_ast(k)]}, typespec_to_ast(v)}
|
||||
{typespec_to_ast(k), typespec_to_ast(v)}
|
||||
# OTP 17
|
||||
{:type, _, :map_field_assoc, k, v} ->
|
||||
{typespec_to_ast(k), typespec_to_ast(v)}
|
||||
end
|
||||
|
||||
{struct, fields} = Keyword.pop(fields, :__struct__)
|
||||
@@ -609,13 +580,13 @@ defmodule Kernel.Typespec do
|
||||
|
||||
defp typespec_to_ast({:type, line, :binary, [arg1, arg2]}) do
|
||||
[arg1, arg2] = for arg <- [arg1, arg2], do: typespec_to_ast(arg)
|
||||
case {typespec_to_ast(arg1), typespec_to_ast(arg2)} do
|
||||
{arg1, 0} ->
|
||||
cond do
|
||||
arg2 == 0 ->
|
||||
quote line: line, do: <<_ :: unquote(arg1)>>
|
||||
{0, arg2} ->
|
||||
arg1 == 0 ->
|
||||
quote line: line, do: <<_ :: _ * unquote(arg2)>>
|
||||
{arg1, arg2} ->
|
||||
quote line: line, do: <<_ :: unquote(arg1), _ :: _ * unquote(arg2)>>
|
||||
true ->
|
||||
quote line: line, do: <<_ :: unquote(arg1) * unquote(arg2)>>
|
||||
end
|
||||
end
|
||||
|
||||
@@ -659,10 +630,8 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
# Special shortcut(s)
|
||||
# TODO: Deprecate char_list type by v1.5
|
||||
defp typespec_to_ast({:remote_type, line, [{:atom, _, :elixir}, {:atom, _, type}, []]})
|
||||
when type in [:charlist, :char_list] do
|
||||
typespec_to_ast({:type, line, :charlist, []})
|
||||
defp typespec_to_ast({:remote_type, line, [{:atom, _, :elixir}, {:atom, _, :char_list}, []]}) do
|
||||
typespec_to_ast({:type, line, :char_list, []})
|
||||
end
|
||||
|
||||
defp typespec_to_ast({:remote_type, line, [{:atom, _, :elixir}, {:atom, _, :struct}, []]}) do
|
||||
@@ -673,10 +642,6 @@ defmodule Kernel.Typespec do
|
||||
typespec_to_ast({:type, line, :as_boolean, [arg]})
|
||||
end
|
||||
|
||||
defp typespec_to_ast({:remote_type, line, [{:atom, _, :elixir}, {:atom, _, :keyword}, args]}) do
|
||||
typespec_to_ast({:type, line, :keyword, args})
|
||||
end
|
||||
|
||||
defp typespec_to_ast({:remote_type, line, [mod, name, args]}) do
|
||||
args = for arg <- args, do: typespec_to_ast(arg)
|
||||
dot = {:., [line: line], [typespec_to_ast(mod), typespec_to_ast(name)]}
|
||||
@@ -742,16 +707,16 @@ defmodule Kernel.Typespec do
|
||||
{:type, line(meta), :binary, [{:integer, line(meta), 0}, {:integer, line(unit_meta), unit}]}
|
||||
end
|
||||
|
||||
defp typespec({:<<>>, meta, [{:::, shared_meta, [{:_, _, ctx}, {:*, _, [size, unit]}]}]}, _, _)
|
||||
when is_atom(ctx) and is_integer(unit) and is_integer(size) do
|
||||
{:type, line(meta), :binary, [{:integer, line(shared_meta), size}, {:integer, line(shared_meta), unit}]}
|
||||
end
|
||||
|
||||
defp typespec({:<<>>, meta, [{:::, size_meta, [{:_, _, ctx}, size]}]}, _, _)
|
||||
when is_atom(ctx) and is_integer(size) do
|
||||
{:type, line(meta), :binary, [{:integer, line(size_meta), size}, {:integer, line(meta), 0}]}
|
||||
end
|
||||
|
||||
defp typespec({:<<>>, meta, [{:::, size_meta, [{:_, _, ctx1}, size]}, {:::, unit_meta, [{:_, _, ctx2}, {:*, _, [{:_, _, ctx3}, unit]}]}]}, _, _)
|
||||
when is_atom(ctx1) and is_atom(ctx2) and is_atom(ctx3) and is_integer(size) and is_integer(unit) do
|
||||
{:type, line(meta), :binary, [{:integer, line(size_meta), size}, {:integer, line(unit_meta), unit}]}
|
||||
end
|
||||
|
||||
## Handle maps and structs
|
||||
defp typespec({:map, meta, args}, _vars, _caller) when args == [] or is_atom(args) do
|
||||
{:type, line(meta), :map, :any}
|
||||
@@ -760,23 +725,11 @@ defmodule Kernel.Typespec do
|
||||
defp typespec({:%{}, meta, fields} = map, vars, caller) do
|
||||
fields =
|
||||
:lists.map(fn
|
||||
{k, v} when is_atom(k) ->
|
||||
{:type, line(meta), :map_field_exact, [typespec(k, vars, caller), typespec(v, vars, caller)]}
|
||||
{{:required, meta2, [k]}, v} ->
|
||||
{:type, line(meta2), :map_field_exact, [typespec(k, vars, caller), typespec(v, vars, caller)]}
|
||||
{{:optional, meta2, [k]}, v} ->
|
||||
{:type, line(meta2), :map_field_assoc, [typespec(k, vars, caller), typespec(v, vars, caller)]}
|
||||
{k, v} ->
|
||||
# TODO: Emit warnings on v1.5
|
||||
# :elixir_errors.warn(caller.line, caller.file,
|
||||
# "invalid map specification. %{foo => bar} is deprecated in favor of " <>
|
||||
# "%{required(foo) => bar} and %{optional(foo) => bar}. required/1 is an " <>
|
||||
# "OTP 19 only feature, if you are targeting OTP 18 use optional/1.")
|
||||
{:type, line(meta), :map_field_assoc, [typespec(k, vars, caller), typespec(v, vars, caller)]}
|
||||
{:|, _, [_, _]} ->
|
||||
compile_error(caller,
|
||||
"invalid map specification. When using the | operator in the map key, " <>
|
||||
"make sure to wrap the key type in parentheses: #{Macro.to_string(map)}")
|
||||
compile_error(caller, "invalid map specification. When using the | operator in the map key, " <>
|
||||
"make sure to wrap the key type in parentheses: #{Macro.to_string(map)}")
|
||||
_ ->
|
||||
compile_error(caller, "invalid map specification: #{Macro.to_string(map)}")
|
||||
end, fields)
|
||||
@@ -785,8 +738,6 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
defp typespec({:%, _, [name, {:%{}, meta, fields}]}, vars, caller) do
|
||||
# We cannot set a function name to avoid tracking
|
||||
# as a compile time dependency, because for structs it actually is one.
|
||||
module = Macro.expand(name, caller)
|
||||
|
||||
struct =
|
||||
@@ -823,8 +774,6 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
defp typespec({:record, meta, [atom, fields]}, vars, caller) do
|
||||
# We cannot set a function name to avoid tracking
|
||||
# as a compile time dependency because for records it actually is one.
|
||||
case Macro.expand({atom, [], [{atom, [], []}]}, caller) do
|
||||
keyword when is_list(keyword) ->
|
||||
types =
|
||||
@@ -838,7 +787,7 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
end, fields)
|
||||
|
||||
typespec({:{}, meta, [atom | types]}, vars, caller)
|
||||
typespec({:{}, meta, [atom|types]}, vars, caller)
|
||||
_ ->
|
||||
compile_error(caller, "unknown record #{inspect atom}")
|
||||
end
|
||||
@@ -870,7 +819,7 @@ defmodule Kernel.Typespec do
|
||||
|
||||
# Handle type operator
|
||||
defp typespec({:::, meta, [var, expr]}, vars, caller) do
|
||||
left = typespec(var, [elem(var, 0) | vars], caller)
|
||||
left = typespec(var, [elem(var, 0)|vars], caller)
|
||||
right = typespec(expr, vars, caller)
|
||||
{:ann_type, line(meta), [left, right]}
|
||||
end
|
||||
@@ -880,24 +829,9 @@ defmodule Kernel.Typespec do
|
||||
{:op, line(meta), op, {:integer, line(meta), integer}}
|
||||
end
|
||||
|
||||
# Handle remote calls in the form of @module_attribute.type.
|
||||
# These are not handled by the general remote type clause as calling
|
||||
# Macro.expand/2 on the remote does not expand module attributes (but expands
|
||||
# things like __MODULE__).
|
||||
defp typespec({{:., meta, [{:@, _, [{attr, _, _}]}, name]}, _, args} = orig, vars, caller) do
|
||||
remote = Module.get_attribute(caller.module, attr)
|
||||
unless is_atom(remote) and remote != nil do
|
||||
message = "invalid remote in typespec: #{Macro.to_string(orig)} (@#{attr} is #{inspect remote})"
|
||||
compile_error(caller, message)
|
||||
end
|
||||
remote_type({typespec(remote, vars, caller), meta, typespec(name, vars, caller), args}, vars, caller)
|
||||
end
|
||||
|
||||
# Handle remote calls
|
||||
defp typespec({{:., meta, [remote, name]}, _, args} = orig, vars, caller) do
|
||||
# We set a function name to avoid tracking
|
||||
# aliases in typespecs as compile time dependencies.
|
||||
remote = Macro.expand(remote, %{caller | function: {:typespec, 0}})
|
||||
remote = Macro.expand remote, caller
|
||||
unless is_atom(remote) do
|
||||
compile_error(caller, "invalid remote in typespec: #{Macro.to_string(orig)}")
|
||||
end
|
||||
@@ -935,14 +869,13 @@ defmodule Kernel.Typespec do
|
||||
# Handle local calls
|
||||
defp typespec({type, meta, arguments}, vars, caller) when type in [:string, :nonempty_string] do
|
||||
:elixir_errors.warn caller.line, caller.file, "#{type}() type use is discouraged. For character lists, use " <>
|
||||
"charlist() type, for strings, String.t()\n#{Exception.format_stacktrace(Macro.Env.stacktrace(caller))}"
|
||||
"char_list() type, for strings, String.t()\n#{Exception.format_stacktrace(Macro.Env.stacktrace(caller))}"
|
||||
arguments = for arg <- arguments, do: typespec(arg, vars, caller)
|
||||
{:type, line(meta), type, arguments}
|
||||
end
|
||||
|
||||
# TODO: Deprecate char_list type by v1.5
|
||||
defp typespec({type, _meta, []}, vars, caller) when type in [:charlist, :char_list] do
|
||||
typespec((quote do: :elixir.charlist()), vars, caller)
|
||||
defp typespec({:char_list, _meta, []}, vars, caller) do
|
||||
typespec((quote do: :elixir.char_list()), vars, caller)
|
||||
end
|
||||
|
||||
defp typespec({:struct, _meta, []}, vars, caller) do
|
||||
@@ -953,10 +886,6 @@ defmodule Kernel.Typespec do
|
||||
typespec((quote do: :elixir.as_boolean(unquote(arg))), vars, caller)
|
||||
end
|
||||
|
||||
defp typespec({:keyword, _meta, args}, vars, caller) when length(args) <= 1 do
|
||||
typespec((quote do: :elixir.keyword(unquote_splicing(args))), vars, caller)
|
||||
end
|
||||
|
||||
defp typespec({:fun, meta, args}, vars, caller) do
|
||||
args = for arg <- args, do: typespec(arg, vars, caller)
|
||||
{:type, line(meta), :fun, args}
|
||||
@@ -995,7 +924,7 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
defp typespec(list, vars, caller) when is_list(list) do
|
||||
[h | t] = :lists.reverse(list)
|
||||
[h|t] = :lists.reverse(list)
|
||||
union = :lists.foldl(fn(x, acc) ->
|
||||
{:|, [], [validate_kw(x, list, caller), acc]}
|
||||
end, validate_kw(h, list, caller), t)
|
||||
@@ -1017,7 +946,7 @@ defmodule Kernel.Typespec do
|
||||
{:remote_type, line(meta), [ remote, name, arguments ]}
|
||||
end
|
||||
|
||||
defp collect_union({:|, _, [a, b]}), do: [a | collect_union(b)]
|
||||
defp collect_union({:|, _, [a, b]}), do: [a|collect_union(b)]
|
||||
defp collect_union(v), do: [v]
|
||||
|
||||
defp validate_kw({key, _} = t, _, _caller) when is_atom(key), do: t
|
||||
@@ -1045,8 +974,8 @@ defmodule Kernel.Typespec do
|
||||
{:var, line(meta), name}
|
||||
end
|
||||
|
||||
defp unpack_typespec_kw([{:type, _, :tuple, [{:atom, _, atom}, type]} | t], acc) do
|
||||
unpack_typespec_kw(t, [{atom, typespec_to_ast(type)} | acc])
|
||||
defp unpack_typespec_kw([{:type, _, :tuple, [{:atom, _, atom}, type]}|t], acc) do
|
||||
unpack_typespec_kw(t, [{atom, typespec_to_ast(type)}|acc])
|
||||
end
|
||||
|
||||
defp unpack_typespec_kw([], acc) do
|
||||
|
||||
@@ -3,26 +3,17 @@ import Kernel, except: [destructure: 2, defdelegate: 2, defstruct: 2]
|
||||
defmodule Kernel.Utils do
|
||||
@moduledoc false
|
||||
|
||||
@doc """
|
||||
Callback for destructure.
|
||||
"""
|
||||
def destructure(list, count) when is_list(list) and is_integer(count) and count >= 0,
|
||||
do: destructure_list(list, count)
|
||||
def destructure(nil, count) when is_integer(count) and count >= 0,
|
||||
do: destructure_nil(count)
|
||||
def destructure(list, count) when is_list(list), do: destructure_list(list, count)
|
||||
def destructure(nil, count), do: destructure_nil(count)
|
||||
|
||||
defp destructure_list(_, 0), do: []
|
||||
defp destructure_list([], count), do: destructure_nil(count)
|
||||
defp destructure_list([h | t], count), do: [h | destructure_list(t, count - 1)]
|
||||
defp destructure_list([h|t], count), do: [h|destructure_list(t, count - 1)]
|
||||
|
||||
defp destructure_nil(0), do: []
|
||||
defp destructure_nil(count), do: [nil | destructure_nil(count - 1)]
|
||||
defp destructure_nil(count), do: [nil|destructure_nil(count - 1)]
|
||||
|
||||
@doc """
|
||||
Callback for defdelegate.
|
||||
"""
|
||||
def defdelegate(fun, opts) when is_list(opts) do
|
||||
# TODO: Remove by 2.0
|
||||
def defdelegate(fun, opts, env) do
|
||||
append_first = Keyword.get(opts, :append_first, false)
|
||||
|
||||
{name, args} =
|
||||
@@ -31,47 +22,32 @@ defmodule Kernel.Utils do
|
||||
_ -> raise ArgumentError, "invalid syntax in defdelegate #{Macro.to_string(fun)}"
|
||||
end
|
||||
|
||||
as_args_list = normalize_args(args)
|
||||
as = Keyword.get(opts, :as, name)
|
||||
:lists.map(fn as_args ->
|
||||
formal_args = make_formal_args(as_args)
|
||||
as_args = case append_first do
|
||||
true -> tl(as_args) ++ [hd(as_args)]
|
||||
false -> as_args
|
||||
:ok = check_defdelegate_args(args, env)
|
||||
|
||||
as_args =
|
||||
case append_first and args != [] do
|
||||
true -> tl(args) ++ [hd(args)]
|
||||
false -> args
|
||||
end
|
||||
{name, formal_args, as, as_args}
|
||||
end, as_args_list)
|
||||
|
||||
as = Keyword.get(opts, :as, name)
|
||||
{name, args, as, as_args}
|
||||
end
|
||||
|
||||
defp make_formal_args(args) do
|
||||
fun = &match?({name, _, mod} when is_atom(name) and is_atom(mod), &1)
|
||||
:lists.filter(fun, args)
|
||||
# TODO: Convert this to an error on 1.3
|
||||
defp check_defdelegate_args([], _env),
|
||||
do: :ok
|
||||
defp check_defdelegate_args([{var, _, mod}|rest], env) when is_atom(var) and is_atom(mod),
|
||||
do: check_defdelegate_args(rest, env)
|
||||
defp check_defdelegate_args([code|_], env) do
|
||||
:elixir_errors.warn(env.line, env.file,
|
||||
"defdelegate/2 will only accept variable names in upcoming versions, " <>
|
||||
"got: #{Macro.to_string(code)}")
|
||||
end
|
||||
|
||||
defp normalize_args(raw_args) do
|
||||
:lists.foldr(fn
|
||||
({:\\, _, [arg, default_arg]}, [as_args | _] = as_args_list) ->
|
||||
new_as_args = [default_arg | as_args]
|
||||
[new_as_args | add_arg(as_args_list, arg)]
|
||||
(arg, as_args_list) ->
|
||||
add_arg(as_args_list, arg)
|
||||
end, [[]], raw_args)
|
||||
end
|
||||
|
||||
defp add_arg(as_args_list, {name, _, mod} = arg) when is_atom(name) and is_atom(mod),
|
||||
do: :lists.map(&([arg | &1]), as_args_list)
|
||||
defp add_arg(_, code) do
|
||||
raise ArgumentError,
|
||||
"defdelegate/2 only accepts function parameters, got: #{Macro.to_string(code)}"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Callback for defstruct.
|
||||
"""
|
||||
def defstruct(module, fields) do
|
||||
case fields do
|
||||
fs when is_list(fs) ->
|
||||
:ok
|
||||
fs when is_list(fs) -> :ok
|
||||
other ->
|
||||
raise ArgumentError, "struct fields definition must be list, got: #{inspect other}"
|
||||
end
|
||||
@@ -92,35 +68,6 @@ defmodule Kernel.Utils do
|
||||
raise ArgumentError, "struct field names must be atoms, got: #{inspect other}"
|
||||
end, fields)
|
||||
|
||||
{:maps.put(:__struct__, module, :maps.from_list(fields)),
|
||||
List.wrap(Module.get_attribute(module, :enforce_keys)),
|
||||
Module.get_attribute(module, :derive)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Announcing callback for defstruct.
|
||||
"""
|
||||
def announce_struct(module) do
|
||||
case :erlang.get(:elixir_compiler_pid) do
|
||||
:undefined -> :ok
|
||||
pid -> send(pid, {:struct_available, module})
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Callback for raise.
|
||||
"""
|
||||
def raise(msg) when is_binary(msg) do
|
||||
RuntimeError.exception(msg)
|
||||
end
|
||||
def raise(atom) when is_atom(atom) do
|
||||
atom.exception([])
|
||||
end
|
||||
def raise(%{__struct__: struct, __exception__: true} = exception) when is_atom(struct) do
|
||||
exception
|
||||
end
|
||||
def raise(other) do
|
||||
ArgumentError.exception("raise/1 expects an alias, string or exception as " <>
|
||||
"the first argument, got: #{inspect other}")
|
||||
:maps.put(:__struct__, module, :maps.from_list(fields))
|
||||
end
|
||||
end
|
||||
|
||||
+57
-118
@@ -2,12 +2,12 @@ defmodule Keyword do
|
||||
@moduledoc """
|
||||
A set of functions for working with keywords.
|
||||
|
||||
A keyword is a list of two-element tuples where the first
|
||||
A keyword is a list of 2-element tuples where the first
|
||||
element of the tuple is an atom and the second element
|
||||
can be any value.
|
||||
|
||||
A keyword may have duplicated keys so it is not strictly
|
||||
a key-value store. However most of the functions in this module
|
||||
a dictionary. However most of the functions in this module
|
||||
behave exactly as a dictionary so they work similarly to
|
||||
the functions you would find in the `Map` module.
|
||||
|
||||
@@ -25,7 +25,7 @@ defmodule Keyword do
|
||||
The functions in Keyword do not guarantee any property when
|
||||
it comes to ordering. However, since a keyword list is simply a
|
||||
list, all the operations defined in `Enum` and `List` can be
|
||||
applied too, especially when ordering is required.
|
||||
applied too, specially when ordering is required.
|
||||
"""
|
||||
|
||||
@compile :inline_list_funcs
|
||||
@@ -71,7 +71,7 @@ defmodule Keyword do
|
||||
[]
|
||||
|
||||
"""
|
||||
@spec new :: []
|
||||
@spec new :: t
|
||||
def new, do: []
|
||||
|
||||
@doc """
|
||||
@@ -104,7 +104,7 @@ defmodule Keyword do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.new([:a, :b], fn(x) -> {x, x} end)
|
||||
iex> Keyword.new([:a, :b], fn (x) -> {x, x} end)
|
||||
[a: :a, b: :b]
|
||||
|
||||
"""
|
||||
@@ -191,14 +191,12 @@ defmodule Keyword do
|
||||
Gets the value from `key` and updates it, all in one pass.
|
||||
|
||||
This `fun` argument receives the value of `key` (or `nil` if `key`
|
||||
is not present) and must return a two-element tuple: the "get" value
|
||||
(the retrieved value, which can be operated on before being returned)
|
||||
and the new value to be stored under `key`. The `fun` may also
|
||||
return `:pop`, implying the current value shall be removed from the
|
||||
keyword list and returned.
|
||||
is not present) and must return a two-elements tuple: the "get" value (the
|
||||
retrieved value, which can be operated on before being returned) and the new
|
||||
value to be stored under `key`.
|
||||
|
||||
The returned value is a tuple with the "get" value returned by
|
||||
`fun` and a new keyword list with the updated value under `key`.
|
||||
The returned value is a tuple with the "get" value returned by `fun` and a new
|
||||
keyword list with the updated value under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -212,48 +210,30 @@ defmodule Keyword do
|
||||
...> end)
|
||||
{nil, [b: "new value!", a: 1]}
|
||||
|
||||
iex> Keyword.get_and_update([a: 1], :a, fn _ -> :pop end)
|
||||
{1, []}
|
||||
|
||||
iex> Keyword.get_and_update([a: 1], :b, fn _ -> :pop end)
|
||||
{nil, [a: 1]}
|
||||
|
||||
"""
|
||||
@spec get_and_update(t, key, (value -> {get, value} | :pop)) :: {get, t} when get: term
|
||||
@spec get_and_update(t, key, (value -> {get, value})) :: {get, t} when get: term
|
||||
def get_and_update(keywords, key, fun)
|
||||
when is_list(keywords) and is_atom(key),
|
||||
do: get_and_update(keywords, [], key, fun)
|
||||
|
||||
defp get_and_update([{key, current} | t], acc, key, fun) do
|
||||
case fun.(current) do
|
||||
{get, value} ->
|
||||
{get, :lists.reverse(acc, [{key, value} | t])}
|
||||
:pop ->
|
||||
{current, :lists.reverse(acc, t)}
|
||||
other ->
|
||||
raise "the given function must return a two-element tuple or :pop, got: #{inspect(other)}"
|
||||
end
|
||||
defp get_and_update([{key, value}|t], acc, key, fun) do
|
||||
{get, new_value} = fun.(value)
|
||||
{get, :lists.reverse(acc, [{key, new_value}|t])}
|
||||
end
|
||||
|
||||
defp get_and_update([{_, _} = h | t], acc, key, fun),
|
||||
do: get_and_update(t, [h | acc], key, fun)
|
||||
defp get_and_update([h|t], acc, key, fun),
|
||||
do: get_and_update(t, [h|acc], key, fun)
|
||||
|
||||
defp get_and_update([], acc, key, fun) do
|
||||
case fun.(nil) do
|
||||
{get, update} ->
|
||||
{get, [{key, update} | :lists.reverse(acc)]}
|
||||
:pop ->
|
||||
{nil, :lists.reverse(acc)}
|
||||
other ->
|
||||
raise "the given function must return a two-element tuple or :pop, got: #{inspect(other)}"
|
||||
end
|
||||
{get, update} = fun.(nil)
|
||||
{get, [{key, update}|:lists.reverse(acc)]}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value from `key` and updates it. Raises if there is no `key`.
|
||||
|
||||
This `fun` argument receives the value of `key` and must return a
|
||||
two-element tuple: the "get" value (the retrieved value, which can be
|
||||
two-elements tuple: the "get" value (the retrieved value, which can be
|
||||
operated on before being returned) and the new value to be stored under
|
||||
`key`.
|
||||
|
||||
@@ -262,7 +242,7 @@ defmodule Keyword do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.get_and_update!([a: 1], :a, fn current_value ->
|
||||
iex> Keyword.get_and_update!([a: 1], :a, fn(current_value) ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{1, [a: "new value!"]}
|
||||
@@ -272,30 +252,19 @@ defmodule Keyword do
|
||||
...> end)
|
||||
** (KeyError) key :b not found in: [a: 1]
|
||||
|
||||
iex> Keyword.get_and_update!([a: 1], :a, fn _ ->
|
||||
...> :pop
|
||||
...> end)
|
||||
{1, []}
|
||||
|
||||
"""
|
||||
@spec get_and_update!(t, key, (value -> {get, value})) :: {get, t} | no_return when get: term
|
||||
def get_and_update!(keywords, key, fun) do
|
||||
get_and_update!(keywords, key, fun, [])
|
||||
end
|
||||
|
||||
defp get_and_update!([{key, value} | keywords], key, fun, acc) do
|
||||
case fun.(value) do
|
||||
{get, value} ->
|
||||
{get, :lists.reverse(acc, [{key, value} | delete(keywords, key)])}
|
||||
:pop ->
|
||||
{value, :lists.reverse(acc, keywords)}
|
||||
other ->
|
||||
raise "the given function must return a two-element tuple or :pop, got: #{inspect(other)}"
|
||||
end
|
||||
defp get_and_update!([{key, value}|keywords], key, fun, acc) do
|
||||
{get, value} = fun.(value)
|
||||
{get, :lists.reverse(acc, [{key, value}|delete(keywords, key)])}
|
||||
end
|
||||
|
||||
defp get_and_update!([{_, _} = e | keywords], key, fun, acc) do
|
||||
get_and_update!(keywords, key, fun, [e | acc])
|
||||
defp get_and_update!([{_, _} = e|keywords], key, fun, acc) do
|
||||
get_and_update!(keywords, key, fun, [e|acc])
|
||||
end
|
||||
|
||||
defp get_and_update!([], key, _fun, acc) when is_atom(key) do
|
||||
@@ -360,7 +329,7 @@ defmodule Keyword do
|
||||
@spec get_values(t, key) :: [value]
|
||||
def get_values(keywords, key) when is_list(keywords) and is_atom(key) do
|
||||
fun = fn
|
||||
{^key, val} -> {true, val}
|
||||
{k, v} when k === key -> {true, v}
|
||||
{_, _} -> false
|
||||
end
|
||||
:lists.filtermap(fun, keywords)
|
||||
@@ -482,7 +451,7 @@ defmodule Keyword do
|
||||
"""
|
||||
@spec put(t, key, value) :: t
|
||||
def put(keywords, key, value) when is_list(keywords) and is_atom(key) do
|
||||
[{key, value} | delete(keywords, key)]
|
||||
[{key, value}|delete(keywords, key)]
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -510,7 +479,7 @@ defmodule Keyword do
|
||||
when is_list(keywords) and is_atom(key) and is_function(fun, 0) do
|
||||
case :lists.keyfind(key, 1, keywords) do
|
||||
{^key, _} -> keywords
|
||||
false -> [{key, fun.()} | keywords]
|
||||
false -> [{key, fun.()}|keywords]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -530,7 +499,7 @@ defmodule Keyword do
|
||||
def put_new(keywords, key, value) when is_list(keywords) and is_atom(key) do
|
||||
case :lists.keyfind(key, 1, keywords) do
|
||||
{^key, _} -> keywords
|
||||
false -> [{key, value} | keywords]
|
||||
false -> [{key, value}|keywords]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -571,23 +540,11 @@ defmodule Keyword do
|
||||
iex> Keyword.merge([a: 1, b: 2], [a: 3, d: 4, a: 5])
|
||||
[b: 2, a: 3, d: 4, a: 5]
|
||||
|
||||
iex> Keyword.merge([a: 1], [2, 3])
|
||||
** (ArgumentError) expected a keyword list as the second argument, got: [2, 3]
|
||||
|
||||
"""
|
||||
@spec merge(t, t) :: t
|
||||
def merge(keywords1, keywords2) when is_list(keywords1) and is_list(keywords2) do
|
||||
if keyword?(keywords2) do
|
||||
fun = fn
|
||||
{key, _value} when is_atom(key) ->
|
||||
not has_key?(keywords2, key)
|
||||
_ ->
|
||||
raise ArgumentError, message: "expected a keyword list as the first argument, got: #{inspect keywords1}"
|
||||
end
|
||||
:lists.filter(fun, keywords1) ++ keywords2
|
||||
else
|
||||
raise ArgumentError, message: "expected a keyword list as the second argument, got: #{inspect keywords2}"
|
||||
end
|
||||
fun = fn {k, _v} -> not has_key?(keywords2, k) end
|
||||
:lists.filter(fun, keywords1) ++ keywords2
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -618,40 +575,26 @@ defmodule Keyword do
|
||||
...> end)
|
||||
[b: 2, a: 4, d: 4, a: 8]
|
||||
|
||||
iex> Keyword.merge([a: 1, b: 2], [:a, :b], fn :a, v1, v2 ->
|
||||
...> v1 + v2
|
||||
...> end)
|
||||
** (ArgumentError) expected a keyword list as the second argument, got: [:a, :b]
|
||||
|
||||
"""
|
||||
@spec merge(t, t, (key, value, value -> value)) :: t
|
||||
def merge(keywords1, keywords2, fun) when is_list(keywords1) and is_list(keywords2) and is_function(fun, 3) do
|
||||
if keyword?(keywords1) do
|
||||
do_merge(keywords2, [], keywords1, keywords1, fun, keywords2)
|
||||
else
|
||||
raise ArgumentError, message: "expected a keyword list as the first argument, got: #{inspect keywords1}"
|
||||
end
|
||||
def merge(keywords1, keywords2, fun) when is_list(keywords1) and is_list(keywords2) do
|
||||
do_merge(keywords2, [], keywords1, keywords1, fun)
|
||||
end
|
||||
|
||||
defp do_merge([{key, value2} | tail], acc, rest, original, fun, keywords2) when is_atom(key) do
|
||||
case :lists.keyfind(key, 1, original) do
|
||||
{^key, value1} ->
|
||||
do_merge(tail, [{key, fun.(key, value1, value2)} | acc],
|
||||
delete(rest, key), :lists.keydelete(key, 1, original), fun, keywords2)
|
||||
|
||||
defp do_merge([{k, v2}|t], acc, rest, original, fun) do
|
||||
case :lists.keyfind(k, 1, original) do
|
||||
{^k, v1} ->
|
||||
do_merge(t, [{k, fun.(k, v1, v2)}|acc],
|
||||
delete(rest, k), :lists.keydelete(k, 1, original), fun)
|
||||
false ->
|
||||
do_merge(tail, [{key, value2} | acc], rest, original, fun, keywords2)
|
||||
do_merge(t, [{k, v2}|acc], rest, original, fun)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_merge([], acc, rest, _original, _fun, _keywords2) do
|
||||
defp do_merge([], acc, rest, _original, _fun) do
|
||||
rest ++ :lists.reverse(acc)
|
||||
end
|
||||
|
||||
defp do_merge(_other, _acc, _rest, _original, _fun, keywords2) do
|
||||
raise ArgumentError, message: "expected a keyword list as the second argument, got: #{inspect keywords2}"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns whether a given `key` exists in the given `keywords`.
|
||||
|
||||
@@ -692,12 +635,12 @@ defmodule Keyword do
|
||||
update!(keywords, key, fun, keywords)
|
||||
end
|
||||
|
||||
defp update!([{key, value} | keywords], key, fun, _dict) do
|
||||
[{key, fun.(value)} | delete(keywords, key)]
|
||||
defp update!([{key, value}|keywords], key, fun, _dict) do
|
||||
[{key, fun.(value)}|delete(keywords, key)]
|
||||
end
|
||||
|
||||
defp update!([{_, _} = e | keywords], key, fun, dict) do
|
||||
[e | update!(keywords, key, fun, dict)]
|
||||
defp update!([{_, _} = e|keywords], key, fun, dict) do
|
||||
[e|update!(keywords, key, fun, dict)]
|
||||
end
|
||||
|
||||
defp update!([], key, _fun, dict) when is_atom(key) do
|
||||
@@ -725,12 +668,12 @@ defmodule Keyword do
|
||||
@spec update(t, key, value, (value -> value)) :: t
|
||||
def update(keywords, key, initial, fun)
|
||||
|
||||
def update([{key, value} | keywords], key, _initial, fun) do
|
||||
[{key, fun.(value)} | delete(keywords, key)]
|
||||
def update([{key, value}|keywords], key, _initial, fun) do
|
||||
[{key, fun.(value)}|delete(keywords, key)]
|
||||
end
|
||||
|
||||
def update([{_, _} = e | keywords], key, initial, fun) do
|
||||
[e | update(keywords, key, initial, fun)]
|
||||
def update([{_, _} = e|keywords], key, initial, fun) do
|
||||
[e|update(keywords, key, initial, fun)]
|
||||
end
|
||||
|
||||
def update([], key, initial, _fun) when is_atom(key) do
|
||||
@@ -743,7 +686,7 @@ defmodule Keyword do
|
||||
|
||||
Returns a tuple with the new list and the old list with removed keys.
|
||||
|
||||
Keys for which there are no entries in the keyword list are ignored.
|
||||
Keys for which there are no entires in the keyword list are ignored.
|
||||
|
||||
Entries with duplicated keys end up in the same keyword list.
|
||||
|
||||
@@ -755,12 +698,11 @@ defmodule Keyword do
|
||||
{[a: 1, c: 3, a: 4], [b: 2]}
|
||||
|
||||
"""
|
||||
@spec split(t, [key]) :: {t, t}
|
||||
def split(keywords, keys) when is_list(keywords) do
|
||||
fun = fn {k, v}, {take, drop} ->
|
||||
case k in keys do
|
||||
true -> {[{k, v} | take], drop}
|
||||
false -> {take, [{k, v} | drop]}
|
||||
true -> {[{k, v}|take], drop}
|
||||
false -> {take, [{k, v}|drop]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -783,7 +725,6 @@ defmodule Keyword do
|
||||
[a: 1, c: 3, a: 5]
|
||||
|
||||
"""
|
||||
@spec take(t, [key]) :: t
|
||||
def take(keywords, keys) when is_list(keywords) do
|
||||
:lists.filter(fn {k, _} -> k in keys end, keywords)
|
||||
end
|
||||
@@ -801,7 +742,6 @@ defmodule Keyword do
|
||||
[a: 1, c: 3, a: 5]
|
||||
|
||||
"""
|
||||
@spec drop(t, [key]) :: t
|
||||
def drop(keywords, keys) when is_list(keywords) do
|
||||
:lists.filter(fn {k, _} -> not k in keys end, keywords)
|
||||
end
|
||||
@@ -874,13 +814,13 @@ defmodule Keyword do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.pop_first([a: 1], :a)
|
||||
iex> Keyword.pop_first [a: 1], :a
|
||||
{1, []}
|
||||
iex> Keyword.pop_first([a: 1], :b)
|
||||
iex> Keyword.pop_first [a: 1], :b
|
||||
{nil, [a: 1]}
|
||||
iex> Keyword.pop_first([a: 1], :b, 3)
|
||||
iex> Keyword.pop_first [a: 1], :b, 3
|
||||
{3, [a: 1]}
|
||||
iex> Keyword.pop_first([a: 1, a: 2], :a)
|
||||
iex> Keyword.pop_first [a: 1, a: 2], :a
|
||||
{1, [a: 2]}
|
||||
|
||||
"""
|
||||
@@ -901,15 +841,14 @@ defmodule Keyword do
|
||||
[a: 1]
|
||||
|
||||
"""
|
||||
@spec to_list(t) :: t
|
||||
def to_list(keyword) when is_list(keyword) do
|
||||
keyword
|
||||
end
|
||||
|
||||
# TODO: Deprecate by 1.3
|
||||
# TODO: Remove by 1.4
|
||||
@doc false
|
||||
# TODO: Remove on 2.0
|
||||
def size(keyword) do
|
||||
IO.warn "Keyword.size/1 is deprecated, please use Kernel.length/1"
|
||||
length(keyword)
|
||||
end
|
||||
end
|
||||
|
||||
+88
-288
@@ -1,80 +1,38 @@
|
||||
defmodule List do
|
||||
@moduledoc """
|
||||
Functions that work on (linked) lists.
|
||||
Specialized functions that only work on lists.
|
||||
|
||||
Lists in Elixir are specified between square brackets:
|
||||
In general, favor using the `Enum` API instead of `List`.
|
||||
|
||||
iex> [1, "two", 3, :four]
|
||||
[1, "two", 3, :four]
|
||||
Index access for list is linear. Negative indexes are also
|
||||
supported but they imply the list will be iterated twice,
|
||||
one to calculate the proper index and another to perform the
|
||||
operation.
|
||||
|
||||
Two lists can be concatenated and subtracted using the
|
||||
`Kernel.++/2` and `Kernel.--/2` operators:
|
||||
A decision was taken to delegate most functions to
|
||||
Erlang's standard library but follow Elixir's convention
|
||||
of receiving the subject (in this case, a list) as the
|
||||
first argument.
|
||||
|
||||
iex> [1, 2, 3] ++ [4, 5, 6]
|
||||
[1, 2, 3, 4, 5, 6]
|
||||
iex> [1, true, 2, false, 3, true] -- [true, false]
|
||||
[1, 2, 3, true]
|
||||
## Char lists
|
||||
|
||||
Lists in Elixir are effectively linked lists, which means
|
||||
they are internally represented in pairs containing the
|
||||
head and the tail of a list:
|
||||
|
||||
iex> [head | tail] = [1, 2, 3]
|
||||
iex> head
|
||||
1
|
||||
iex> tail
|
||||
[2, 3]
|
||||
|
||||
Similarly, we could write the list `[1, 2, 3]` using only
|
||||
such pairs (called cons cells):
|
||||
|
||||
iex> [1 | [2 | [3 | []]]]
|
||||
[1, 2, 3]
|
||||
|
||||
Some lists, called improper lists, do not have an empty list as
|
||||
the second element in the last cons cell:
|
||||
|
||||
iex> [1 | [2 | [3 | 4]]]
|
||||
[1, 2, 3 | 4]
|
||||
|
||||
Although improper lists are generally avoided, they are used in some
|
||||
special circumstances like iodata and chardata entities (see the `IO` module).
|
||||
|
||||
Due to their cons cell based representation, prepending an element
|
||||
to a list is always fast (constant time), while appending becomes
|
||||
slower as the list grows in size (linear time):
|
||||
|
||||
iex> list = [1, 2, 3]
|
||||
iex> [0 | list] # fast
|
||||
[0, 1, 2, 3]
|
||||
iex> list ++ [4] # slow
|
||||
[1, 2, 3, 4]
|
||||
|
||||
The `Kernel` module contains many functions to manipulate lists
|
||||
and that are allowed in guards. For example, `Kernel.hd/1` to
|
||||
retrieve the head, `Kernel.tl/1` to fetch the tail and
|
||||
`Kernel.length/1` for calculating the length. Keep in mind that,
|
||||
similar to appending to a list, calculating the length needs to
|
||||
traverse the whole list.
|
||||
|
||||
## Charlists
|
||||
|
||||
If a list is made of non-negative integers, it can also be called
|
||||
a charlist. Elixir uses single quotes to define charlists:
|
||||
If a list is made of non-negative integers, it can also
|
||||
be called as a char list. Elixir uses single quotes to
|
||||
define char lists:
|
||||
|
||||
iex> 'héllo'
|
||||
[104, 233, 108, 108, 111]
|
||||
|
||||
In particular, charlists may be printed back in single
|
||||
In particular, char lists may be printed back in single
|
||||
quotes if they contain only ASCII-printable codepoints:
|
||||
|
||||
iex> 'abc'
|
||||
'abc'
|
||||
|
||||
The rationale behind this behaviour is to better support
|
||||
Erlang libraries which may return text as charlists
|
||||
Erlang libraries which may return text as char lists
|
||||
instead of Elixir strings. One example of such functions
|
||||
is `Application.loaded_applications/0`:
|
||||
is `Application.loaded_applications`:
|
||||
|
||||
Application.loaded_applications
|
||||
#=> [{:stdlib, 'ERTS CXC 138 10', '2.6'},
|
||||
@@ -82,37 +40,22 @@ defmodule List do
|
||||
{:elixir, 'elixir', '1.0.0'},
|
||||
{:kernel, 'ERTS CXC 138 10', '4.1'},
|
||||
{:logger, 'logger', '1.0.0'}]
|
||||
|
||||
## List and Enum modules
|
||||
|
||||
This module aims to provide operations that are specific
|
||||
to lists, like conversion between data types, updates,
|
||||
deletions and key lookups (for lists of tuples). For traversing
|
||||
lists in general, developers should use the functions in the
|
||||
`Enum` module that work across a variety of data types.
|
||||
|
||||
In both `Enum` and `List` modules, any kind of index access
|
||||
on a list is linear. Negative indexes are also supported but
|
||||
they imply the list will be iterated twice, one to calculate
|
||||
the proper index and another to perform the operation.
|
||||
"""
|
||||
|
||||
@compile :inline_list_funcs
|
||||
|
||||
@doc """
|
||||
Deletes the given `item` from the `list`. Returns a new list without
|
||||
the item.
|
||||
|
||||
If the `item` occurs more than once in the `list`, just
|
||||
Deletes the given item from the list. Returns a list without
|
||||
the item. If the item occurs more than once in the list, just
|
||||
the first occurrence is removed.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.delete([:a, :b, :c], :a)
|
||||
[:b, :c]
|
||||
iex> List.delete([1, 2, 3], 1)
|
||||
[2, 3]
|
||||
|
||||
iex> List.delete([:a, :b, :b, :c], :b)
|
||||
[:a, :b, :c]
|
||||
iex> List.delete([1, 2, 2, 3], 2)
|
||||
[1, 2, 3]
|
||||
|
||||
"""
|
||||
@spec delete(list, any) :: list
|
||||
@@ -131,6 +74,7 @@ defmodule List do
|
||||
iex> List.duplicate([1, 2], 2)
|
||||
[[1, 2], [1, 2]]
|
||||
|
||||
|
||||
"""
|
||||
@spec duplicate(elem, non_neg_integer) :: [elem] when elem: var
|
||||
def duplicate(elem, n) do
|
||||
@@ -173,10 +117,10 @@ defmodule List do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.foldl([5, 5], 10, fn(x, acc) -> x + acc end)
|
||||
iex> List.foldl([5, 5], 10, fn (x, acc) -> x + acc end)
|
||||
20
|
||||
|
||||
iex> List.foldl([1, 2, 3, 4], 0, fn(x, acc) -> x - acc end)
|
||||
iex> List.foldl([1, 2, 3, 4], 0, fn (x, acc) -> x - acc end)
|
||||
2
|
||||
|
||||
"""
|
||||
@@ -191,7 +135,7 @@ defmodule List do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.foldr([1, 2, 3, 4], 0, fn(x, acc) -> x - acc end)
|
||||
iex> List.foldr([1, 2, 3, 4], 0, fn (x, acc) -> x - acc end)
|
||||
-2
|
||||
|
||||
"""
|
||||
@@ -216,8 +160,8 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec first([elem]) :: nil | elem when elem: var
|
||||
def first([]), do: nil
|
||||
def first([head | _]), do: head
|
||||
def first([]), do: nil
|
||||
def first([h|_]), do: h
|
||||
|
||||
@doc """
|
||||
Returns the last element in `list` or `nil` if `list` is empty.
|
||||
@@ -235,9 +179,9 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec last([elem]) :: nil | elem when elem: var
|
||||
def last([]), do: nil
|
||||
def last([head]), do: head
|
||||
def last([_ | tail]), do: last(tail)
|
||||
def last([]), do: nil
|
||||
def last([h]), do: h
|
||||
def last([_|t]), do: last(t)
|
||||
|
||||
@doc """
|
||||
Receives a list of tuples and returns the first tuple
|
||||
@@ -278,7 +222,7 @@ defmodule List do
|
||||
false
|
||||
|
||||
"""
|
||||
@spec keymember?([tuple], any, non_neg_integer) :: boolean
|
||||
@spec keymember?([tuple], any, non_neg_integer) :: any
|
||||
def keymember?(list, key, position) do
|
||||
:lists.keymember(key, position + 1, list)
|
||||
end
|
||||
@@ -317,10 +261,9 @@ defmodule List do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a `list` of tuples and replaces the item
|
||||
identified by `key` at `position`.
|
||||
|
||||
If the item does not exist, it is added to the end of the `list`.
|
||||
Receives a list of tuples and replaces the item
|
||||
identified by `key` at `position`. If the item
|
||||
does not exist, it is added to the end of the list.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -337,7 +280,7 @@ defmodule List do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a `list` of tuples and deletes the first tuple
|
||||
Receives a list of tuples and deletes the first tuple
|
||||
where the item at `position` matches the
|
||||
given `key`. Returns the new list.
|
||||
|
||||
@@ -387,7 +330,6 @@ defmodule List do
|
||||
|
||||
@doc """
|
||||
Wraps the argument in a list.
|
||||
|
||||
If the argument is already a list, returns the list.
|
||||
If the argument is `nil`, returns an empty list.
|
||||
|
||||
@@ -438,9 +380,8 @@ defmodule List do
|
||||
|
||||
@doc """
|
||||
Returns a list with `value` inserted at the specified `index`.
|
||||
|
||||
Note that `index` is capped at the list length. Negative indices
|
||||
indicate an offset from the end of the `list`.
|
||||
indicate an offset from the end of the list.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -458,7 +399,7 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec insert_at(list, integer, any) :: list
|
||||
def insert_at(list, index, value) when is_integer(index) do
|
||||
def insert_at(list, index, value) do
|
||||
if index < 0 do
|
||||
do_insert_at(list, length(list) + index + 1, value)
|
||||
else
|
||||
@@ -468,8 +409,7 @@ defmodule List do
|
||||
|
||||
@doc """
|
||||
Returns a list with a replaced value at the specified `index`.
|
||||
|
||||
Negative indices indicate an offset from the end of the `list`.
|
||||
Negative indices indicate an offset from the end of the list.
|
||||
If `index` is out of bounds, the original `list` is returned.
|
||||
|
||||
## Examples
|
||||
@@ -488,7 +428,7 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec replace_at(list, integer, any) :: list
|
||||
def replace_at(list, index, value) when is_integer(index) do
|
||||
def replace_at(list, index, value) do
|
||||
if index < 0 do
|
||||
do_replace_at(list, length(list) + index, value)
|
||||
else
|
||||
@@ -498,8 +438,7 @@ defmodule List do
|
||||
|
||||
@doc """
|
||||
Returns a list with an updated value at the specified `index`.
|
||||
|
||||
Negative indices indicate an offset from the end of the `list`.
|
||||
Negative indices indicate an offset from the end of the list.
|
||||
If `index` is out of bounds, the original `list` is returned.
|
||||
|
||||
## Examples
|
||||
@@ -518,7 +457,7 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec update_at([elem], integer, (elem -> any)) :: list when elem: var
|
||||
def update_at(list, index, fun) when is_function(fun, 1) and is_integer(index) do
|
||||
def update_at(list, index, fun) do
|
||||
if index < 0 do
|
||||
do_update_at(list, length(list) + index, fun)
|
||||
else
|
||||
@@ -528,8 +467,7 @@ defmodule List do
|
||||
|
||||
@doc """
|
||||
Produces a new list by removing the value at the specified `index`.
|
||||
|
||||
Negative indices indicate an offset from the end of the `list`.
|
||||
Negative indices indicate an offset from the end of the list.
|
||||
If `index` is out of bounds, the original `list` is returned.
|
||||
|
||||
## Examples
|
||||
@@ -545,41 +483,18 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec delete_at(list, integer) :: list
|
||||
def delete_at(list, index) when is_integer(index) do
|
||||
elem(pop_at(list, index), 1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns and removes the value at the specified `index` in the `list`.
|
||||
|
||||
Negative indices indicate an offset from the end of the `list`.
|
||||
If `index` is out of bounds, the original `list` is returned.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.pop_at([1, 2, 3], 0)
|
||||
{1, [2, 3]}
|
||||
iex> List.pop_at([1, 2, 3], 5)
|
||||
{nil, [1, 2, 3]}
|
||||
iex> List.pop_at([1, 2, 3], 5, 10)
|
||||
{10, [1, 2, 3]}
|
||||
iex> List.pop_at([1, 2, 3], -1)
|
||||
{3, [1, 2]}
|
||||
|
||||
"""
|
||||
@spec pop_at(list, integer, any) :: {any, list}
|
||||
def pop_at(list, index, default \\ nil) when is_integer(index) do
|
||||
def delete_at(list, index) do
|
||||
if index < 0 do
|
||||
do_pop_at(list, length(list) + index, default, [])
|
||||
do_delete_at(list, length(list) + index)
|
||||
else
|
||||
do_pop_at(list, index, default, [])
|
||||
do_delete_at(list, index)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a charlist to an atom.
|
||||
Converts a char list to an atom.
|
||||
|
||||
Currently Elixir does not support conversions from charlists
|
||||
Currently Elixir does not support conversions from char lists
|
||||
which contains Unicode codepoints greater than 0xFF.
|
||||
|
||||
Inlined by the compiler.
|
||||
@@ -590,16 +505,16 @@ defmodule List do
|
||||
:elixir
|
||||
|
||||
"""
|
||||
@spec to_atom(charlist) :: atom
|
||||
def to_atom(charlist) do
|
||||
:erlang.list_to_atom(charlist)
|
||||
@spec to_atom(char_list) :: atom
|
||||
def to_atom(char_list) do
|
||||
:erlang.list_to_atom(char_list)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a charlist to an existing atom. Raises an `ArgumentError`
|
||||
Converts a char list to an existing atom. Raises an `ArgumentError`
|
||||
if the atom does not exist.
|
||||
|
||||
Currently Elixir does not support conversions from charlists
|
||||
Currently Elixir does not support conversions from char lists
|
||||
which contains Unicode codepoints greater than 0xFF.
|
||||
|
||||
Inlined by the compiler.
|
||||
@@ -614,13 +529,13 @@ defmodule List do
|
||||
** (ArgumentError) argument error
|
||||
|
||||
"""
|
||||
@spec to_existing_atom(charlist) :: atom
|
||||
def to_existing_atom(charlist) do
|
||||
:erlang.list_to_existing_atom(charlist)
|
||||
@spec to_existing_atom(char_list) :: atom
|
||||
def to_existing_atom(char_list) do
|
||||
:erlang.list_to_existing_atom(char_list)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the float whose text representation is `charlist`.
|
||||
Returns the float whose text representation is `char_list`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -630,13 +545,13 @@ defmodule List do
|
||||
2.2017764
|
||||
|
||||
"""
|
||||
@spec to_float(charlist) :: float
|
||||
def to_float(charlist) do
|
||||
:erlang.list_to_float(charlist)
|
||||
@spec to_float(char_list) :: float
|
||||
def to_float(char_list) do
|
||||
:erlang.list_to_float(char_list)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an integer whose text representation is `charlist`.
|
||||
Returns an integer whose text representation is `char_list`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -646,13 +561,13 @@ defmodule List do
|
||||
123
|
||||
|
||||
"""
|
||||
@spec to_integer(charlist) :: integer
|
||||
def to_integer(charlist) do
|
||||
:erlang.list_to_integer(charlist)
|
||||
@spec to_integer(char_list) :: integer
|
||||
def to_integer(char_list) do
|
||||
:erlang.list_to_integer(char_list)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an integer whose text representation is `charlist` in base `base`.
|
||||
Returns an integer whose text representation is `char_list` in base `base`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -662,9 +577,9 @@ defmodule List do
|
||||
1023
|
||||
|
||||
"""
|
||||
@spec to_integer(charlist, 2..36) :: integer
|
||||
def to_integer(charlist, base) do
|
||||
:erlang.list_to_integer(charlist, base)
|
||||
@spec to_integer(char_list, 2..36) :: integer
|
||||
def to_integer(char_list, base) do
|
||||
:erlang.list_to_integer(char_list, base)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -703,22 +618,10 @@ defmodule List do
|
||||
@spec to_string(:unicode.charlist) :: String.t
|
||||
def to_string(list) when is_list(list) do
|
||||
try do
|
||||
:unicode.characters_to_binary(list)
|
||||
:unicode.characters_to_binary(list)
|
||||
rescue
|
||||
ArgumentError ->
|
||||
raise ArgumentError, """
|
||||
cannot convert the given list to a string.
|
||||
|
||||
To be converted to a string, a list must contain only:
|
||||
|
||||
* strings
|
||||
* integers representing Unicode codepoints
|
||||
* or a list containing one of these three elements
|
||||
|
||||
Please check the given list or call inspect/1 to get the list representation, got:
|
||||
|
||||
#{inspect list}
|
||||
"""
|
||||
raise ArgumentError, "cannot convert list to string. The list must contain only integers, strings or nested such lists; got: #{inspect list}"
|
||||
else
|
||||
result when is_binary(result) ->
|
||||
result
|
||||
@@ -731,109 +634,6 @@ defmodule List do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a keyword list that represents an *edit script*.
|
||||
|
||||
The algorithm is outlined in the
|
||||
"An O(ND) Difference Algorithm and Its Variations" paper by E. Myers.
|
||||
|
||||
An *edit script* is a keyword list. Each key describes the "editing action" to
|
||||
take in order to bring `list1` closer to being equal to `list2`; a key can be
|
||||
`:eq`, `:ins`, or `:del`. Each value is a sublist of either `list1` or `list2`
|
||||
that should be inserted (if the corresponding key `:ins`), deleted (if the
|
||||
corresponding key is `:del`), or left alone (if the corresponding key is
|
||||
`:eq`) in `list1` in order to be closer to `list2`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.myers_difference([1, 4, 2, 3], [1, 2, 3, 4])
|
||||
[eq: [1], del: [4], eq: [2, 3], ins: [4]]
|
||||
|
||||
"""
|
||||
@spec myers_difference(list, list) :: [{:eq | :ins | :del, list}] | nil
|
||||
def myers_difference(list1, list2) when is_list(list1) and is_list(list2) do
|
||||
path = {0, 0, list1, list2, []}
|
||||
find_script(0, length(list1) + length(list2), [path])
|
||||
end
|
||||
|
||||
defp find_script(envelope, max, _paths) when envelope > max do
|
||||
nil
|
||||
end
|
||||
|
||||
defp find_script(envelope, max, paths) do
|
||||
case each_diagonal(-envelope, envelope, paths, []) do
|
||||
{:done, edits} -> compact_reverse(edits, [])
|
||||
{:next, paths} -> find_script(envelope + 1, max, paths)
|
||||
end
|
||||
end
|
||||
|
||||
defp compact_reverse([], acc), do: acc
|
||||
|
||||
defp compact_reverse([{kind, elem} | rest], [{kind, result} | acc]) do
|
||||
compact_reverse(rest, [{kind, [elem | result]} | acc])
|
||||
end
|
||||
|
||||
defp compact_reverse([{kind, elem} | rest], acc) do
|
||||
compact_reverse(rest, [{kind, [elem]} | acc])
|
||||
end
|
||||
|
||||
defp each_diagonal(diag, limit, _paths, next_paths) when diag > limit do
|
||||
{:next, Enum.reverse(next_paths)}
|
||||
end
|
||||
|
||||
defp each_diagonal(diag, limit, paths, next_paths) do
|
||||
{path, rest} = proceed_path(diag, limit, paths)
|
||||
with {:cont, path} <- follow_snake(path) do
|
||||
each_diagonal(diag + 2, limit, rest, [path | next_paths])
|
||||
end
|
||||
end
|
||||
|
||||
defp proceed_path(0, 0, [path]), do: {path, []}
|
||||
|
||||
defp proceed_path(diag, limit, [path | _] = paths) when diag == -limit do
|
||||
{move_down(path), paths}
|
||||
end
|
||||
|
||||
defp proceed_path(diag, limit, [path]) when diag == limit do
|
||||
{move_right(path), []}
|
||||
end
|
||||
|
||||
defp proceed_path(_diag, _limit, [path1, path2 | rest]) do
|
||||
if elem(path1, 1) > elem(path2, 1) do
|
||||
{move_right(path1), [path2 | rest]}
|
||||
else
|
||||
{move_down(path2), [path2 | rest]}
|
||||
end
|
||||
end
|
||||
|
||||
defp move_right({x, y, list1, [elem | rest], edits}) do
|
||||
{x + 1, y, list1, rest, [{:ins, elem} | edits]}
|
||||
end
|
||||
|
||||
defp move_right({x, y, list1, [], edits}) do
|
||||
{x + 1, y, list1, [], edits}
|
||||
end
|
||||
|
||||
defp move_down({x, y, [elem | rest], list2, edits}) do
|
||||
{x, y + 1, rest, list2, [{:del, elem} | edits]}
|
||||
end
|
||||
|
||||
defp move_down({x, y, [], list2, edits}) do
|
||||
{x, y + 1, [], list2, edits}
|
||||
end
|
||||
|
||||
defp follow_snake({x, y, [elem | rest1], [elem | rest2], edits}) do
|
||||
follow_snake({x + 1, y + 1, rest1, rest2, [{:eq, elem} | edits]})
|
||||
end
|
||||
|
||||
defp follow_snake({_x, _y, [], [], edits}) do
|
||||
{:done, edits}
|
||||
end
|
||||
|
||||
defp follow_snake(path) do
|
||||
{:cont, path}
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
# replace_at
|
||||
@@ -850,8 +650,8 @@ defmodule List do
|
||||
[value | rest]
|
||||
end
|
||||
|
||||
defp do_replace_at([head | tail], index, value) do
|
||||
[head | do_replace_at(tail, index - 1, value)]
|
||||
defp do_replace_at([h | t], index, value) do
|
||||
[h | do_replace_at(t, index - 1, value)]
|
||||
end
|
||||
|
||||
# insert_at
|
||||
@@ -864,8 +664,8 @@ defmodule List do
|
||||
[value | list]
|
||||
end
|
||||
|
||||
defp do_insert_at([head | tail], index, value) do
|
||||
[head | do_insert_at(tail, index - 1, value)]
|
||||
defp do_insert_at([h | t], index, value) do
|
||||
[h | do_insert_at(t, index - 1, value)]
|
||||
end
|
||||
|
||||
# update_at
|
||||
@@ -878,30 +678,30 @@ defmodule List do
|
||||
list
|
||||
end
|
||||
|
||||
defp do_update_at([head | tail], index, fun) do
|
||||
[head | do_update_at(tail, index - 1, fun)]
|
||||
defp do_update_at([h | t], index, fun) do
|
||||
[h | do_update_at(t, index - 1, fun)]
|
||||
end
|
||||
|
||||
defp do_update_at([], _index, _fun) do
|
||||
[]
|
||||
end
|
||||
|
||||
# pop_at
|
||||
# delete_at
|
||||
|
||||
defp do_pop_at([], _index, default, acc) do
|
||||
{default, :lists.reverse(acc)}
|
||||
defp do_delete_at([], _index) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp do_pop_at(list, index, default, []) when index < 0 do
|
||||
{default, list}
|
||||
defp do_delete_at([_ | t], 0) do
|
||||
t
|
||||
end
|
||||
|
||||
defp do_pop_at([head | tail], 0, _default, acc) do
|
||||
{head, :lists.reverse(acc, tail)}
|
||||
defp do_delete_at(list, index) when index < 0 do
|
||||
list
|
||||
end
|
||||
|
||||
defp do_pop_at([head | tail], index, default, acc) do
|
||||
do_pop_at(tail, index - 1, default, [head | acc])
|
||||
defp do_delete_at([h | t], index) do
|
||||
[h | do_delete_at(t, index - 1)]
|
||||
end
|
||||
|
||||
# zip
|
||||
@@ -919,8 +719,8 @@ defmodule List do
|
||||
{nil, nil}
|
||||
end
|
||||
|
||||
defp do_zip_each([head | tail], acc) do
|
||||
{tail, [head | acc]}
|
||||
defp do_zip_each([h | t], acc) do
|
||||
{t, [h | acc]}
|
||||
end
|
||||
|
||||
defp do_zip_each([], _) do
|
||||
|
||||
@@ -3,54 +3,54 @@ defprotocol List.Chars do
|
||||
The List.Chars protocol is responsible for
|
||||
converting a structure to a list (only if applicable).
|
||||
The only function required to be implemented is
|
||||
`to_charlist` which does the conversion.
|
||||
`to_char_list` which does the conversion.
|
||||
|
||||
The `to_charlist/1` function automatically imported
|
||||
by `Kernel` invokes this protocol.
|
||||
The `to_char_list` function automatically imported
|
||||
by Kernel invokes this protocol.
|
||||
"""
|
||||
|
||||
def to_charlist(term)
|
||||
|
||||
# TODO: Deprecate by v1.5
|
||||
@doc false
|
||||
Kernel.def to_char_list(term) do
|
||||
__MODULE__.to_charlist(term)
|
||||
end
|
||||
def to_char_list(thing)
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: Atom do
|
||||
def to_charlist(atom), do: Atom.to_charlist(atom)
|
||||
def to_char_list(atom), do: Atom.to_char_list(atom)
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: BitString do
|
||||
@doc """
|
||||
Returns the given binary `term` converted to a charlist.
|
||||
Returns the given binary converted to a char list.
|
||||
"""
|
||||
def to_charlist(term) when is_binary(term) do
|
||||
String.to_charlist(term)
|
||||
def to_char_list(thing) when is_binary(thing) do
|
||||
String.to_char_list(thing)
|
||||
end
|
||||
|
||||
def to_charlist(term) do
|
||||
def to_char_list(thing) do
|
||||
raise Protocol.UndefinedError,
|
||||
protocol: @protocol,
|
||||
value: term,
|
||||
description: "cannot convert a bitstring to a charlist"
|
||||
value: thing,
|
||||
description: "cannot convert a bitstring to a char list"
|
||||
end
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: List do
|
||||
# Note that same inlining is used for the rewrite rule.
|
||||
def to_charlist(list), do: list
|
||||
def to_char_list(list), do: list
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: Integer do
|
||||
def to_charlist(term) do
|
||||
Integer.to_charlist(term)
|
||||
def to_char_list(thing) do
|
||||
Integer.to_char_list(thing)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: Float do
|
||||
def to_charlist(term) do
|
||||
:io_lib_format.fwrite_g(term)
|
||||
@digits 20
|
||||
@limit :math.pow(10, @digits)
|
||||
|
||||
def to_char_list(thing) when thing > @limit do
|
||||
Float.to_char_list(thing, scientific: @digits)
|
||||
end
|
||||
|
||||
def to_char_list(thing) do
|
||||
Float.to_char_list(thing, compact: true, decimals: @digits)
|
||||
end
|
||||
end
|
||||
|
||||
+73
-205
@@ -1,58 +1,21 @@
|
||||
import Kernel, except: [to_string: 1]
|
||||
|
||||
defmodule Macro do
|
||||
@moduledoc ~S"""
|
||||
@moduledoc """
|
||||
Conveniences for working with macros.
|
||||
|
||||
## Custom Sigils
|
||||
|
||||
To create a custom sigil, define a function with the name
|
||||
`sigil_{identifier}` that takes two arguments. The first argument will be
|
||||
the string, the second will be a charlist containing any modifiers. If the
|
||||
sigil is lower case (such as `sigil_x`) then the string argument will allow
|
||||
interpolation. If the sigil is upper case (such as `sigil_X`) then the string
|
||||
will not be interpolated.
|
||||
the interpolated string, the second will be a char list containing any
|
||||
modifiers.
|
||||
|
||||
Valid modifiers include only lower and upper case letters. Other characters
|
||||
will cause a syntax error.
|
||||
|
||||
The module containing the custom sigil must be imported before the sigil
|
||||
syntax can be used.
|
||||
|
||||
### Examples
|
||||
|
||||
defmodule MySigils do
|
||||
defmacro sigil_x(term, [?r]) do
|
||||
quote do
|
||||
unquote(term) |> String.reverse()
|
||||
end
|
||||
end
|
||||
defmacro sigil_x(term, _modifiers) do
|
||||
term
|
||||
end
|
||||
defmacro sigil_X(term, [?r]) do
|
||||
quote do
|
||||
unquote(term) |> String.reverse()
|
||||
end
|
||||
end
|
||||
defmacro sigil_X(term, _modifiers) do
|
||||
term
|
||||
end
|
||||
end
|
||||
|
||||
import MySigils
|
||||
|
||||
~x(with #{"inter" <> "polation"})
|
||||
#=>"with interpolation"
|
||||
|
||||
~x(with #{"inter" <> "polation"})r
|
||||
#=>"noitalopretni htiw"
|
||||
|
||||
~X(without #{"interpolation"})
|
||||
#=>"without \#{"interpolation"}"
|
||||
|
||||
~X(without #{"interpolation"})r
|
||||
#=>"}\"noitalopretni\"{# tuohtiw"
|
||||
"""
|
||||
|
||||
@typedoc "Abstract Syntax Tree (AST)"
|
||||
@@ -101,30 +64,7 @@ defmodule Macro do
|
||||
@doc """
|
||||
Breaks a pipeline expression into a list.
|
||||
|
||||
The AST for a pipeline (a sequence of applications of `|>`) is similar to the
|
||||
AST of a sequence of binary operators or function applications: the top-level
|
||||
expression is the right-most `:|>` (which is the last one to be executed), and
|
||||
its left-hand and right-hand sides are its arguments:
|
||||
|
||||
quote do: 100 |> div(5) |> div(2)
|
||||
#=> {:|>, _, [arg1, arg2]}
|
||||
|
||||
In the example above, the `|>` pipe is the right-most pipe; `arg1` is the AST
|
||||
for `100 |> div(5)`, and `arg2` is the AST for `div(2)`.
|
||||
|
||||
It's often useful to have the AST for such a pipeline as a list of function
|
||||
applications. This function does exactly that:
|
||||
|
||||
Macro.unpipe(quote do: 100 |> div(5) |> div(2))
|
||||
#=> [{100, 0}, {{:div, [], [5]}, 0}, {{:div, [], [2]}, 0}]
|
||||
|
||||
We get a list that follows the pipeline directly: first the `100`, then the
|
||||
`div(5)` (more precisely, its AST), then `div(2)`. The `0` as the second
|
||||
element of the tuples is the position of the previous element in the pipeline
|
||||
inside the current function application: `{{:div, [], [5]}, 0}` means that the
|
||||
previous element (`100`) will be inserted as the 0th (first) argument to the
|
||||
`div/2` function, so that the AST for that function will become `{:div, [],
|
||||
[100, 5]}` (`div(100, 5)`).
|
||||
Raises if the pipeline is ill-formed.
|
||||
"""
|
||||
@spec unpipe(Macro.t) :: [Macro.t]
|
||||
def unpipe(expr) do
|
||||
@@ -136,7 +76,7 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
defp unpipe(other, acc) do
|
||||
[{other, 0} | acc]
|
||||
[{other, 0}|acc]
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -153,27 +93,12 @@ defmodule Macro do
|
||||
raise ArgumentError, bad_pipe(expr, call_args)
|
||||
end
|
||||
|
||||
# Without this, `Macro |> Env == Macro.Env`.
|
||||
def pipe(expr, {:__aliases__, _, _} = call_args, _integer) do
|
||||
raise ArgumentError, bad_pipe(expr, call_args)
|
||||
end
|
||||
|
||||
def pipe(expr, {call, _, [_, _]} = call_args, _integer)
|
||||
when call in unquote(@binary_ops) do
|
||||
raise ArgumentError, "cannot pipe #{to_string expr} into #{to_string call_args}, " <>
|
||||
"the #{to_string call} operator can only take two arguments"
|
||||
end
|
||||
|
||||
# {:fn, _, _} is what we get when we pipe into an anonymous function without
|
||||
# calling it, e.g., `:foo |> (fn x -> x end)`.
|
||||
def pipe(expr, {:fn, _, _}, _integer) do
|
||||
expr_str = to_string(expr)
|
||||
raise ArgumentError,
|
||||
"cannot pipe #{expr_str} into an anonymous function without" <>
|
||||
" calling the function; use something like (fn ... end).() or" <>
|
||||
" define the anonymous function as a regular private function"
|
||||
end
|
||||
|
||||
def pipe(expr, {call, line, atom}, integer) when is_atom(atom) do
|
||||
{call, line, List.insert_at([], integer, expr)}
|
||||
end
|
||||
@@ -191,12 +116,6 @@ defmodule Macro do
|
||||
"can only pipe into local calls foo(), remote calls Foo.bar() or anonymous functions calls foo.()"
|
||||
end
|
||||
|
||||
@doc false
|
||||
def pipe_warning({call, _, _}) when call in unquote(@unary_ops) do
|
||||
"piping into a unary operator is deprecated. You could use e.g. Kernel.+(5) instead of +5"
|
||||
end
|
||||
def pipe_warning(_), do: nil
|
||||
|
||||
@doc """
|
||||
Applies the given function to the node metadata if it contains one.
|
||||
|
||||
@@ -231,7 +150,7 @@ defmodule Macro do
|
||||
|
||||
In order to build a variable, a context is expected.
|
||||
Most of the times, in order to preserve hygiene, the
|
||||
context must be `__MODULE__/0`:
|
||||
context must be `__MODULE__`:
|
||||
|
||||
iex> Macro.var(:foo, __MODULE__)
|
||||
{:foo, [], __MODULE__}
|
||||
@@ -249,7 +168,7 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Performs a depth-first traversal of quoted expressions
|
||||
Performs a depth-first, traversal of quoted expressions
|
||||
using an accumulator.
|
||||
"""
|
||||
@spec traverse(t, any, (t, any -> {t, any}), (t, any -> {t, any})) :: {t, any}
|
||||
@@ -258,15 +177,19 @@ defmodule Macro do
|
||||
do_traverse(ast, acc, pre, post)
|
||||
end
|
||||
|
||||
defp do_traverse({form, meta, args}, acc, pre, post) when is_atom(form) do
|
||||
{args, acc} = do_traverse_args(args, acc, pre, post)
|
||||
post.({form, meta, args}, acc)
|
||||
end
|
||||
|
||||
defp do_traverse({form, meta, args}, acc, pre, post) do
|
||||
{form, acc} = pre.(form, acc)
|
||||
{form, acc} = do_traverse(form, acc, pre, post)
|
||||
{args, acc} = do_traverse_args(args, acc, pre, post)
|
||||
unless is_atom(form) do
|
||||
{form, acc} = pre.(form, acc)
|
||||
{form, acc} = do_traverse(form, acc, pre, post)
|
||||
end
|
||||
|
||||
unless is_atom(args) do
|
||||
{args, acc} = Enum.map_reduce(args, acc, fn x, acc ->
|
||||
{x, acc} = pre.(x, acc)
|
||||
do_traverse(x, acc, pre, post)
|
||||
end)
|
||||
end
|
||||
|
||||
post.({form, meta, args}, acc)
|
||||
end
|
||||
|
||||
@@ -279,7 +202,10 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
defp do_traverse(list, acc, pre, post) when is_list(list) do
|
||||
{list, acc} = do_traverse_args(list, acc, pre, post)
|
||||
{list, acc} = Enum.map_reduce(list, acc, fn x, acc ->
|
||||
{x, acc} = pre.(x, acc)
|
||||
do_traverse(x, acc, pre, post)
|
||||
end)
|
||||
post.(list, acc)
|
||||
end
|
||||
|
||||
@@ -287,17 +213,6 @@ defmodule Macro do
|
||||
post.(x, acc)
|
||||
end
|
||||
|
||||
defp do_traverse_args(args, acc, _pre, _post) when is_atom(args) do
|
||||
{args, acc}
|
||||
end
|
||||
|
||||
defp do_traverse_args(args, acc, pre, post) when is_list(args) do
|
||||
Enum.map_reduce(args, acc, fn x, acc ->
|
||||
{x, acc} = pre.(x, acc)
|
||||
do_traverse(x, acc, pre, post)
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Performs a depth-first, pre-order traversal of quoted expressions.
|
||||
"""
|
||||
@@ -340,19 +255,19 @@ defmodule Macro do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.decompose_call(quote(do: foo))
|
||||
iex> Macro.decompose_call(quote do: foo)
|
||||
{:foo, []}
|
||||
|
||||
iex> Macro.decompose_call(quote(do: foo()))
|
||||
iex> Macro.decompose_call(quote do: foo())
|
||||
{:foo, []}
|
||||
|
||||
iex> Macro.decompose_call(quote(do: foo(1, 2, 3)))
|
||||
iex> Macro.decompose_call(quote do: foo(1, 2, 3))
|
||||
{:foo, [1, 2, 3]}
|
||||
|
||||
iex> Macro.decompose_call(quote(do: Elixir.M.foo(1, 2, 3)))
|
||||
iex> Macro.decompose_call(quote do: Elixir.M.foo(1, 2, 3))
|
||||
{{:__aliases__, [], [:Elixir, :M]}, :foo, [1, 2, 3]}
|
||||
|
||||
iex> Macro.decompose_call(quote(do: 42))
|
||||
iex> Macro.decompose_call(quote do: 42)
|
||||
:error
|
||||
|
||||
"""
|
||||
@@ -376,7 +291,7 @@ defmodule Macro do
|
||||
into a syntax tree.
|
||||
|
||||
One may pass `unquote: true` to `escape/2`
|
||||
which leaves `unquote/1` statements unescaped, effectively
|
||||
which leaves `unquote` statements unescaped, effectively
|
||||
unquoting the contents on escape.
|
||||
|
||||
## Examples
|
||||
@@ -400,24 +315,8 @@ defmodule Macro do
|
||||
@doc """
|
||||
Validates the given expressions are valid quoted expressions.
|
||||
|
||||
Checks the `t:Macro.t/0` for the specification of a valid
|
||||
Check the `type:Macro.t` for the specification of a valid
|
||||
quoted expression.
|
||||
|
||||
It returns `:ok` if the expression is valid. Otherwise it returns a tuple in the form of
|
||||
`{:error, remainder}` where `remainder` is the invalid part of the quoted expression.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.validate({:two_element, :tuple})
|
||||
:ok
|
||||
iex> Macro.validate({:three, :element, :tuple})
|
||||
{:error, {:three, :element, :tuple}}
|
||||
|
||||
iex> Macro.validate([1, 2, 3])
|
||||
:ok
|
||||
iex> Macro.validate([1, 2, 3, {4}])
|
||||
{:error, {4}}
|
||||
|
||||
"""
|
||||
@spec validate(term) :: :ok | {:error, term}
|
||||
def validate(expr) do
|
||||
@@ -455,9 +354,8 @@ defmodule Macro do
|
||||
for information on how to customize the escaping map.
|
||||
|
||||
In this setup, Elixir will escape the following: `\0`, `\a`, `\b`,
|
||||
`\d`, `\e`, `\f`, `\n`, `\r`, `\s`, `\t` and `\v`. Bytes can be
|
||||
given as hexadecimals via `\xNN` and Unicode Codepoints as
|
||||
`\uNNNN` escapes.
|
||||
`\d`, `\e`, `\f`, `\n`, `\r`, `\s`, `\t` and `\v`. Unicode codepoints
|
||||
can be given as hexadecimals via `\xNN` and `\x{NN...}` escapes.
|
||||
|
||||
This function is commonly used on sigil implementations
|
||||
(like `~r`, `~s` and others) which receive a raw, unescaped
|
||||
@@ -500,19 +398,17 @@ defmodule Macro do
|
||||
def unescape_map(?t), do: ?\t
|
||||
def unescape_map(?v), do: ?\v
|
||||
def unescape_map(?x), do: true
|
||||
def unescape_map(?u), do: true
|
||||
def unescape_map(e), do: e
|
||||
|
||||
If the `unescape_map/1` function returns `false`. The char is
|
||||
not escaped and the backslash is kept in the string.
|
||||
If the `unescape_map` function returns `false`. The char is
|
||||
not escaped and `\` is kept in the char list.
|
||||
|
||||
Hexadecimals and Unicode codepoints will be escaped if the map
|
||||
function returns `true` for `?x`. Unicode codepoints if the map
|
||||
function returns `true` for `?u`.
|
||||
Hexadecimals will be escaped if the map function returns `true`
|
||||
for `?x`.
|
||||
|
||||
## Examples
|
||||
|
||||
Using the `unescape_map/1` function defined above is easy:
|
||||
Using the `unescape_map` function defined above is easy:
|
||||
|
||||
Macro.unescape_string "example\\n", &unescape_map(&1)
|
||||
|
||||
@@ -551,23 +447,11 @@ defmodule Macro do
|
||||
@doc """
|
||||
Converts the given expression to a binary.
|
||||
|
||||
The given `fun` is called for every node in the AST with two arguments: the
|
||||
AST of the node being printed and the string representation of that same
|
||||
node. The return value of this function is used as the final string
|
||||
representation for that AST node.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.to_string(quote(do: foo.bar(1, 2, 3)))
|
||||
iex> Macro.to_string(quote do: foo.bar(1, 2, 3))
|
||||
"foo.bar(1, 2, 3)"
|
||||
|
||||
iex> Macro.to_string(quote(do: 1 + 2), fn
|
||||
...> 1, _string -> "one"
|
||||
...> 2, _string -> "two"
|
||||
...> _ast, string -> string
|
||||
...> end)
|
||||
"one + two"
|
||||
|
||||
"""
|
||||
@spec to_string(Macro.t) :: String.t
|
||||
@spec to_string(Macro.t, (Macro.t, String.t -> String.t)) :: String.t
|
||||
@@ -650,18 +534,17 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
# left -> right
|
||||
def to_string([{:->, _, _} | _] = ast, fun) do
|
||||
def to_string([{:->, _, _}|_] = ast, fun) do
|
||||
fun.(ast, "(" <> arrow_to_string(ast, fun, true) <> ")")
|
||||
end
|
||||
|
||||
# left when right
|
||||
def to_string({:when, _, [left, right]} = ast, fun) do
|
||||
right =
|
||||
if right != [] and Keyword.keyword?(right) do
|
||||
kw_list_to_string(right, fun)
|
||||
else
|
||||
fun.(ast, op_to_string(right, fun, :when, :right))
|
||||
end
|
||||
if right != [] and Keyword.keyword?(right) do
|
||||
right = kw_list_to_string(right, fun)
|
||||
else
|
||||
right = fun.(ast, op_to_string(right, fun, :when, :right))
|
||||
end
|
||||
|
||||
fun.(ast, op_to_string(left, fun, :when, :left) <> " when " <> right)
|
||||
end
|
||||
@@ -729,7 +612,7 @@ defmodule Macro do
|
||||
end
|
||||
end
|
||||
|
||||
# Two-element tuples
|
||||
# Two-item tuples
|
||||
def to_string({left, right}, fun) do
|
||||
to_string({:{}, [], [left, right]}, fun)
|
||||
end
|
||||
@@ -740,8 +623,8 @@ defmodule Macro do
|
||||
list == [] ->
|
||||
"[]"
|
||||
:io_lib.printable_list(list) ->
|
||||
IO.iodata_to_binary [?', Inspect.BitString.escape(IO.chardata_to_string(list), ?'), ?']
|
||||
Inspect.List.keyword?(list) ->
|
||||
"'" <> Inspect.BitString.escape(IO.chardata_to_string(list), ?') <> "'"
|
||||
Keyword.keyword?(list) ->
|
||||
"[" <> kw_list_to_string(list, fun) <> "]"
|
||||
true ->
|
||||
"[" <> Enum.map_join(list, ", ", &to_string(&1, fun)) <> "]"
|
||||
@@ -763,11 +646,11 @@ defmodule Macro do
|
||||
to_string(ast, fun)
|
||||
end
|
||||
|
||||
defp bitmods_to_string({op, _, [left, right]} = ast, fun, _, _) when op in [:*, :-] do
|
||||
defp bitmods_to_string({:-, _, [left, right]} = ast, fun, _, _) do
|
||||
result =
|
||||
bitmods_to_string(left, fun, op, :left) <>
|
||||
Atom.to_string(op) <>
|
||||
bitmods_to_string(right, fun, op, :right)
|
||||
bitmods_to_string(left, fun, :-, :left) <>
|
||||
"-" <>
|
||||
bitmods_to_string(right, fun, :-, :right)
|
||||
fun.(ast, result)
|
||||
end
|
||||
|
||||
@@ -778,13 +661,13 @@ defmodule Macro do
|
||||
# Block keywords
|
||||
@kw_keywords [:do, :catch, :rescue, :after, :else]
|
||||
|
||||
defp kw_blocks?([{:do, _} | _] = kw) do
|
||||
defp kw_blocks?([_|_] = kw) do
|
||||
Enum.all?(kw, &match?({x, _} when x in unquote(@kw_keywords), &1))
|
||||
end
|
||||
defp kw_blocks?(_), do: false
|
||||
|
||||
# Check if we have an interpolated string.
|
||||
defp interpolated?({:<<>>, _, [_ | _] = parts}) do
|
||||
defp interpolated?({:<<>>, _, [_|_] = parts}) do
|
||||
Enum.all?(parts, fn
|
||||
{:::, _, [{{:., _, [Kernel, :to_string]}, _, [_]},
|
||||
{:binary, _, _}]} -> true
|
||||
@@ -834,10 +717,6 @@ defmodule Macro do
|
||||
|
||||
defp call_to_string(atom, _fun) when is_atom(atom),
|
||||
do: Atom.to_string(atom)
|
||||
defp call_to_string({:., _, [{:&, _, [val]} = arg]}, fun) when not is_integer(val),
|
||||
do: "(" <> module_to_string(arg, fun) <> ")."
|
||||
defp call_to_string({:., _, [{:fn, _, _} = arg]}, fun),
|
||||
do: "(" <> module_to_string(arg, fun) <> ")."
|
||||
defp call_to_string({:., _, [arg]}, fun),
|
||||
do: module_to_string(arg, fun) <> "."
|
||||
defp call_to_string({:., _, [left, right]}, fun),
|
||||
@@ -854,13 +733,10 @@ defmodule Macro do
|
||||
defp args_to_string(args, fun) do
|
||||
{list, last} = :elixir_utils.split_last(args)
|
||||
|
||||
if last != [] and Inspect.List.keyword?(last) do
|
||||
prefix =
|
||||
case list do
|
||||
[] -> ""
|
||||
_ -> Enum.map_join(list, ", ", &to_string(&1, fun)) <> ", "
|
||||
end
|
||||
prefix <> kw_list_to_string(last, fun)
|
||||
if last != [] and Keyword.keyword?(last) do
|
||||
args = Enum.map_join(list, ", ", &to_string(&1, fun))
|
||||
if list != [], do: args = args <> ", "
|
||||
args <> kw_list_to_string(last, fun)
|
||||
else
|
||||
Enum.map_join(args, ", ", &to_string(&1, fun))
|
||||
end
|
||||
@@ -880,7 +756,7 @@ defmodule Macro do
|
||||
Atom.to_string(key) <> "\n " <> block <> "\n"
|
||||
end
|
||||
|
||||
defp block_to_string([{:->, _, _} | _] = block, fun) do
|
||||
defp block_to_string([{:->, _, _}|_] = block, fun) do
|
||||
Enum.map_join(block, "\n", fn({:->, _, [left, right]}) ->
|
||||
left = comma_join_or_empty_paren(left, fun, false)
|
||||
left <> "->\n " <> adjust_new_lines block_to_string(right, fun), "\n "
|
||||
@@ -899,7 +775,7 @@ defmodule Macro do
|
||||
|
||||
defp map_to_string(list, fun) do
|
||||
cond do
|
||||
Inspect.List.keyword?(list) -> kw_list_to_string(list, fun)
|
||||
Keyword.keyword?(list) -> kw_list_to_string(list, fun)
|
||||
true -> map_list_to_string(list, fun)
|
||||
end
|
||||
end
|
||||
@@ -972,7 +848,7 @@ defmodule Macro do
|
||||
|
||||
* Macros (local or remote)
|
||||
* Aliases are expanded (if possible) and return atoms
|
||||
* Compilation environment macros (`__ENV__/0`, `__MODULE__/0` and `__DIR__/0`)
|
||||
* Pseudo-variables (`__ENV__`, `__MODULE__` and `__DIR__`)
|
||||
* Module attributes reader (`@foo`)
|
||||
|
||||
If the expression cannot be expanded, it returns the expression
|
||||
@@ -990,7 +866,7 @@ defmodule Macro do
|
||||
Consider the implementation below:
|
||||
|
||||
defmacro defmodule_with_length(name, do: block) do
|
||||
length = length(Atom.to_charlist(name))
|
||||
length = length(Atom.to_char_list(name))
|
||||
|
||||
quote do
|
||||
defmodule unquote(name) do
|
||||
@@ -1030,7 +906,7 @@ defmodule Macro do
|
||||
|
||||
defmacro defmodule_with_length(name, do: block) do
|
||||
expanded = Macro.expand(name, __CALLER__)
|
||||
length = length(Atom.to_charlist(expanded))
|
||||
length = length(Atom.to_char_list(expanded))
|
||||
|
||||
quote do
|
||||
defmodule unquote(name) do
|
||||
@@ -1064,7 +940,15 @@ defmodule Macro do
|
||||
end
|
||||
end
|
||||
|
||||
# Expand compilation environment macros
|
||||
# Expand @ calls
|
||||
defp do_expand_once({:@, _, [{name, _, args}]} = original, env) when is_atom(args) or args == [] do
|
||||
case (module = env.module) && Module.open?(module) do
|
||||
true -> {escape(Module.get_attribute(module, name)), true}
|
||||
false -> {original, false}
|
||||
end
|
||||
end
|
||||
|
||||
# Expand pseudo-variables
|
||||
defp do_expand_once({:__MODULE__, _, atom}, env) when is_atom(atom),
|
||||
do: {env.module, true}
|
||||
defp do_expand_once({:__DIR__, _, atom}, env) when is_atom(atom),
|
||||
@@ -1112,7 +996,7 @@ defmodule Macro do
|
||||
|
||||
case expand do
|
||||
{:ok, receiver, quoted} ->
|
||||
next = :erlang.unique_integer()
|
||||
next = :elixir_counter.next
|
||||
{:elixir_quote.linify_with_context_counter(0, {receiver, next}, quoted), true}
|
||||
{:ok, _receiver, _name, _args} ->
|
||||
{original, false}
|
||||
@@ -1133,7 +1017,7 @@ defmodule Macro do
|
||||
|
||||
case expand do
|
||||
{:ok, receiver, quoted} ->
|
||||
next = :erlang.unique_integer()
|
||||
next = :elixir_counter.next
|
||||
{:elixir_quote.linify_with_context_counter(0, {receiver, next}, quoted), true}
|
||||
:error ->
|
||||
{original, false}
|
||||
@@ -1169,11 +1053,6 @@ defmodule Macro do
|
||||
If an atom is given, it is assumed to be an Elixir module,
|
||||
so it is converted to a binary and then processed.
|
||||
|
||||
This function was designed to underscore language identifiers/tokens,
|
||||
that's why it belongs to the `Macro` module. Do not use it as a general
|
||||
mechanism for underscoring strings as it does not support Unicode or
|
||||
characters that are not valid in Elixir identifiers.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.underscore "FooBar"
|
||||
@@ -1194,9 +1073,6 @@ defmodule Macro do
|
||||
iex> Macro.camelize "sap_example"
|
||||
"SapExample"
|
||||
|
||||
iex> Macro.camelize "hello_10"
|
||||
"Hello10"
|
||||
|
||||
"""
|
||||
def underscore(atom) when is_atom(atom) do
|
||||
"Elixir." <> rest = Atom.to_string(atom)
|
||||
@@ -1234,11 +1110,6 @@ defmodule Macro do
|
||||
@doc """
|
||||
Converts the given string to CamelCase format.
|
||||
|
||||
This function was designed to camelize language identifiers/tokens,
|
||||
that's why it belongs to the `Macro` module. Do not use it as a general
|
||||
mechanism for camelizing strings as it does not support Unicode or
|
||||
characters that are not valid in Elixir identifiers.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.camelize "foo_bar"
|
||||
@@ -1263,9 +1134,6 @@ defmodule Macro do
|
||||
defp do_camelize(<<?_, h, t::binary>>) when h >= ?a and h <= ?z,
|
||||
do: <<to_upper_char(h)>> <> do_camelize(t)
|
||||
|
||||
defp do_camelize(<<?_, h, t::binary>>) when h >= ?0 and h <= ?9,
|
||||
do: <<h>> <> do_camelize(t)
|
||||
|
||||
defp do_camelize(<<?_>>),
|
||||
do: <<>>
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@ defmodule Macro.Env do
|
||||
A struct that holds compile time environment information.
|
||||
|
||||
The current environment can be accessed at any time as
|
||||
`__ENV__/0`. Inside macros, the caller environment can be
|
||||
accessed as `__CALLER__/0`.
|
||||
`__ENV__`. Inside macros, the caller environment can be
|
||||
accessed as `__CALLER__`.
|
||||
|
||||
An instance of `Macro.Env` must not be modified by hand. If you need to
|
||||
create a custom environment to pass to `Code.eval_quoted/3`, use the
|
||||
@@ -29,8 +29,8 @@ defmodule Macro.Env do
|
||||
`nil` if not inside a function
|
||||
* `context` - the context of the environment; it can be `nil`
|
||||
(default context), inside a guard or inside a match
|
||||
* `aliases` - a list of two-element tuples, where the first
|
||||
element is the aliased name and the second one the actual name
|
||||
* `aliases` - a list of two-item tuples, where the first
|
||||
item is the aliased name and the second the actual name
|
||||
* `requires` - the list of required modules
|
||||
* `functions` - a list of functions imported from each module
|
||||
* `macros` - a list of macros imported from each module
|
||||
@@ -41,6 +41,7 @@ defmodule Macro.Env do
|
||||
construct (may be `nil`)
|
||||
* `lexical_tracker` - PID of the lexical tracker which is responsible for
|
||||
keeping user info
|
||||
* `local` - the module to expand local functions to
|
||||
"""
|
||||
|
||||
@type name_arity :: {atom, arity}
|
||||
@@ -72,7 +73,8 @@ defmodule Macro.Env do
|
||||
context_modules: context_modules,
|
||||
vars: vars,
|
||||
export_vars: export_vars,
|
||||
lexical_tracker: lexical_tracker}
|
||||
lexical_tracker: lexical_tracker,
|
||||
local: local}
|
||||
|
||||
def __struct__ do
|
||||
%{__struct__: __MODULE__,
|
||||
@@ -92,10 +94,6 @@ defmodule Macro.Env do
|
||||
lexical_tracker: nil}
|
||||
end
|
||||
|
||||
def __struct__(kv) do
|
||||
Enum.reduce kv, __struct__(), fn {k, v}, acc -> :maps.update(k, v, acc) end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a keyword list containing the file and line
|
||||
information as keys.
|
||||
|
||||
+114
-308
@@ -2,94 +2,10 @@ defmodule Map do
|
||||
@moduledoc """
|
||||
A set of functions for working with maps.
|
||||
|
||||
Maps are the "go to" key-value data structure in Elixir. Maps can be created
|
||||
with the `%{}` syntax, and key-value pairs can be expressed as `key => value`:
|
||||
|
||||
iex> %{}
|
||||
%{}
|
||||
iex> %{"one" => :two, 3 => "four"}
|
||||
%{3 => "four", "one" => :two}
|
||||
|
||||
Key-value pairs in a map do not follow any order (that's why the printed map
|
||||
in the example above has a different order than the map that was created).
|
||||
|
||||
Maps do not impose any restriction on the key type: anything can be a key in a
|
||||
map. As a key-value structure, maps do not allow duplicated keys; keys are
|
||||
compared using the exact-equality operator (`===`). If colliding keys are defined
|
||||
in a map literal, the last one prevails.
|
||||
|
||||
When the key in a key-value pair is an atom, the `key: value` shorthand syntax
|
||||
can be used (as in many other special forms), provided key-value pairs are put at
|
||||
the end:
|
||||
|
||||
iex> %{"hello" => "world", a: 1, b: 2}
|
||||
%{:a => 1, :b => 2, "hello" => "world"}
|
||||
|
||||
Keys in maps can be accessed through some of the functions in this module
|
||||
(such as `Map.get/3` or `Map.fetch/2`) or through the `[]` syntax provided by
|
||||
the `Access` module:
|
||||
|
||||
iex> map = %{a: 1, b: 2}
|
||||
iex> Map.fetch(map, :a)
|
||||
{:ok, 1}
|
||||
iex> map[:b]
|
||||
2
|
||||
iex> map["non_existing_key"]
|
||||
nil
|
||||
|
||||
The alternative access syntax `map.key` is provided alongside `[]` when the
|
||||
map has a `:key` key; note that while `map[key]` will return `nil` if `map`
|
||||
doesn't contain the key `key`, `map.key` will raise if `map` doesn't contain
|
||||
the key `:key`.
|
||||
|
||||
iex> map = %{foo: "bar", baz: "bong"}
|
||||
iex> map.foo
|
||||
"bar"
|
||||
iex> map.non_existing_key
|
||||
** (KeyError) key :non_existing_key not found in: %{baz: "bong", foo: "bar"}
|
||||
|
||||
Maps can be pattern matched on; when a map is on the left-hand side of a
|
||||
pattern match, it will match if the map on the right-hand side contains the
|
||||
keys on the left-hand side and their values match the ones on the left-hand
|
||||
side. This means that an empty map matches every map.
|
||||
|
||||
iex> %{} = %{foo: "bar"}
|
||||
%{foo: "bar"}
|
||||
iex> %{a: a} = %{:a => 1, "b" => 2, [:c, :e, :e] => 3}
|
||||
iex> a
|
||||
1
|
||||
iex> %{:c => 3} = %{:a => 1, 2 => :b}
|
||||
** (MatchError) no match of right hand side value: %{2 => :b, :a => 1}
|
||||
|
||||
Variables can be used as map keys both when writing map literals as well as
|
||||
when matching:
|
||||
|
||||
iex> n = 1
|
||||
1
|
||||
iex> %{n => :one}
|
||||
%{1 => :one}
|
||||
iex> %{^n => :one} = %{1 => :one, 2 => :two, 3 => :three}
|
||||
%{1 => :one, 2 => :two, 3 => :three}
|
||||
|
||||
Maps also support a specific update syntax to update the value stored under
|
||||
*existing* atom keys:
|
||||
|
||||
iex> map = %{one: 1, two: 2}
|
||||
iex> %{map | one: "one"}
|
||||
%{one: "one", two: 2}
|
||||
iex> %{map | three: 3}
|
||||
** (KeyError) key :three not found
|
||||
|
||||
## Modules to work with maps
|
||||
|
||||
This module aims to provide functions that perform operations specific to maps
|
||||
(like accessing keys, updating values, and so on). For traversing maps as
|
||||
collections, developers should use the `Enum` module that works across a
|
||||
variety of data types.
|
||||
|
||||
The `Kernel` module also provides a few functions to work with maps: for
|
||||
example, `Kernel.map_size/1` to know the number of key-value pairs in a map or
|
||||
`Kernel.is_map/1` to know if a term is a map.
|
||||
Maps are key-value stores where keys can be any value and
|
||||
are compared using the match operator (`===`). Maps can be
|
||||
created with the `%{}` special form defined in the
|
||||
`Kernel.SpecialForms` module.
|
||||
"""
|
||||
|
||||
@type key :: any
|
||||
@@ -97,7 +13,7 @@ defmodule Map do
|
||||
@compile {:inline, fetch: 2, put: 3, delete: 2, has_key?: 2}
|
||||
|
||||
@doc """
|
||||
Returns all keys from `map`.
|
||||
Returns all keys from the map.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -109,7 +25,7 @@ defmodule Map do
|
||||
defdelegate keys(map), to: :maps
|
||||
|
||||
@doc """
|
||||
Returns all values from `map`.
|
||||
Returns all values from the map.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -121,10 +37,7 @@ defmodule Map do
|
||||
defdelegate values(map), to: :maps
|
||||
|
||||
@doc """
|
||||
Converts `map` to a list.
|
||||
|
||||
Each key-value pair in the map is converted to a two-element tuple `{key,
|
||||
value}` in the resulting list.
|
||||
Converts the map to a list.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -150,7 +63,7 @@ defmodule Map do
|
||||
def new, do: %{}
|
||||
|
||||
@doc """
|
||||
Creates a map from an `enumerable`.
|
||||
Creates a map from an enumerable.
|
||||
|
||||
Duplicated keys are removed; the latest one prevails.
|
||||
|
||||
@@ -162,22 +75,15 @@ defmodule Map do
|
||||
%{a: 3}
|
||||
|
||||
"""
|
||||
@spec new(Enumerable.t) :: map
|
||||
def new(enumerable)
|
||||
def new(%{__struct__: _} = struct), do: new_from_enum(struct)
|
||||
def new(%{} = map), do: map
|
||||
def new(enum), do: new_from_enum(enum)
|
||||
|
||||
defp new_from_enum(enumerable) do
|
||||
enumerable
|
||||
|> Enum.to_list
|
||||
|> :maps.from_list
|
||||
@spec new(Enum.t) :: map
|
||||
def new(enumerable) do
|
||||
Enum.reduce(enumerable, %{}, fn {k, v}, acc -> put(acc, k, v) end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a map from an `enumerable` via the given transformation function.
|
||||
Creates a map from an enumerable via the transformation function.
|
||||
|
||||
Duplicated keys are removed; the latest one prevails.
|
||||
Duplicated entries are removed; the latest one prevails.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -185,25 +91,17 @@ defmodule Map do
|
||||
%{a: :a, b: :b}
|
||||
|
||||
"""
|
||||
@spec new(Enumerable.t, (term -> {key, value})) :: map
|
||||
def new(enumerable, transform) when is_function(transform, 1) do
|
||||
enumerable
|
||||
|> Enum.to_list
|
||||
|> new_transform(transform, [])
|
||||
end
|
||||
|
||||
defp new_transform([], _fun, acc) do
|
||||
acc
|
||||
|> :lists.reverse
|
||||
|> :maps.from_list
|
||||
end
|
||||
|
||||
defp new_transform([item | rest], fun, acc) do
|
||||
new_transform(rest, fun, [fun.(item) | acc])
|
||||
@spec new(Enum.t, (term -> {key, value})) :: map
|
||||
def new(enumerable, transform) do
|
||||
fun = fn el, acc ->
|
||||
{k, v} = transform.(el)
|
||||
put(acc, k, v)
|
||||
end
|
||||
Enum.reduce(enumerable, %{}, fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns whether the given `key` exists in the given `map`.
|
||||
Returns whether a given `key` exists in the given `map`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -217,10 +115,9 @@ defmodule Map do
|
||||
def has_key?(map, key), do: :maps.is_key(key, map)
|
||||
|
||||
@doc """
|
||||
Fetches the value for a specific `key` in the given `map`.
|
||||
Fetches the value for a specific `key` and returns it in a tuple.
|
||||
|
||||
If `map` contains the given `key` with value `value`, then `{:ok, value}` is
|
||||
returned. If `map` doesn't contain `key`, `:error` is returned.
|
||||
If the `key` does not exist, returns `:error`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -234,11 +131,9 @@ defmodule Map do
|
||||
def fetch(map, key), do: :maps.find(key, map)
|
||||
|
||||
@doc """
|
||||
Fetches the value for a specific `key` in the given `map`, erroring out if
|
||||
`map` doesn't contain `key`.
|
||||
Fetches the value for specific `key`.
|
||||
|
||||
If `map` contains the given `key`, the corresponding value is returned. If
|
||||
`map` doesn't contain `key`, a `KeyError` exception is raised.
|
||||
If `key` does not exist, a `KeyError` is raised.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -258,7 +153,7 @@ defmodule Map do
|
||||
|
||||
@doc """
|
||||
Puts the given `value` under `key` unless the entry `key`
|
||||
already exists in `map`.
|
||||
already exists.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -278,11 +173,10 @@ defmodule Map do
|
||||
|
||||
@doc """
|
||||
Evaluates `fun` and puts the result under `key`
|
||||
in `map` unless `key` is already present.
|
||||
in map unless `key` is already present.
|
||||
|
||||
This function is useful in case you want to compute the value to put under
|
||||
`key` only if `key` is not already present (e.g., the value is expensive to
|
||||
calculate or generally difficult to setup and teardown again).
|
||||
This is useful if the value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -306,10 +200,8 @@ defmodule Map do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a new map with all the key-value pairs in `map` where the key
|
||||
is in `keys`.
|
||||
|
||||
If `keys` contains keys that are not in `map`, they're simply ignored.
|
||||
Takes all entries corresponding to the given keys and
|
||||
returns them in a new map.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -317,34 +209,21 @@ defmodule Map do
|
||||
%{a: 1, c: 3}
|
||||
|
||||
"""
|
||||
@spec take(map, Enumerable.t) :: map
|
||||
def take(map, keys)
|
||||
|
||||
def take(map, keys) when is_map(map) do
|
||||
keys
|
||||
|> Enum.to_list
|
||||
|> do_take(map, [])
|
||||
end
|
||||
|
||||
def take(non_map, _keys) do
|
||||
:erlang.error({:badmap, non_map})
|
||||
end
|
||||
|
||||
defp do_take([], _map, acc), do: :maps.from_list(acc)
|
||||
defp do_take([key | rest], map, acc) do
|
||||
acc = case fetch(map, key) do
|
||||
{:ok, value} -> [{key, value} | acc]
|
||||
:error -> acc
|
||||
end
|
||||
do_take(rest, map, acc)
|
||||
@spec take(map, [key]) :: map
|
||||
def take(map, keys) do
|
||||
Enum.reduce(keys, new, fn key, acc ->
|
||||
case fetch(map, key) do
|
||||
{:ok, value} -> put(acc, key, value)
|
||||
:error -> acc
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value for a specific `key` in `map`.
|
||||
Gets the value for a specific `key`.
|
||||
|
||||
If `key` is present in `map` with value `value`, then `value` is
|
||||
returned. Otherwise, `default` is returned (which is `nil` unless
|
||||
specified otherwise).
|
||||
If `key` does not exist, return the default value
|
||||
(`nil` if no default value).
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -368,10 +247,9 @@ defmodule Map do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value for a specific `key` in `map`.
|
||||
Gets the value for a specific `key`.
|
||||
|
||||
If `key` is present in `map` with value `value`, then `value` is
|
||||
returned. Otherwise, `fun` is evaluated and its result is returned.
|
||||
If `key` does not exist, lazily evaluates `fun` and returns its result.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
@@ -398,7 +276,7 @@ defmodule Map do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the given `value` under `key` in `map`.
|
||||
Puts the given `value` under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -409,14 +287,14 @@ defmodule Map do
|
||||
|
||||
"""
|
||||
@spec put(map, key, value) :: map
|
||||
def put(map, key, value) do
|
||||
:maps.put(key, value, map)
|
||||
def put(map, key, val) do
|
||||
:maps.put(key, val, map)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes the entry in `map` for a specific `key`.
|
||||
Deletes the entries in the map for a specific `key`.
|
||||
|
||||
If the `key` does not exist, returns `map` unchanged.
|
||||
If the `key` does not exist, returns the map unchanged.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -432,13 +310,7 @@ defmodule Map do
|
||||
@doc """
|
||||
Merges two maps into one.
|
||||
|
||||
All keys in `map2` will be added to `map1`, overriding any existing one
|
||||
(i.e., the keys in `map2` "have precedence" over the ones in `map1`).
|
||||
|
||||
If you have a struct and you would like to merge a set of keys into the
|
||||
struct, do not use this function, as it would merge all keys on the right
|
||||
side into the struct, even if the key is not part of the struct. Instead,
|
||||
use `Kernel.struct/2`.
|
||||
All keys in `map2` will be added to `map1`, overriding any existing one.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -450,13 +322,10 @@ defmodule Map do
|
||||
defdelegate merge(map1, map2), to: :maps
|
||||
|
||||
@doc """
|
||||
Merges two maps into one, resolving conflicts through the given `callback`.
|
||||
Merges two maps into one.
|
||||
|
||||
All keys in `map2` will be added to `map1`. The given function will be invoked
|
||||
when there are duplicate keys; its arguments are `key` (the duplicate key),
|
||||
`value1` (the value of `key` in `map1`), and `value2` (the value of `key` in
|
||||
`map2`). The value returned by `callback` is used as the value under `key` in
|
||||
the resulting map.
|
||||
All keys in `map2` will be added to `map1`. The given function will
|
||||
be invoked with the key, value1 and value2 to solve conflicts.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -467,7 +336,7 @@ defmodule Map do
|
||||
|
||||
"""
|
||||
@spec merge(map, map, (key, value, value -> value)) :: map
|
||||
def merge(map1, map2, callback) when is_function(callback, 3) do
|
||||
def merge(map1, map2, callback) do
|
||||
:maps.fold fn k, v2, acc ->
|
||||
update(acc, k, v2, fn(v1) -> callback.(k, v1, v2) end)
|
||||
end, map1, map2
|
||||
@@ -476,9 +345,7 @@ defmodule Map do
|
||||
@doc """
|
||||
Updates the `key` in `map` with the given function.
|
||||
|
||||
If `key` is present in `map` with value `value`, `fun` is invoked with
|
||||
argument `value` and its result is used as the new value of `key`. If `key` is
|
||||
not present in `map`, `initial` is inserted as the value of `key`.
|
||||
If the `key` does not exist, inserts the given `initial` value.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -489,7 +356,7 @@ defmodule Map do
|
||||
|
||||
"""
|
||||
@spec update(map, key, value, (value -> value)) :: map
|
||||
def update(map, key, initial, fun) when is_function(fun, 1) do
|
||||
def update(map, key, initial, fun) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} ->
|
||||
put(map, key, fun.(value))
|
||||
@@ -499,11 +366,7 @@ defmodule Map do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns and removes the value associated with `key` in `map`.
|
||||
|
||||
If `key` is present in `map` with value `value`, `{value, new_map}` is
|
||||
returned where `new_map` is the result of removing `key` from `map`. If `key`
|
||||
is not present in `map`, `{default, map}` is returned.
|
||||
Returns and removes all values associated with `key` in the `map`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -517,19 +380,14 @@ defmodule Map do
|
||||
"""
|
||||
@spec pop(map, key, value) :: {value, map}
|
||||
def pop(map, key, default \\ nil) do
|
||||
case map do
|
||||
%{^key => value} -> {value, delete(map, key)}
|
||||
%{} -> {default, map}
|
||||
case fetch(map, key) do
|
||||
{:ok, value} -> {value, delete(map, key)}
|
||||
:error -> {default, map}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily returns and removes the value associated with `key` in `map`.
|
||||
|
||||
If `key` is present in `map` with value `value`, `{value, new_map}` is
|
||||
returned where `new_map` is the result of removing `key` from `map`. If `key`
|
||||
is not present in `map`, `{fun_result, map}` is returned, where `fun_result`
|
||||
is the result of applying `fun`.
|
||||
Lazily returns and removes all values associated with `key` in the `map`.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
@@ -556,9 +414,7 @@ defmodule Map do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Drops the given `keys` from `map`.
|
||||
|
||||
If `keys` contains keys that are not in `map`, they're simply ignored.
|
||||
Drops the given keys from the map.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -566,31 +422,18 @@ defmodule Map do
|
||||
%{a: 1, c: 3}
|
||||
|
||||
"""
|
||||
@spec drop(map, Enumerable.t) :: map
|
||||
def drop(map, keys)
|
||||
|
||||
def drop(map, keys) when is_map(map) do
|
||||
keys
|
||||
|> Enum.to_list
|
||||
|> drop_list(map)
|
||||
end
|
||||
|
||||
def drop(non_map, _keys) do
|
||||
:erlang.error({:badmap, non_map})
|
||||
end
|
||||
|
||||
defp drop_list([], acc), do: acc
|
||||
defp drop_list([key | rest], acc) do
|
||||
drop_list(rest, delete(acc, key))
|
||||
@spec drop(map, [key]) :: map
|
||||
def drop(map, keys) do
|
||||
Enum.reduce(keys, map, &delete(&2, &1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Takes all entries corresponding to the given `keys` in `maps` and extracts
|
||||
them into a separate map.
|
||||
Takes all entries corresponding to the given keys and extracts them into a
|
||||
separate map.
|
||||
|
||||
Returns a tuple with the new map and the old map with removed keys.
|
||||
|
||||
Keys for which there are no entries in `map` are ignored.
|
||||
Keys for which there are no entires in the map are ignored.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -598,37 +441,22 @@ defmodule Map do
|
||||
{%{a: 1, c: 3}, %{b: 2}}
|
||||
|
||||
"""
|
||||
@spec split(map, Enumerable.t) :: {map, map}
|
||||
def split(map, keys)
|
||||
|
||||
def split(map, keys) when is_map(map) do
|
||||
keys
|
||||
|> Enum.to_list
|
||||
|> do_split([], map)
|
||||
end
|
||||
|
||||
def split(non_map, _keys) do
|
||||
:erlang.error({:badmap, non_map})
|
||||
end
|
||||
|
||||
defp do_split([], inc, exc) do
|
||||
{:maps.from_list(inc), exc}
|
||||
end
|
||||
defp do_split([key | rest], inc, exc) do
|
||||
case fetch(exc, key) do
|
||||
{:ok, value} ->
|
||||
do_split(rest, [{key, value} | inc], delete(exc, key))
|
||||
:error ->
|
||||
do_split(rest, inc, exc)
|
||||
end
|
||||
@spec split(map, [key]) :: {map, map}
|
||||
def split(map, keys) do
|
||||
Enum.reduce(keys, {new, map}, fn key, {inc, exc} = acc ->
|
||||
case fetch(exc, key) do
|
||||
{:ok, value} ->
|
||||
{put(inc, key, value), delete(exc, key)}
|
||||
:error ->
|
||||
acc
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates `key` with the given function.
|
||||
Updates the `key` with the given function.
|
||||
|
||||
If `key` is present in `map` with value `value`, `fun` is invoked with
|
||||
argument `value` and its result is used as the new value of `key`. If `key` is
|
||||
not present in `map`, a `KeyError` exception is raised.
|
||||
If the `key` does not exist, raises `KeyError`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -636,16 +464,16 @@ defmodule Map do
|
||||
%{a: 2}
|
||||
|
||||
iex> Map.update!(%{a: 1}, :b, &(&1 * 2))
|
||||
** (KeyError) key :b not found in: %{a: 1}
|
||||
** (KeyError) key :b not found
|
||||
|
||||
"""
|
||||
@spec update!(map, key, (value -> value)) :: map | no_return
|
||||
def update!(%{} = map, key, fun) when is_function(fun, 1) do
|
||||
def update!(%{} = map, key, fun) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} ->
|
||||
put(map, key, fun.(value))
|
||||
:error ->
|
||||
raise KeyError, term: map, key: key
|
||||
:erlang.error({:badkey, key})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -654,15 +482,13 @@ defmodule Map do
|
||||
@doc """
|
||||
Gets the value from `key` and updates it, all in one pass.
|
||||
|
||||
`fun` is called with the current value under `key` in `map` (or `nil` if `key`
|
||||
is not present in `map`) and must return a two-element tuple: the "get" value
|
||||
(the retrieved value, which can be operated on before being returned) and the
|
||||
new value to be stored under `key` in the resulting new map. `fun` may also
|
||||
return `:pop`, which means the current value shall be removed from `map` and
|
||||
returned (making this function behave like `Map.pop(map, key)`.
|
||||
This `fun` argument receives the value of `key` (or `nil` if `key`
|
||||
is not present) and must return a two-elements tuple: the "get" value (the
|
||||
retrieved value, which can be operated on before being returned) and the new
|
||||
value to be stored under `key`.
|
||||
|
||||
The returned value is a tuple with the "get" value returned by
|
||||
`fun` and a new map with the updated value under `key`.
|
||||
The returned value is a tuple with the "get" value returned by `fun` and a
|
||||
new map with the updated value under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -676,29 +502,16 @@ defmodule Map do
|
||||
...> end)
|
||||
{nil, %{b: "new value!", a: 1}}
|
||||
|
||||
iex> Map.get_and_update(%{a: 1}, :a, fn _ -> :pop end)
|
||||
{1, %{}}
|
||||
|
||||
iex> Map.get_and_update(%{a: 1}, :b, fn _ -> :pop end)
|
||||
{nil, %{a: 1}}
|
||||
|
||||
"""
|
||||
@spec get_and_update(map, key, (value -> {get, value} | :pop)) :: {get, map} when get: term
|
||||
def get_and_update(%{} = map, key, fun) when is_function(fun, 1) do
|
||||
current =
|
||||
case :maps.find(key, map) do
|
||||
{:ok, value} -> value
|
||||
:error -> nil
|
||||
end
|
||||
|
||||
case fun.(current) do
|
||||
{get, update} ->
|
||||
{get, :maps.put(key, update, map)}
|
||||
:pop ->
|
||||
{current, :maps.remove(key, map)}
|
||||
other ->
|
||||
raise "the given function must return a two-element tuple or :pop, got: #{inspect(other)}"
|
||||
@spec get_and_update(map, key, (value -> {get, value})) :: {get, map} when get: term
|
||||
def get_and_update(%{} = map, key, fun) do
|
||||
current_value = case :maps.find(key, map) do
|
||||
{:ok, value} -> value
|
||||
:error -> nil
|
||||
end
|
||||
|
||||
{get, update} = fun.(current_value)
|
||||
{get, :maps.put(key, update, map)}
|
||||
end
|
||||
|
||||
def get_and_update(map, _key, _fun), do: :erlang.error({:badmap, map})
|
||||
@@ -706,12 +519,17 @@ defmodule Map do
|
||||
@doc """
|
||||
Gets the value from `key` and updates it. Raises if there is no `key`.
|
||||
|
||||
Behaves exactly like `get_and_update/3`, but raises a `KeyError` exception if
|
||||
`key` is not present in `map`.
|
||||
This `fun` argument receives the value of `key` and must return a
|
||||
two-elements tuple: the "get" value (the retrieved value, which can be
|
||||
operated on before being returned) and the new value to be stored under
|
||||
`key`.
|
||||
|
||||
The returned value is a tuple with the "get" value returned by `fun` and a
|
||||
new map with the updated value under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.get_and_update!(%{a: 1}, :a, fn current_value ->
|
||||
iex> Map.get_and_update!(%{a: 1}, :a, fn(current_value) ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{1, %{a: "new value!"}}
|
||||
@@ -719,39 +537,27 @@ defmodule Map do
|
||||
iex> Map.get_and_update!(%{a: 1}, :b, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
** (KeyError) key :b not found in: %{a: 1}
|
||||
|
||||
iex> Map.get_and_update!(%{a: 1}, :a, fn _ ->
|
||||
...> :pop
|
||||
...> end)
|
||||
{1, %{}}
|
||||
** (KeyError) key :b not found
|
||||
|
||||
"""
|
||||
@spec get_and_update!(map, key, (value -> {get, value})) :: {get, map} | no_return when get: term
|
||||
def get_and_update!(%{} = map, key, fun) when is_function(fun, 1) do
|
||||
def get_and_update!(%{} = map, key, fun) do
|
||||
case :maps.find(key, map) do
|
||||
{:ok, value} ->
|
||||
case fun.(value) do
|
||||
{get, update} ->
|
||||
{get, :maps.put(key, update, map)}
|
||||
:pop ->
|
||||
{value, :maps.remove(key, map)}
|
||||
other ->
|
||||
raise "the given function must return a two-element tuple or :pop, got: #{inspect(other)}"
|
||||
end
|
||||
{get, update} = fun.(value)
|
||||
{get, :maps.put(key, update, map)}
|
||||
:error ->
|
||||
raise KeyError, term: map, key: key
|
||||
:erlang.error({:badkey, key})
|
||||
end
|
||||
end
|
||||
|
||||
def get_and_update!(map, _key, _fun), do: :erlang.error({:badmap, map})
|
||||
|
||||
@doc """
|
||||
Converts a `struct` to map.
|
||||
Converts a struct to map.
|
||||
|
||||
It accepts the struct module or a struct itself and
|
||||
simply removes the `__struct__` field from the given struct
|
||||
or from a new struct generated from the given module.
|
||||
simply removes the `__struct__` field from the struct.
|
||||
|
||||
## Example
|
||||
|
||||
@@ -792,10 +598,10 @@ defmodule Map do
|
||||
@spec equal?(map, map) :: boolean
|
||||
def equal?(%{} = map1, %{} = map2), do: map1 === map2
|
||||
|
||||
# TODO: Deprecate by 1.3
|
||||
# TODO: Remove by 1.4
|
||||
@doc false
|
||||
# TODO: Remove on 2.0
|
||||
def size(map) do
|
||||
IO.warn "Map.size/1 is deprecated, please use Kernel.map_size/1"
|
||||
map_size(map)
|
||||
end
|
||||
end
|
||||
|
||||
+51
-136
@@ -1,42 +1,16 @@
|
||||
defmodule MapSet do
|
||||
@moduledoc """
|
||||
Functions that work on sets.
|
||||
A set of functions for working with sets.
|
||||
|
||||
`MapSet` is the "go to" set data structure in Elixir. A set can be constructed
|
||||
using `MapSet.new/0`:
|
||||
|
||||
iex> MapSet.new
|
||||
#MapSet<[]>
|
||||
|
||||
A set can contain any kind of elements and elements in a set don't have to be
|
||||
of the same type. By definition, sets can't contain duplicate elements: when
|
||||
inserting an element in a set where it's already present, the insertion is
|
||||
simply a no-op.
|
||||
|
||||
iex> set = MapSet.new
|
||||
iex> MapSet.put(set, "foo")
|
||||
#MapSet<["foo"]>
|
||||
iex> set |> MapSet.put("foo") |> MapSet.put("foo")
|
||||
#MapSet<["foo"]>
|
||||
|
||||
A `MapSet` is represented internally using the `%MapSet{}` struct. This struct
|
||||
can be used whenever there's a need to pattern match on something being a `MapSet`:
|
||||
|
||||
iex> match?(%MapSet{}, MapSet.new())
|
||||
true
|
||||
|
||||
Note that, however, the struct fields are private and must not be accessed
|
||||
directly; use the functions in this module to perform operations on sets.
|
||||
|
||||
Sets can also be constructed starting from other collection-type data
|
||||
structures: for example, see `MapSet.new/1` or `Enum.into/2`.
|
||||
The `MapSet` is represented internally as a struct,
|
||||
therefore `%MapSet{}` can be used whenever there is a
|
||||
need to match on any `MapSet`. Note though the struct
|
||||
fields are private and must not be accessed directly.
|
||||
Instead, use the functions in this module.
|
||||
"""
|
||||
|
||||
@opaque t :: %__MODULE__{map: map}
|
||||
@type value :: term
|
||||
|
||||
@opaque t(value) :: %__MODULE__{map: %{optional(value) => true}}
|
||||
@type t :: t(term)
|
||||
|
||||
defstruct map: %{}
|
||||
|
||||
@doc """
|
||||
@@ -63,14 +37,8 @@ defmodule MapSet do
|
||||
|
||||
"""
|
||||
@spec new(Enum.t) :: t
|
||||
def new(%__MODULE__{} = mapset), do: mapset
|
||||
def new(enumerable) do
|
||||
map =
|
||||
enumerable
|
||||
|> Enum.to_list
|
||||
|> do_new([])
|
||||
|
||||
%MapSet{map: map}
|
||||
Enum.reduce(enumerable, %MapSet{}, &put(&2, &1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -82,32 +50,9 @@ defmodule MapSet do
|
||||
#MapSet<[2, 4]>
|
||||
|
||||
"""
|
||||
@spec new(Enum.t, (term -> val)) :: t(val) when val: value
|
||||
def new(enumerable, transform) when is_function(transform, 1) do
|
||||
map =
|
||||
enumerable
|
||||
|> Enum.to_list
|
||||
|> do_new_transform(transform, [])
|
||||
|
||||
%MapSet{map: map}
|
||||
end
|
||||
|
||||
defp do_new([], acc) do
|
||||
acc
|
||||
|> :lists.reverse
|
||||
|> :maps.from_list
|
||||
end
|
||||
defp do_new([item | rest], acc) do
|
||||
do_new(rest, [{item, true} | acc])
|
||||
end
|
||||
|
||||
defp do_new_transform([], _fun, acc) do
|
||||
acc
|
||||
|> :lists.reverse
|
||||
|> :maps.from_list
|
||||
end
|
||||
defp do_new_transform([item | rest], fun, acc) do
|
||||
do_new_transform(rest, fun, [{fun.(item), true} | acc])
|
||||
@spec new(Enum.t, (term -> term)) :: t
|
||||
def new(enumerable, transform) do
|
||||
Enum.reduce(enumerable, %MapSet{}, &put(&2, transform.(&1)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -124,9 +69,9 @@ defmodule MapSet do
|
||||
#MapSet<[1, 3]>
|
||||
|
||||
"""
|
||||
@spec delete(t(val1), val2) :: t(val1) when val1: value, val2: value
|
||||
def delete(%MapSet{map: map} = set, value) do
|
||||
%{set | map: Map.delete(map, value)}
|
||||
@spec delete(t, value) :: t
|
||||
def delete(%MapSet{map: map} = set, term) do
|
||||
%{set | map: Map.delete(map, term)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -138,37 +83,12 @@ defmodule MapSet do
|
||||
#MapSet<[1]>
|
||||
|
||||
"""
|
||||
@spec difference(t(val1), t(val2)) :: t(val1) when val1: value, val2: value
|
||||
def difference(mapset1, mapset2)
|
||||
|
||||
# If the first set is less than twice the size of the second map,
|
||||
# it is fastest to re-accumulate items in the first set that are not
|
||||
# present in the second set.
|
||||
def difference(%MapSet{map: map1}, %MapSet{map: map2})
|
||||
when map_size(map1) < map_size(map2) * 2 do
|
||||
map = map1
|
||||
|> Map.keys
|
||||
|> filter_not_in(map2)
|
||||
|
||||
%MapSet{map: map}
|
||||
end
|
||||
|
||||
# If the second set is less than half the size of the first set, it's fastest
|
||||
# to simply iterate through each item in the second set, deleting them from
|
||||
# the first set.
|
||||
@spec difference(t, t) :: t
|
||||
def difference(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
%MapSet{map: Map.drop(map1, Map.keys(map2))}
|
||||
end
|
||||
|
||||
defp filter_not_in(keys, map2, acc \\ [])
|
||||
defp filter_not_in([], _map2, acc), do: :maps.from_list(acc)
|
||||
defp filter_not_in([key | rest], map2, acc) do
|
||||
acc = if Map.has_key?(map2, key) do
|
||||
acc
|
||||
else
|
||||
[{key, true} | acc]
|
||||
end
|
||||
filter_not_in(rest, map2, acc)
|
||||
map = :maps.fold(fn value, _, acc ->
|
||||
Map.delete(acc, value)
|
||||
end, map1, map2)
|
||||
%MapSet{map: map}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -184,21 +104,16 @@ defmodule MapSet do
|
||||
"""
|
||||
@spec disjoint?(t, t) :: boolean
|
||||
def disjoint?(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
{map1, map2} = order_by_size(map1, map2)
|
||||
|
||||
map1
|
||||
|> Map.keys
|
||||
|> none_in?(map2)
|
||||
end
|
||||
|
||||
defp none_in?([], _) do
|
||||
true
|
||||
end
|
||||
defp none_in?([key | rest], map2) do
|
||||
case Map.has_key?(map2, key) do
|
||||
true -> false
|
||||
false -> none_in?(rest, map2)
|
||||
end
|
||||
if map_size(map1) > map_size(map2), do: {map1, map2} = {map2, map1}
|
||||
:maps.fold(fn value, _, _ ->
|
||||
if Map.has_key?(map2, value) do
|
||||
throw({:halt, false})
|
||||
else
|
||||
true
|
||||
end
|
||||
end, true, map1)
|
||||
catch
|
||||
{:halt, false} -> false
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -231,11 +146,17 @@ defmodule MapSet do
|
||||
#MapSet<[]>
|
||||
|
||||
"""
|
||||
@spec intersection(t(val), t(val)) :: t(val) when val: value
|
||||
@spec intersection(t, t) :: t
|
||||
def intersection(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
{map1, map2} = order_by_size(map1, map2)
|
||||
|
||||
%MapSet{map: Map.take(map2, Map.keys(map1))}
|
||||
if map_size(map1) > map_size(map2), do: {map1, map2} = {map2, map1}
|
||||
map = :maps.fold(fn value, _, acc ->
|
||||
if Map.has_key?(map2, value) do
|
||||
Map.put(acc, value, true)
|
||||
else
|
||||
acc
|
||||
end
|
||||
end, %{}, map1)
|
||||
%MapSet{map: map}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -265,7 +186,7 @@ defmodule MapSet do
|
||||
#MapSet<[1, 2, 3, 4]>
|
||||
|
||||
"""
|
||||
@spec put(t(val), new_val) :: t(val | new_val) when val: value, new_val: value
|
||||
@spec put(t, value) :: t
|
||||
def put(%MapSet{map: map} = set, value) do
|
||||
%{set | map: Map.put(map, value, true)}
|
||||
end
|
||||
@@ -300,21 +221,18 @@ defmodule MapSet do
|
||||
@spec subset?(t, t) :: boolean
|
||||
def subset?(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
if map_size(map1) <= map_size(map2) do
|
||||
map1
|
||||
|> Map.keys
|
||||
|> do_subset?(map2)
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
defp do_subset?([], _), do: true
|
||||
defp do_subset?([key | rest], map2) do
|
||||
if Map.has_key?(map2, key) do
|
||||
do_subset?(rest, map2)
|
||||
:maps.fold(fn value, _, _ ->
|
||||
if Map.has_key?(map2, value) do
|
||||
true
|
||||
else
|
||||
throw({:halt, false})
|
||||
end
|
||||
end, true, map1)
|
||||
else
|
||||
false
|
||||
end
|
||||
catch
|
||||
{:halt, false} -> false
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -326,7 +244,7 @@ defmodule MapSet do
|
||||
[1, 2, 3]
|
||||
|
||||
"""
|
||||
@spec to_list(t(val)) :: [val] when val: value
|
||||
@spec to_list(t) :: list
|
||||
def to_list(%MapSet{map: map}) do
|
||||
Map.keys(map)
|
||||
end
|
||||
@@ -340,14 +258,11 @@ defmodule MapSet do
|
||||
#MapSet<[1, 2, 3, 4]>
|
||||
|
||||
"""
|
||||
@spec union(t(val1), t(val2)) :: t(val1 | val2) when val1: value, val2: value
|
||||
@spec union(t, t) :: t
|
||||
def union(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
%MapSet{map: Map.merge(map1, map2)}
|
||||
end
|
||||
|
||||
defp order_by_size(map1, map2) when map_size(map1) > map_size(map2), do: {map2, map1}
|
||||
defp order_by_size(map1, map2), do: {map1, map2}
|
||||
|
||||
defimpl Enumerable do
|
||||
def reduce(set, acc, fun), do: Enumerable.List.reduce(MapSet.to_list(set), acc, fun)
|
||||
def member?(set, val), do: {:ok, MapSet.member?(set, val)}
|
||||
|
||||
+410
-433
File diff suppressed because it is too large
Load Diff
@@ -83,12 +83,13 @@ defmodule Module.LocalsTracker do
|
||||
defp to_pid(pid) when is_pid(pid), do: pid
|
||||
defp to_pid(mod) when is_atom(mod) do
|
||||
table = :elixir_module.data_table(mod)
|
||||
:ets.lookup_element(table, {:elixir, :locals_tracker}, 2)
|
||||
[{_, val}] = :ets.lookup(table, {:elixir, :locals_tracker})
|
||||
val
|
||||
end
|
||||
|
||||
# Internal API
|
||||
|
||||
# Starts the tracker and returns its PID.
|
||||
# Starts the tracker and returns its pid.
|
||||
@doc false
|
||||
def start_link do
|
||||
:gen_server.start_link(__MODULE__, [], [])
|
||||
@@ -120,7 +121,7 @@ defmodule Module.LocalsTracker do
|
||||
:gen_server.cast(pid, {:add_local, from, to})
|
||||
end
|
||||
|
||||
# Adds an import dispatch to the given target.
|
||||
# Adds a import dispatch to the given target.
|
||||
@doc false
|
||||
def add_import(pid, function, module, target) when is_atom(module) and is_tuple(target) do
|
||||
:gen_server.cast(pid, {:add_import, function, module, target})
|
||||
@@ -173,9 +174,9 @@ defmodule Module.LocalsTracker do
|
||||
reduce_unreachable(private, [], :sets.from_list(unreachable))
|
||||
end
|
||||
|
||||
defp reduce_unreachable([{vertex, callers} | t], acc, unreachable) do
|
||||
defp reduce_unreachable([{vertex, callers}|t], acc, unreachable) do
|
||||
if :sets.is_subset(callers, unreachable) do
|
||||
reduce_unreachable(t, [{vertex, callers} | acc], unreachable)
|
||||
reduce_unreachable(t, [{vertex, callers}|acc], unreachable)
|
||||
else
|
||||
reduce_unreachable(acc ++ t, [], :sets.del_element(vertex, unreachable))
|
||||
end
|
||||
@@ -194,7 +195,7 @@ defmodule Module.LocalsTracker do
|
||||
if :lists.member(tuple, reachable) do
|
||||
acc
|
||||
else
|
||||
[{:unused_def, tuple, kind} | acc]
|
||||
[{:unused_def, tuple, kind}|acc]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -206,12 +207,12 @@ defmodule Module.LocalsTracker do
|
||||
invoked = for {n, a} <- reachable, n == name, a in min..max, do: a
|
||||
|
||||
if invoked == [] do
|
||||
[{:unused_def, tuple, kind} | acc]
|
||||
[{:unused_def, tuple, kind}|acc]
|
||||
else
|
||||
case :lists.min(invoked) - min do
|
||||
0 -> acc
|
||||
^default -> [{:unused_args, tuple} | acc]
|
||||
unused_args -> [{:unused_args, tuple, unused_args} | acc]
|
||||
^default -> [{:unused_args, tuple}|acc]
|
||||
unused_args -> [{:unused_args, tuple, unused_args}|acc]
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -243,11 +244,11 @@ defmodule Module.LocalsTracker do
|
||||
@doc false
|
||||
def handle_call({:cache_env, env}, _from, {d, cache}) do
|
||||
case cache do
|
||||
[{i, ^env} | _] ->
|
||||
[{i, ^env}|_] ->
|
||||
{:reply, i, {d, cache}}
|
||||
t ->
|
||||
i = length(t)
|
||||
{:reply, i, {d, [{i, env} | t]}}
|
||||
{:reply, i, {d, [{i, env}|t]}}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -352,7 +353,7 @@ defmodule Module.LocalsTracker do
|
||||
|
||||
defp replace_edge!(d, from, to) do
|
||||
_ = unless :lists.member(to, :digraph.out_neighbours(d, from)) do
|
||||
[:"$e" | _] = :digraph.add_edge(d, from, to)
|
||||
[:"$e"|_] = :digraph.add_edge(d, from, to)
|
||||
end
|
||||
:ok
|
||||
end
|
||||
|
||||
+13
-13
@@ -146,7 +146,7 @@ defmodule Node do
|
||||
`:ignored` if the local node is not alive.
|
||||
|
||||
For more information, see
|
||||
[`:net_kernel.connect_node/1`](http://www.erlang.org/doc/man/net_kernel.html#connect_node-1).
|
||||
[`:erlang.connect_node/1`](http://www.erlang.org/doc/man/net_kernel.html#connect_node-1).
|
||||
"""
|
||||
@spec connect(t) :: boolean | :ignored
|
||||
def connect(node) do
|
||||
@@ -154,8 +154,8 @@ defmodule Node do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the PID of a new process started by the application of `fun`
|
||||
on `node`. If `node` does not exist, a useless PID is returned.
|
||||
Returns the pid of a new process started by the application of `fun`
|
||||
on `node`. If `node` does not exist, a useless pid is returned.
|
||||
|
||||
For the list of available options, see
|
||||
[`:erlang.spawn/2`](http://www.erlang.org/doc/man/erlang.html#spawn-2).
|
||||
@@ -168,10 +168,10 @@ defmodule Node do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the PID of a new process started by the application of `fun`
|
||||
Returns the pid of a new process started by the application of `fun`
|
||||
on `node`.
|
||||
|
||||
If `node` does not exist, a useless PID is returned.
|
||||
If `node` does not exist, a useless pid is returned.
|
||||
|
||||
For the list of available options, see
|
||||
[`:erlang.spawn_opt/3`](http://www.erlang.org/doc/man/erlang.html#spawn_opt-3).
|
||||
@@ -184,10 +184,10 @@ defmodule Node do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the PID of a new process started by the application of
|
||||
Returns the pid of a new process started by the application of
|
||||
`module.function(args)` on `node`.
|
||||
|
||||
If `node` does not exist, a useless PID is returned.
|
||||
If `node` does not exist, a useless pid is returned.
|
||||
|
||||
For the list of available options, see
|
||||
[`:erlang.spawn/4`](http://www.erlang.org/doc/man/erlang.html#spawn-4).
|
||||
@@ -200,10 +200,10 @@ defmodule Node do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the PID of a new process started by the application of
|
||||
Returns the pid of a new process started by the application of
|
||||
`module.function(args)` on `node`.
|
||||
|
||||
If `node` does not exist, a useless PID is returned.
|
||||
If `node` does not exist, a useless pid is returned.
|
||||
|
||||
For the list of available options, see
|
||||
[`:erlang.spawn/5`](http://www.erlang.org/doc/man/erlang.html#spawn_opt-5).
|
||||
@@ -216,10 +216,10 @@ defmodule Node do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the PID of a new linked process started by the application of `fun` on `node`.
|
||||
Returns the pid of a new linked process started by the application of `fun` on `node`.
|
||||
|
||||
A link is created between the calling process and the new process, atomically.
|
||||
If `node` does not exist, a useless PID is returned (and due to the link, an exit
|
||||
If `node` does not exist, a useless pid is returned (and due to the link, an exit
|
||||
signal with exit reason `:noconnection` will be received).
|
||||
|
||||
Inlined by the compiler.
|
||||
@@ -230,11 +230,11 @@ defmodule Node do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the PID of a new linked process started by the application of
|
||||
Returns the pid of a new linked process started by the application of
|
||||
`module.function(args)` on `node`.
|
||||
|
||||
A link is created between the calling process and the new process, atomically.
|
||||
If `node` does not exist, a useless PID is returned (and due to the link, an exit
|
||||
If `node` does not exist, a useless pid is returned (and due to the link, an exit
|
||||
signal with exit reason `:noconnection` will be received).
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
+182
-445
@@ -8,158 +8,70 @@ defmodule OptionParser do
|
||||
@type errors :: [{String.t, String.t | nil}]
|
||||
@type options :: [switches: Keyword.t, strict: Keyword.t, aliases: Keyword.t]
|
||||
|
||||
defmodule ParseError do
|
||||
defexception [:message]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses `argv` into a keyword list.
|
||||
Parses `argv` into a keywords list.
|
||||
|
||||
It returns a three-element tuple with the form `{parsed, args, invalid}`, where:
|
||||
It returns a three-element tuple as follows:
|
||||
|
||||
* `parsed` is a keyword list of parsed switches with `{switch_name, value}`
|
||||
tuples in it; `switch_name` is the atom representing the switch name while
|
||||
`value` is the value for that switch parsed according to `opts` (see the
|
||||
"Examples" section for more information)
|
||||
* `args` is a list of the remaining arguments in `argv` as strings
|
||||
* `invalid` is a list of invalid options as `{option_name, value}` where
|
||||
`option_name` is the raw option and `value` is `nil` if the option wasn't
|
||||
expected or the string value if the value didn't have the expected type for
|
||||
the corresponding option
|
||||
1. parsed switches,
|
||||
2. remaining arguments,
|
||||
3. invalid options.
|
||||
|
||||
Elixir converts switches to underscored atoms, so `--source-path` becomes
|
||||
`:source_path`. This is done to better suit Elixir conventions. However, this
|
||||
means that switches can't contain underscores and switches that do contain
|
||||
underscores are always returned in the list of invalid switches.
|
||||
|
||||
When parsing, it is common to list switches and their expected types:
|
||||
|
||||
iex> OptionParser.parse(["--debug"], switches: [debug: :boolean])
|
||||
{[debug: true], [], []}
|
||||
|
||||
iex> OptionParser.parse(["--source", "lib"], switches: [source: :string])
|
||||
{[source: "lib"], [], []}
|
||||
|
||||
iex> OptionParser.parse(["--source-path", "lib", "test/enum_test.exs", "--verbose"],
|
||||
...> switches: [source_path: :string, verbose: :boolean])
|
||||
{[source_path: "lib", verbose: true], ["test/enum_test.exs"], []}
|
||||
|
||||
We will explore the valid switches and operation modes of option parser below.
|
||||
|
||||
## Options
|
||||
|
||||
The following options are supported:
|
||||
|
||||
* `:switches` or `:strict` - see the "Switch definitions" section below
|
||||
* `:allow_nonexistent_atoms` - see the "Parsing dynamic switches" section below
|
||||
* `:aliases` - see the "Aliases" section below
|
||||
|
||||
## Switch definitions
|
||||
|
||||
Switches can be specified via one of two options:
|
||||
|
||||
* `:switches` - defines some switches and their types. This function
|
||||
still attempts to parse switches that are not in this list.
|
||||
* `:strict` - defines strict switches. Any switch in `argv` that is not
|
||||
specified in the list is returned in the invalid options list.
|
||||
|
||||
Both these options accept a keyword list of `{name, type}` tuples where `name`
|
||||
is an atom defining the name of the switch and `type` is an atom that
|
||||
specifies the type for the value of this switch (see the "Types" section below
|
||||
for the possible types and more information about type casting).
|
||||
|
||||
Note that you should only supply the `:switches` or the`:strict` option.
|
||||
If you supply both, an `ArgumentError` exception will be raised.
|
||||
|
||||
### Types
|
||||
|
||||
Switches parsed by `OptionParser` may take zero or one arguments.
|
||||
|
||||
The following switches types take no arguments:
|
||||
|
||||
* `:boolean` - sets the value to `true` when given (see also the
|
||||
"Negation switches" section below)
|
||||
* `:count` - counts the number of times the switch is given
|
||||
|
||||
The following switches take one argument:
|
||||
|
||||
* `:integer` - parses the value as an integer
|
||||
* `:float` - parses the value as a float
|
||||
* `:string` - parses the value as a string
|
||||
|
||||
If a switch can't be parsed according to the given type, it is
|
||||
returned in the invalid options list.
|
||||
|
||||
### Modifiers
|
||||
|
||||
Switches can be specified with modifiers, which change how
|
||||
they behave. The following modifiers are supported:
|
||||
|
||||
* `:keep` - keeps duplicated items instead of overriding them;
|
||||
works with all types except `:count`. Specifying `switch_name: :keep`
|
||||
assumes the type of `:switch_name` will be `:string`.
|
||||
|
||||
To use `:keep` with a type other than `:string`, use a list as the type
|
||||
for the switch. For example: `[foo: [:integer, :keep]]`.
|
||||
|
||||
### Negation switches
|
||||
|
||||
In case a switch `SWITCH` is specified to have type `:boolean`, it may be
|
||||
passed as `--no-SWITCH` as well which will set the option to `false`:
|
||||
|
||||
iex> OptionParser.parse(["--no-op", "path/to/file"], switches: [op: :boolean])
|
||||
{[op: false], ["path/to/file"], []}
|
||||
|
||||
### Parsing dynamic switches
|
||||
|
||||
`OptionParser` also includes a dynamic mode where it will attempt to parse
|
||||
switches dynamically. Such can be done by not specifying the `:switches` or
|
||||
`:strict` option.
|
||||
## Examples
|
||||
|
||||
iex> OptionParser.parse(["--debug"])
|
||||
{[debug: true], [], []}
|
||||
|
||||
iex> OptionParser.parse(["--source", "lib"])
|
||||
{[source: "lib"], [], []}
|
||||
|
||||
Switches followed by a value will be assigned the value, as a string. Switches
|
||||
without an argument, like `--debug` in the examples above, will automatically be
|
||||
set to `true`.
|
||||
|
||||
Since Elixir converts switches to atoms, the dynamic mode will only parse
|
||||
switches that translates to atoms used by the runtime. Therefore, the code below
|
||||
likely won't parse the given option since the `:option_parser_example` atom is
|
||||
never used anywhere:
|
||||
|
||||
OptionParser.parse(["--option-parser-example"])
|
||||
# Does nothing more...
|
||||
|
||||
However, the code below does since the `:option_parser_example` atom is used
|
||||
at some point later (or earlier) on:
|
||||
|
||||
{opts, _, _} = OptionParser.parse(["--option-parser-example"])
|
||||
opts[:option_parser_example]
|
||||
|
||||
In other words, when using dynamic mode, Elixir will do the correct thing and
|
||||
only parse options that are used by the runtime, ignoring all others. If you
|
||||
would like to parse all switches, regardless if they exist or not, you can
|
||||
force creation of atoms by passing `allow_nonexistent_atoms: true` as option.
|
||||
Such option is useful when you are building command-line applications that
|
||||
receive dynamically-named arguments but must be used with care on long-running
|
||||
systems.
|
||||
iex> OptionParser.parse(["--source-path", "lib", "test/enum_test.exs", "--verbose"])
|
||||
{[source_path: "lib", verbose: true], ["test/enum_test.exs"], []}
|
||||
|
||||
By default, Elixir will try to automatically parse all switches.
|
||||
Switches followed by a value will be assigned the value, as a string.
|
||||
Switches without an argument, like `--debug` in the examples above, will
|
||||
automatically be set to `true`.
|
||||
Switches without an argument, like `--debug` will automatically
|
||||
be set to `true`.
|
||||
|
||||
## Aliases
|
||||
Note: Elixir also converts the switches to underscore atoms, so
|
||||
`--source-path` becomes `:source_path`, to better suit Elixir
|
||||
conventions. This means that option names on the command line cannot contain
|
||||
underscores; such options will be put in the invalid options list.
|
||||
|
||||
A set of aliases can be specified in the `:aliases` option:
|
||||
## Switch Definitions
|
||||
|
||||
iex> OptionParser.parse(["-d"], aliases: [d: :debug])
|
||||
{[debug: true], [], []}
|
||||
Often it is better to explicitly list the known
|
||||
switches and their formats. The switches can be specified via two
|
||||
alternative options:
|
||||
|
||||
## Examples
|
||||
* `:switches` - defines some switches. An attempt is still made to parse
|
||||
switches that do not appear in the list.
|
||||
|
||||
Here are some examples of working with different types and modifiers:
|
||||
* `:strict` - the switches are strict. Any switch that is not specified
|
||||
in the list is returned in the invalid options list.
|
||||
|
||||
Note that you should only supply the `:switches` or `:strict` option. If you
|
||||
supply both, an error will be raised.
|
||||
|
||||
For each switch, the following types are supported:
|
||||
|
||||
* `:boolean` - marks the given switch as a boolean. Boolean switches
|
||||
never consume the following value unless it is `true` or
|
||||
`false`.
|
||||
* `:integer` - parses the switch as an integer.
|
||||
* `:float` - parses the switch as a float.
|
||||
* `:string` - returns the switch as a string.
|
||||
|
||||
If a switch can't be parsed, it is returned in the invalid options list.
|
||||
|
||||
The following extra "types" are supported:
|
||||
|
||||
* `:keep` - keeps duplicated items in the list instead of overriding them.
|
||||
|
||||
Note: if you want to use `:keep` with a non-string type, use a list, e.g.
|
||||
`[foo: [:integer, :keep]]`.
|
||||
|
||||
Examples:
|
||||
|
||||
iex> OptionParser.parse(["--unlock", "path/to/file"], strict: [unlock: :boolean])
|
||||
{[unlock: true], ["path/to/file"], []}
|
||||
@@ -174,12 +86,6 @@ defmodule OptionParser do
|
||||
iex> OptionParser.parse(["--limit", "xyz"], strict: [limit: :integer])
|
||||
{[], [], [{"--limit", "xyz"}]}
|
||||
|
||||
iex> OptionParser.parse(["--verbose"], switches: [verbose: :count])
|
||||
{[verbose: 1], [], []}
|
||||
|
||||
iex> OptionParser.parse(["-v", "-v"], aliases: [v: :verbose], strict: [verbose: :count])
|
||||
{[verbose: 2], [], []}
|
||||
|
||||
iex> OptionParser.parse(["--unknown", "xyz"], strict: [])
|
||||
{[], ["xyz"], [{"--unknown", nil}]}
|
||||
|
||||
@@ -190,49 +96,27 @@ defmodule OptionParser do
|
||||
iex> OptionParser.parse(["--unlock", "path/to/file", "--unlock", "path/to/another/file"], strict: [unlock: :keep])
|
||||
{[unlock: "path/to/file", unlock: "path/to/another/file"], [], []}
|
||||
|
||||
## Negation switches
|
||||
|
||||
In case a switch is declared as boolean, it may be passed as `--no-SWITCH`
|
||||
which will set the option to `false`:
|
||||
|
||||
iex> OptionParser.parse(["--no-op", "path/to/file"], switches: [op: :boolean])
|
||||
{[op: false], ["path/to/file"], []}
|
||||
|
||||
## Aliases
|
||||
|
||||
A set of aliases can be given as options too:
|
||||
|
||||
iex> OptionParser.parse(["-d"], aliases: [d: :debug])
|
||||
{[debug: true], [], []}
|
||||
|
||||
"""
|
||||
@spec parse(argv, options) :: {parsed, argv, errors}
|
||||
def parse(argv, opts \\ []) when is_list(argv) and is_list(opts) do
|
||||
do_parse(argv, compile_config(opts), [], [], [], true)
|
||||
end
|
||||
|
||||
@doc """
|
||||
The same as `parse/2` but raises an `OptionParser.ParseError`
|
||||
exception if any invalid options are given.
|
||||
|
||||
If there are no errors, returns a `{parsed, rest}` tuple where:
|
||||
|
||||
* `parsed` is the list of parsed switches (same as in `parse/2`)
|
||||
* `rest` is the list of arguments (same as in `parse/2`)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> OptionParser.parse!(["--debug", "path/to/file"], strict: [debug: :boolean])
|
||||
{[debug: true], ["path/to/file"]}
|
||||
|
||||
iex> OptionParser.parse!(["--limit", "xyz"], strict: [limit: :integer])
|
||||
** (OptionParser.ParseError) 1 error found!
|
||||
--limit : Expected type integer, got "xyz"
|
||||
|
||||
iex> OptionParser.parse!(["--unknown", "xyz"], strict: [])
|
||||
** (OptionParser.ParseError) 1 error found!
|
||||
--unknown : Unknown option
|
||||
|
||||
iex> OptionParser.parse!(["-l", "xyz", "-f", "bar"],
|
||||
...> switches: [limit: :integer, foo: :integer], aliases: [l: :limit, f: :foo])
|
||||
** (OptionParser.ParseError) 2 errors found!
|
||||
-l : Expected type integer, got "xyz"
|
||||
-f : Expected type integer, got "bar"
|
||||
|
||||
"""
|
||||
@spec parse!(argv, options) :: {parsed, argv} | no_return
|
||||
def parse!(argv, opts \\ []) when is_list(argv) and is_list(opts) do
|
||||
case parse(argv, opts) do
|
||||
{parsed, args, []} -> {parsed, args}
|
||||
{_, _, errors} -> raise ParseError, format_errors(errors, opts)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Similar to `parse/2` but only parses the head of `argv`;
|
||||
as soon as it finds a non-switch, it stops parsing.
|
||||
@@ -241,12 +125,10 @@ defmodule OptionParser do
|
||||
|
||||
## Example
|
||||
|
||||
iex> OptionParser.parse_head(["--source", "lib", "test/enum_test.exs", "--verbose"],
|
||||
...> switches: [source: :string, verbose: :boolean])
|
||||
iex> OptionParser.parse_head(["--source", "lib", "test/enum_test.exs", "--verbose"])
|
||||
{[source: "lib"], ["test/enum_test.exs", "--verbose"], []}
|
||||
|
||||
iex> OptionParser.parse_head(["--verbose", "--source", "lib", "test/enum_test.exs", "--unlock"],
|
||||
...> switches: [source: :string, verbose: :boolean, unlock: :boolean])
|
||||
iex> OptionParser.parse_head(["--verbose", "--source", "lib", "test/enum_test.exs", "--unlock"])
|
||||
{[verbose: true, source: "lib"], ["test/enum_test.exs", "--unlock"], []}
|
||||
|
||||
"""
|
||||
@@ -255,67 +137,33 @@ defmodule OptionParser do
|
||||
do_parse(argv, compile_config(opts), [], [], [], false)
|
||||
end
|
||||
|
||||
@doc """
|
||||
The same as `parse_head/2` but raises an `OptionParser.ParseError`
|
||||
exception if any invalid options are given.
|
||||
|
||||
If there are no errors, returns a `{parsed, rest}` tuple where:
|
||||
|
||||
* `parsed` is the list of parsed switches (same as in `parse_head/2`)
|
||||
* `rest` is the list of arguments (same as in `parse_head/2`)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> OptionParser.parse_head!(["--source", "lib", "path/to/file", "--verbose"],
|
||||
...> switches: [source: :string, verbose: :boolean])
|
||||
{[source: "lib"], ["path/to/file", "--verbose"]}
|
||||
|
||||
iex> OptionParser.parse_head!(["--number", "lib", "test/enum_test.exs", "--verbose"],
|
||||
...> strict: [number: :integer])
|
||||
** (OptionParser.ParseError) 1 error found!
|
||||
--number : Expected type integer, got "lib"
|
||||
|
||||
iex> OptionParser.parse_head!(["--verbose", "--source", "lib", "test/enum_test.exs", "--unlock"],
|
||||
...> strict: [verbose: :integer, source: :integer])
|
||||
** (OptionParser.ParseError) 2 errors found!
|
||||
--verbose : Missing argument of type integer
|
||||
--source : Expected type integer, got "lib"
|
||||
"""
|
||||
@spec parse_head!(argv, options) :: {parsed, argv} | no_return
|
||||
def parse_head!(argv, opts \\ []) when is_list(argv) and is_list(opts) do
|
||||
case parse_head(argv, opts) do
|
||||
{parsed, args, []} -> {parsed, args}
|
||||
{_, _, errors} -> raise ParseError, format_errors(errors, opts)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_parse([], _config, opts, args, invalid, _all?) do
|
||||
{Enum.reverse(opts), Enum.reverse(args), Enum.reverse(invalid)}
|
||||
end
|
||||
|
||||
defp do_parse(argv, {aliases, switches, strict?, allow_nonexistent_atoms?} = config, opts, args, invalid, all?) do
|
||||
case next(argv, aliases, switches, strict?, allow_nonexistent_atoms?) do
|
||||
defp do_parse(argv, {aliases, switches, strict}=config, opts, args, invalid, all?) do
|
||||
case next(argv, aliases, switches, strict) do
|
||||
{:ok, option, value, rest} ->
|
||||
# the option exists and it was successfully parsed
|
||||
# the option exist and it was successfully parsed
|
||||
kinds = List.wrap Keyword.get(switches, option)
|
||||
new_opts = store_option(opts, option, value, kinds)
|
||||
new_opts = do_store_option(opts, option, value, kinds)
|
||||
do_parse(rest, config, new_opts, args, invalid, all?)
|
||||
|
||||
{:invalid, option, value, rest} ->
|
||||
# the option exist but it has wrong value
|
||||
do_parse(rest, config, opts, args, [{option, value} | invalid], all?)
|
||||
do_parse(rest, config, opts, args, [{option, value}|invalid], all?)
|
||||
|
||||
{:undefined, option, _value, rest} ->
|
||||
# the option does not exist (for strict cases)
|
||||
do_parse(rest, config, opts, args, [{option, nil} | invalid], all?)
|
||||
do_parse(rest, config, opts, args, [{option, nil}|invalid], all?)
|
||||
|
||||
{:error, ["--" | rest]} ->
|
||||
{:error, ["--"|rest]} ->
|
||||
{Enum.reverse(opts), Enum.reverse(args, rest), Enum.reverse(invalid)}
|
||||
|
||||
{:error, [arg | rest] = remaining_args} ->
|
||||
{:error, [arg|rest]=remaining_args} ->
|
||||
# there is no option
|
||||
if all? do
|
||||
do_parse(rest, config, opts, [arg | args], invalid, all?)
|
||||
do_parse(rest, config, opts, [arg|args], invalid, all?)
|
||||
else
|
||||
{Enum.reverse(opts), Enum.reverse(args, remaining_args), Enum.reverse(invalid)}
|
||||
end
|
||||
@@ -326,21 +174,22 @@ defmodule OptionParser do
|
||||
Low-level function that parses one option.
|
||||
|
||||
It accepts the same options as `parse/2` and `parse_head/2`
|
||||
as both functions are built on top of this function. This function
|
||||
as both functions are built on top of next. This function
|
||||
may return:
|
||||
|
||||
* `{:ok, key, value, rest}` - the option `key` with `value` was
|
||||
successfully parsed
|
||||
|
||||
* `{:invalid, key, value, rest}` - the option `key` is invalid with `value`
|
||||
(returned when the value cannot be parsed according to the switch type)
|
||||
(returned when the switch type does not match the one given via the
|
||||
command line)
|
||||
|
||||
* `{:undefined, key, value, rest}` - the option `key` is undefined
|
||||
(returned in strict mode when the switch is unknown)
|
||||
|
||||
* `{:error, rest}` - there are no switches at the head of the given `argv`
|
||||
|
||||
* `{:error, rest}` - there are no switches at the top of the given argv
|
||||
"""
|
||||
|
||||
@spec next(argv, options) ::
|
||||
{:ok, key :: atom, value :: term, argv} |
|
||||
{:invalid, String.t, String.t | nil, argv} |
|
||||
@@ -348,132 +197,79 @@ defmodule OptionParser do
|
||||
{:error, argv}
|
||||
|
||||
def next(argv, opts \\ []) when is_list(argv) and is_list(opts) do
|
||||
{aliases, switches, strict?, allow_nonexistent_atoms?} = compile_config(opts)
|
||||
next(argv, aliases, switches, strict?, allow_nonexistent_atoms?)
|
||||
{aliases, switches, strict} = compile_config(opts)
|
||||
next(argv, aliases, switches, strict)
|
||||
end
|
||||
|
||||
defp next([], _aliases, _switches, _strict?, _allow_nonexistent_atoms?) do
|
||||
defp next([], _aliases, _switches, _strict) do
|
||||
{:error, []}
|
||||
end
|
||||
|
||||
defp next(["--" | _] = argv, _aliases, _switches, _strict?, _allow_nonexistent_atoms?) do
|
||||
defp next(["--"|_]=argv, _aliases, _switches, _strict) do
|
||||
{:error, argv}
|
||||
end
|
||||
|
||||
defp next(["-" | _] = argv, _aliases, _switches, _strict?, _allow_nonexistent_atoms?) do
|
||||
defp next(["-"|_]=argv, _aliases, _switches, _strict) do
|
||||
{:error, argv}
|
||||
end
|
||||
|
||||
defp next(["- " <> _ | _] = argv, _aliases, _switches, _strict?, _allow_nonexistent_atoms?) do
|
||||
defp next(["- " <> _|_]=argv, _aliases, _switches, _strict) do
|
||||
{:error, argv}
|
||||
end
|
||||
|
||||
# Handles --foo or --foo=bar
|
||||
defp next(["--" <> option | rest], _aliases, switches, strict?, allow_nonexistent_atoms?) do
|
||||
defp next(["-" <> option|rest], aliases, switches, strict) do
|
||||
{option, value} = split_option(option)
|
||||
tagged = tag_option(option, switches, allow_nonexistent_atoms?)
|
||||
do_next(tagged, value, "--" <> option, rest, switches, strict?, allow_nonexistent_atoms?)
|
||||
end
|
||||
opt_name_bin = "-" <> option
|
||||
tagged = tag_option(option, switches, aliases)
|
||||
|
||||
# Handles -a, -abc, -abc=something
|
||||
defp next(["-" <> option | rest] = argv, aliases, switches, strict?, allow_nonexistent_atoms?) do
|
||||
{option, value} = split_option(option)
|
||||
original = "-" <> option
|
||||
|
||||
cond do
|
||||
is_nil(value) and negative_number?(original) ->
|
||||
{:error, argv}
|
||||
String.contains?(option, ["-", "_"]) ->
|
||||
{:undefined, original, value, rest}
|
||||
String.length(option) > 1 ->
|
||||
key = get_option_key(option, allow_nonexistent_atoms?)
|
||||
option_key = aliases[key]
|
||||
if key && option_key do
|
||||
IO.warn "multi-letter aliases are deprecated, got: #{inspect(key)}"
|
||||
do_next({:default, option_key}, value, original, rest, switches, strict?, allow_nonexistent_atoms?)
|
||||
else
|
||||
next(expand_multiletter_alias(option, value) ++ rest, aliases, switches, strict?, allow_nonexistent_atoms?)
|
||||
end
|
||||
true ->
|
||||
# We have a regular one-letter alias here
|
||||
tagged = tag_oneletter_alias(option, aliases, allow_nonexistent_atoms?)
|
||||
do_next(tagged, value, original, rest, switches, strict?, allow_nonexistent_atoms?)
|
||||
end
|
||||
end
|
||||
|
||||
defp next(argv, _aliases, _switches, _strict?, _allow_nonexistent_atoms?) do
|
||||
{:error, argv}
|
||||
end
|
||||
|
||||
defp do_next(tagged, value, original, rest, switches, strict?, allow_nonexistent_atoms?) do
|
||||
if strict? and not option_defined?(tagged, switches) do
|
||||
{:undefined, original, value, rest}
|
||||
if strict and not option_defined?(tagged, switches) do
|
||||
{:undefined, opt_name_bin, value, rest}
|
||||
else
|
||||
{option, kinds, value} = normalize_option(tagged, value, switches)
|
||||
{value, kinds, rest} = normalize_value(value, kinds, rest, strict?)
|
||||
{opt_name, kinds, value} = normalize_option(tagged, value, switches)
|
||||
{value, kinds, rest} = normalize_value(value, kinds, rest, strict)
|
||||
case validate_option(value, kinds) do
|
||||
{:ok, new_value} -> {:ok, option, new_value, rest}
|
||||
:invalid -> {:invalid, original, value, rest}
|
||||
{:ok, new_value} -> {:ok, opt_name, new_value, rest}
|
||||
:invalid -> {:invalid, opt_name_bin, value, rest}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp next(argv, _aliases, _switches, _strict) do
|
||||
{:error, argv}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a key-value enumerable and converts it to `t:argv/0`.
|
||||
Receives a key-value enumerable and converts it to argv.
|
||||
|
||||
Keys must be atoms. Keys with `nil` value are discarded,
|
||||
Keys must be atoms. Keys with nil value are discarded,
|
||||
boolean values are converted to `--key` or `--no-key`
|
||||
(if the value is `true` or `false`, respectively),
|
||||
and all other values are converted using `Kernel.to_string/1`.
|
||||
|
||||
It is advised to pass to `to_argv/2` the same set of `options`
|
||||
given to `parse/2`. Some switches can only be reconstructed
|
||||
correctly with the `switches` information in hand.
|
||||
and all other values are converted using `to_string/1`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> OptionParser.to_argv([foo_bar: "baz"])
|
||||
["--foo-bar", "baz"]
|
||||
|
||||
iex> OptionParser.to_argv([bool: true, bool: false, discarded: nil])
|
||||
["--bool", "--no-bool"]
|
||||
|
||||
Some switches will output different values based on the switches
|
||||
flag:
|
||||
|
||||
iex> OptionParser.to_argv([number: 2], switches: [])
|
||||
["--number", "2"]
|
||||
iex> OptionParser.to_argv([number: 2], switches: [number: :count])
|
||||
["--number", "--number"]
|
||||
|
||||
"""
|
||||
@spec to_argv(Enumerable.t, options) :: argv
|
||||
def to_argv(enum, opts \\ []) do
|
||||
switches = Keyword.get(opts, :switches, [])
|
||||
@spec to_argv(Enumerable.t) :: argv
|
||||
def to_argv(enum) do
|
||||
Enum.flat_map(enum, fn
|
||||
{_key, nil} -> []
|
||||
{key, true} -> [to_switch(key)]
|
||||
{key, false} -> [to_switch(key, "--no-")]
|
||||
{key, value} -> to_argv(key, value, switches)
|
||||
{key, value} -> [to_switch(key), to_string(value)]
|
||||
end)
|
||||
end
|
||||
|
||||
defp to_argv(key, value, switches) do
|
||||
if switches[key] == :count do
|
||||
List.duplicate(to_switch(key), value)
|
||||
else
|
||||
[to_switch(key), to_string(value)]
|
||||
end
|
||||
end
|
||||
|
||||
defp to_switch(key, prefix \\ "--") when is_atom(key) do
|
||||
prefix <> String.replace(Atom.to_string(key), "_", "-")
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Splits a string into `t:argv/0` chunks.
|
||||
|
||||
This function splits the given `string` into a list of strings in a similar
|
||||
way to many shells.
|
||||
Splits a string into argv chunks.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -482,11 +278,10 @@ defmodule OptionParser do
|
||||
|
||||
iex> OptionParser.split("foo \"bar baz\"")
|
||||
["foo", "bar baz"]
|
||||
|
||||
"""
|
||||
@spec split(String.t) :: argv
|
||||
def split(string) do
|
||||
do_split(String.trim_leading(string, " "), "", [], nil)
|
||||
do_split(strip_leading_spaces(string), "", [], nil)
|
||||
end
|
||||
|
||||
# If we have an escaped quote, simply remove the escape
|
||||
@@ -507,7 +302,7 @@ defmodule OptionParser do
|
||||
|
||||
# If we have space and we are outside of a quote, start new segment
|
||||
defp do_split(<<?\s, t::binary>>, buffer, acc, nil),
|
||||
do: do_split(String.trim_leading(t, " "), "", [buffer | acc], nil)
|
||||
do: do_split(strip_leading_spaces(t), "", [buffer|acc], nil)
|
||||
|
||||
# All other characters are moved to buffer
|
||||
defp do_split(<<h, t::binary>>, buffer, acc, quote) do
|
||||
@@ -519,117 +314,88 @@ defmodule OptionParser do
|
||||
do: Enum.reverse(acc)
|
||||
|
||||
defp do_split(<<>>, buffer, acc, nil),
|
||||
do: Enum.reverse([buffer | acc])
|
||||
do: Enum.reverse([buffer|acc])
|
||||
|
||||
# Otherwise raise
|
||||
defp do_split(<<>>, _, _acc, marker) do
|
||||
raise "argv string did not terminate properly, a #{<<marker>>} was opened but never closed"
|
||||
end
|
||||
|
||||
defp strip_leading_spaces(" " <> t), do: strip_leading_spaces(t)
|
||||
defp strip_leading_spaces(t), do: t
|
||||
|
||||
## Helpers
|
||||
|
||||
defp compile_config(opts) do
|
||||
aliases = opts[:aliases] || []
|
||||
allow_nonexistent_atoms? = opts[:allow_nonexistent_atoms] || false
|
||||
|
||||
{switches, strict?} = cond do
|
||||
{switches, strict} = cond do
|
||||
opts[:switches] && opts[:strict] ->
|
||||
raise ArgumentError, ":switches and :strict cannot be given together"
|
||||
switches = opts[:switches] ->
|
||||
{switches, false}
|
||||
strict = opts[:strict] ->
|
||||
{strict, true}
|
||||
s = opts[:switches] ->
|
||||
{s, false}
|
||||
s = opts[:strict] ->
|
||||
{s, true}
|
||||
true ->
|
||||
{[], false}
|
||||
end
|
||||
|
||||
{aliases, switches, strict?, allow_nonexistent_atoms?}
|
||||
{aliases, switches, strict}
|
||||
end
|
||||
|
||||
defp validate_option(value, kinds) do
|
||||
{invalid?, value} =
|
||||
cond do
|
||||
:invalid in kinds ->
|
||||
{true, value}
|
||||
:boolean in kinds ->
|
||||
case value do
|
||||
t when t in [true, "true"] -> {false, true}
|
||||
f when f in [false, "false"] -> {false, false}
|
||||
_ -> {true, value}
|
||||
end
|
||||
:count in kinds ->
|
||||
case value do
|
||||
1 -> {false, value}
|
||||
_ -> {true, value}
|
||||
end
|
||||
:integer in kinds ->
|
||||
case Integer.parse(value) do
|
||||
{value, ""} -> {false, value}
|
||||
_ -> {true, value}
|
||||
end
|
||||
:float in kinds ->
|
||||
case Float.parse(value) do
|
||||
{value, ""} -> {false, value}
|
||||
_ -> {true, value}
|
||||
end
|
||||
true ->
|
||||
{false, value}
|
||||
end
|
||||
{is_invalid, value} = cond do
|
||||
:invalid in kinds ->
|
||||
{true, value}
|
||||
:boolean in kinds ->
|
||||
case value do
|
||||
t when t in [true, "true"] -> {nil, true}
|
||||
f when f in [false, "false"] -> {nil, false}
|
||||
_ -> {true, value}
|
||||
end
|
||||
:integer in kinds ->
|
||||
case Integer.parse(value) do
|
||||
{value, ""} -> {nil, value}
|
||||
_ -> {true, value}
|
||||
end
|
||||
:float in kinds ->
|
||||
case Float.parse(value) do
|
||||
{value, ""} -> {nil, value}
|
||||
_ -> {true, value}
|
||||
end
|
||||
true ->
|
||||
{nil, value}
|
||||
end
|
||||
|
||||
if invalid? do
|
||||
if is_invalid do
|
||||
:invalid
|
||||
else
|
||||
{:ok, value}
|
||||
end
|
||||
end
|
||||
|
||||
defp store_option(dict, option, value, kinds) do
|
||||
defp do_store_option(dict, option, value, kinds) do
|
||||
cond do
|
||||
:count in kinds ->
|
||||
Keyword.update(dict, option, value, & &1 + 1)
|
||||
:keep in kinds ->
|
||||
[{option, value} | dict]
|
||||
[{option, value}|dict]
|
||||
true ->
|
||||
[{option, value} | Keyword.delete(dict, option)]
|
||||
[{option, value}|Keyword.delete(dict, option)]
|
||||
end
|
||||
end
|
||||
|
||||
defp tag_option("no-" <> option = original, switches, allow_nonexistent_atoms?) do
|
||||
cond do
|
||||
(negated = get_option_key(option, allow_nonexistent_atoms?)) && :boolean in List.wrap(switches[negated]) ->
|
||||
{:negated, negated}
|
||||
option_key = get_option_key(original, allow_nonexistent_atoms?) ->
|
||||
{:default, option_key}
|
||||
true ->
|
||||
:unknown
|
||||
end
|
||||
defp tag_option(<<?-, option::binary>>, switches, _aliases) do
|
||||
get_negated(option, switches)
|
||||
end
|
||||
|
||||
defp tag_option(option, _switches, allow_nonexistent_atoms?) do
|
||||
if option_key = get_option_key(option, allow_nonexistent_atoms?) do
|
||||
{:default, option_key}
|
||||
defp tag_option(option, _switches, aliases) when is_binary(option) do
|
||||
opt = get_option(option)
|
||||
if alias = aliases[opt] do
|
||||
{:default, alias}
|
||||
else
|
||||
:unknown
|
||||
end
|
||||
end
|
||||
|
||||
defp tag_oneletter_alias(alias, aliases, allow_nonexistent_atoms?) when is_binary(alias) do
|
||||
if option_key = aliases[to_existing_key(alias, allow_nonexistent_atoms?)] do
|
||||
{:default, option_key}
|
||||
else
|
||||
:unknown
|
||||
end
|
||||
end
|
||||
|
||||
defp expand_multiletter_alias(letters, value) when is_binary(letters) do
|
||||
{last, expanded} =
|
||||
letters
|
||||
|> String.codepoints()
|
||||
|> Enum.map(&("-" <> &1))
|
||||
|> List.pop_at(-1)
|
||||
expanded ++ [last <> if(value, do: "=" <> value, else: "")]
|
||||
end
|
||||
|
||||
defp option_defined?(:unknown, _switches) do
|
||||
false
|
||||
end
|
||||
@@ -658,33 +424,30 @@ defmodule OptionParser do
|
||||
{option, List.wrap(switches[option]), value}
|
||||
end
|
||||
|
||||
defp normalize_value(nil, kinds, t, strict?) do
|
||||
defp normalize_value(nil, kinds, t, strict) do
|
||||
nil_or_true = if strict, do: nil, else: true
|
||||
cond do
|
||||
:boolean in kinds ->
|
||||
{true, kinds, t}
|
||||
:count in kinds ->
|
||||
{1, kinds, t}
|
||||
value_in_tail?(t) ->
|
||||
[h | t] = t
|
||||
[h|t] = t
|
||||
{h, kinds, t}
|
||||
kinds == [] and strict? ->
|
||||
{nil, kinds, t}
|
||||
kinds == [] ->
|
||||
{true, kinds, t}
|
||||
{nil_or_true, kinds, t}
|
||||
true ->
|
||||
{nil, [:invalid], t}
|
||||
end
|
||||
end
|
||||
|
||||
defp normalize_value(value, kinds, t, _strict?) do
|
||||
defp normalize_value(value, kinds, t, _) do
|
||||
{value, kinds, t}
|
||||
end
|
||||
|
||||
defp value_in_tail?(["-" | _]), do: true
|
||||
defp value_in_tail?(["- " <> _ | _]), do: true
|
||||
defp value_in_tail?(["-" <> arg | _]), do: negative_number?("-" <> arg)
|
||||
defp value_in_tail?([]), do: false
|
||||
defp value_in_tail?(_), do: true
|
||||
defp value_in_tail?(["-"|_]), do: true
|
||||
defp value_in_tail?(["- " <> _|_]), do: true
|
||||
defp value_in_tail?(["-" <> _|_]), do: false
|
||||
defp value_in_tail?([]), do: false
|
||||
defp value_in_tail?(_), do: true
|
||||
|
||||
defp split_option(option) do
|
||||
case :binary.split(option, "=") do
|
||||
@@ -693,66 +456,40 @@ defmodule OptionParser do
|
||||
end
|
||||
end
|
||||
|
||||
defp to_underscore(option),
|
||||
do: to_underscore(option, <<>>)
|
||||
defp to_underscore("_" <> _rest, _acc),
|
||||
do: nil
|
||||
defp to_underscore(option), do: to_underscore(option, <<>>)
|
||||
|
||||
defp to_underscore("_" <> _rest, _acc), do: nil
|
||||
|
||||
defp to_underscore("-" <> rest, acc),
|
||||
do: to_underscore(rest, acc <> "_")
|
||||
|
||||
defp to_underscore(<<c>> <> rest, acc),
|
||||
do: to_underscore(rest, <<acc::binary, c>>)
|
||||
defp to_underscore(<<>>, acc),
|
||||
do: acc
|
||||
|
||||
def get_option_key(option, allow_nonexistent_atoms?) do
|
||||
if string = to_underscore(option) do
|
||||
to_existing_key(string, allow_nonexistent_atoms?)
|
||||
defp to_underscore(<<>>, acc), do: acc
|
||||
|
||||
defp get_option(option) do
|
||||
if str = to_underscore(option) do
|
||||
String.to_atom(str)
|
||||
end
|
||||
end
|
||||
|
||||
defp to_existing_key(option, true),
|
||||
do: String.to_atom(option)
|
||||
defp to_existing_key(option, false) do
|
||||
try do
|
||||
String.to_existing_atom(option)
|
||||
rescue
|
||||
ArgumentError -> nil
|
||||
defp get_negated("no-" <> rest = original, switches) do
|
||||
cond do
|
||||
(negated = get_option(rest)) && :boolean in List.wrap(switches[negated]) ->
|
||||
{:negated, negated}
|
||||
option = get_option(original) ->
|
||||
{:default, option}
|
||||
true ->
|
||||
:unknown
|
||||
end
|
||||
end
|
||||
|
||||
defp negative_number?(arg) do
|
||||
match?({_, ""}, Float.parse(arg))
|
||||
end
|
||||
|
||||
defp format_errors([_ | _] = errors, opts) do
|
||||
types = opts[:switches] || opts[:strict]
|
||||
error_count = length(errors)
|
||||
error = if error_count == 1, do: "error", else: "errors"
|
||||
"#{error_count} #{error} found!\n" <>
|
||||
Enum.map_join(errors, "\n", &format_error(&1, opts, types))
|
||||
end
|
||||
|
||||
defp format_error({option, nil}, opts, types) do
|
||||
if type = get_type(option, opts, types) do
|
||||
"#{option} : Missing argument of type #{type}"
|
||||
defp get_negated(rest, _switches) do
|
||||
if option = get_option(rest) do
|
||||
{:default, option}
|
||||
else
|
||||
"#{option} : Unknown option"
|
||||
end
|
||||
end
|
||||
|
||||
defp format_error({option, value}, opts, types) do
|
||||
type = get_type(option, opts, types)
|
||||
"#{option} : Expected type #{type}, got #{inspect value}"
|
||||
end
|
||||
|
||||
defp get_type(option, opts, types) do
|
||||
allow_nonexistent_atoms? = opts[:allow_nonexistent_atoms] || false
|
||||
key = option |> String.trim_leading("-") |> get_option_key(allow_nonexistent_atoms?)
|
||||
|
||||
if option_key = opts[:aliases][key] do
|
||||
types[option_key]
|
||||
else
|
||||
types[key]
|
||||
:unknown
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+66
-67
@@ -3,13 +3,13 @@ defmodule Path do
|
||||
This module provides conveniences for manipulating or
|
||||
retrieving file system paths.
|
||||
|
||||
The functions in this module may receive a chardata as
|
||||
The functions in this module may receive a char data as
|
||||
argument (i.e. a string or a list of characters / string)
|
||||
and will always return a string (encoded in UTF-8).
|
||||
|
||||
The majority of the functions in this module do not
|
||||
interact with the file system, except for a few functions
|
||||
that require it (like `wildcard/2` and `expand/1`).
|
||||
that require it (like `wildcard/1` and `expand/1`).
|
||||
"""
|
||||
|
||||
alias :filename, as: FN
|
||||
@@ -69,15 +69,15 @@ defmodule Path do
|
||||
end
|
||||
|
||||
# Absolute path on current drive
|
||||
defp absname_vr(["/" | rest], [volume | _], _relative),
|
||||
do: absname_join([volume | rest])
|
||||
defp absname_vr(["/"|rest], [volume|_], _relative),
|
||||
do: absname_join([volume|rest])
|
||||
|
||||
# Relative to current directory on current drive.
|
||||
defp absname_vr([<<x, ?:>> | rest], [<<x, _::binary>> | _], relative),
|
||||
defp absname_vr([<<x, ?:>>|rest], [<<x, _::binary>>|_], relative),
|
||||
do: absname(absname_join(rest), relative)
|
||||
|
||||
# Relative to current directory on another drive.
|
||||
defp absname_vr([<<x, ?:>> | name], _, _relative) do
|
||||
defp absname_vr([<<x, ?:>>|name], _, _relative) do
|
||||
cwd =
|
||||
case :file.get_cwd([x, ?:]) do
|
||||
{:ok, dir} -> IO.chardata_to_string(dir)
|
||||
@@ -87,8 +87,8 @@ defmodule Path do
|
||||
end
|
||||
|
||||
# Joins a list
|
||||
defp absname_join([name1, name2 | rest]), do:
|
||||
absname_join([absname_join(name1, name2) | rest])
|
||||
defp absname_join([name1, name2|rest]), do:
|
||||
absname_join([absname_join(name1, name2)|rest])
|
||||
defp absname_join([name]), do:
|
||||
do_absname_join(IO.chardata_to_string(name), <<>>, [], major_os_type())
|
||||
|
||||
@@ -100,28 +100,28 @@ defmodule Path do
|
||||
do_absname_join(rest, relativename, [?:, uc_letter+?a-?A], :win32)
|
||||
defp do_absname_join(<<?\\, rest::binary>>, relativename, result, :win32), do:
|
||||
do_absname_join(<<?/, rest::binary>>, relativename, result, :win32)
|
||||
defp do_absname_join(<<?/, rest::binary>>, relativename, [?., ?/ | result], os_type), do:
|
||||
do_absname_join(rest, relativename, [?/ | result], os_type)
|
||||
defp do_absname_join(<<?/, rest::binary>>, relativename, [?/ | result], os_type), do:
|
||||
do_absname_join(rest, relativename, [?/ | result], os_type)
|
||||
defp do_absname_join(<<?/, rest::binary>>, relativename, [?., ?/|result], os_type), do:
|
||||
do_absname_join(rest, relativename, [?/|result], os_type)
|
||||
defp do_absname_join(<<?/, rest::binary>>, relativename, [?/|result], os_type), do:
|
||||
do_absname_join(rest, relativename, [?/|result], os_type)
|
||||
defp do_absname_join(<<>>, <<>>, result, os_type), do:
|
||||
IO.iodata_to_binary(reverse_maybe_remove_dir_sep(result, os_type))
|
||||
defp do_absname_join(<<>>, relativename, [?: | rest], :win32), do:
|
||||
do_absname_join(relativename, <<>>, [?: | rest], :win32)
|
||||
defp do_absname_join(<<>>, relativename, [?/ | result], os_type), do:
|
||||
do_absname_join(relativename, <<>>, [?/ | result], os_type)
|
||||
IO.iodata_to_binary(reverse_maybe_remove_dirsep(result, os_type))
|
||||
defp do_absname_join(<<>>, relativename, [?:|rest], :win32), do:
|
||||
do_absname_join(relativename, <<>>, [?:|rest], :win32)
|
||||
defp do_absname_join(<<>>, relativename, [?/|result], os_type), do:
|
||||
do_absname_join(relativename, <<>>, [?/|result], os_type)
|
||||
defp do_absname_join(<<>>, relativename, result, os_type), do:
|
||||
do_absname_join(relativename, <<>>, [?/ | result], os_type)
|
||||
do_absname_join(relativename, <<>>, [?/|result], os_type)
|
||||
defp do_absname_join(<<char, rest::binary>>, relativename, result, os_type), do:
|
||||
do_absname_join(rest, relativename, [char | result], os_type)
|
||||
do_absname_join(rest, relativename, [char|result], os_type)
|
||||
|
||||
defp reverse_maybe_remove_dir_sep([?/, ?:, letter], :win32), do:
|
||||
defp reverse_maybe_remove_dirsep([?/, ?:, letter], :win32), do:
|
||||
[letter, ?:, ?/]
|
||||
defp reverse_maybe_remove_dir_sep([?/], _), do:
|
||||
defp reverse_maybe_remove_dirsep([?/], _), do:
|
||||
[?/]
|
||||
defp reverse_maybe_remove_dir_sep([?/ | name], _), do:
|
||||
defp reverse_maybe_remove_dirsep([?/|name], _), do:
|
||||
:lists.reverse(name)
|
||||
defp reverse_maybe_remove_dir_sep(name, _), do:
|
||||
defp reverse_maybe_remove_dirsep(name, _), do:
|
||||
:lists.reverse(name)
|
||||
|
||||
@doc """
|
||||
@@ -131,7 +131,7 @@ defmodule Path do
|
||||
## Examples
|
||||
|
||||
Path.expand("/foo/bar/../bar")
|
||||
#=> "/foo/bar"
|
||||
"/foo/bar"
|
||||
|
||||
"""
|
||||
@spec expand(t) :: binary
|
||||
@@ -187,7 +187,7 @@ defmodule Path do
|
||||
"""
|
||||
@spec type(t) :: :absolute | :relative | :volumerelative
|
||||
def type(name) when is_list(name) or is_binary(name) do
|
||||
pathtype(name, major_os_type()) |> elem(0)
|
||||
pathtype(name, major_os_type) |> elem(0)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -227,19 +227,19 @@ defmodule Path do
|
||||
|
||||
defp unix_pathtype(<<?/, relative::binary>>), do:
|
||||
{:absolute, relative}
|
||||
defp unix_pathtype([?/ | relative]), do:
|
||||
defp unix_pathtype([?/|relative]), do:
|
||||
{:absolute, relative}
|
||||
defp unix_pathtype([list | rest]) when is_list(list), do:
|
||||
defp unix_pathtype([list|rest]) when is_list(list), do:
|
||||
unix_pathtype(list ++ rest)
|
||||
defp unix_pathtype(relative), do:
|
||||
{:relative, relative}
|
||||
|
||||
@slash [?/, ?\\]
|
||||
|
||||
defp win32_pathtype([list | rest]) when is_list(list), do:
|
||||
defp win32_pathtype([list|rest]) when is_list(list), do:
|
||||
win32_pathtype(list++rest)
|
||||
defp win32_pathtype([char, list | rest]) when is_list(list), do:
|
||||
win32_pathtype([char | list++rest])
|
||||
defp win32_pathtype([char, list|rest]) when is_list(list), do:
|
||||
win32_pathtype([char|list++rest])
|
||||
defp win32_pathtype(<<c1, c2, relative::binary>>) when c1 in @slash and c2 in @slash, do:
|
||||
{:absolute, relative}
|
||||
defp win32_pathtype(<<c, relative::binary>>) when c in @slash, do:
|
||||
@@ -253,8 +253,8 @@ defmodule Path do
|
||||
{:absolute, relative}
|
||||
defp win32_pathtype([c | relative]) when c in @slash, do:
|
||||
{:volumerelative, relative}
|
||||
defp win32_pathtype([c1, c2, list | rest]) when is_list(list), do:
|
||||
win32_pathtype([c1, c2 | list++rest])
|
||||
defp win32_pathtype([c1, c2, list|rest]) when is_list(list), do:
|
||||
win32_pathtype([c1, c2|list++rest])
|
||||
defp win32_pathtype([_letter, ?:, c | relative]) when c in @slash, do:
|
||||
{:absolute, relative}
|
||||
defp win32_pathtype([_letter, ?: | relative]), do:
|
||||
@@ -290,11 +290,11 @@ defmodule Path do
|
||||
relative_to(split(path), split(from), path)
|
||||
end
|
||||
|
||||
defp relative_to([h | t1], [h | t2], original) do
|
||||
defp relative_to([h|t1], [h|t2], original) do
|
||||
relative_to(t1, t2, original)
|
||||
end
|
||||
|
||||
defp relative_to([_ | _] = l1, [], _original) do
|
||||
defp relative_to([_|_] = l1, [], _original) do
|
||||
join(l1)
|
||||
end
|
||||
|
||||
@@ -363,11 +363,10 @@ defmodule Path do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Path.dirname("/foo/bar.ex")
|
||||
"/foo"
|
||||
|
||||
iex> Path.dirname("/foo/bar/baz.ex")
|
||||
"/foo/bar"
|
||||
Path.dirname("/foo/bar.ex")
|
||||
#=> "/foo"
|
||||
Path.dirname("/foo/bar/baz.ex")
|
||||
#=> "/foo/bar"
|
||||
|
||||
"""
|
||||
@spec dirname(t) :: binary
|
||||
@@ -428,10 +427,10 @@ defmodule Path do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Joins a list of paths.
|
||||
Joins a list of strings.
|
||||
|
||||
This function should be used to convert a list of paths to a path.
|
||||
Note that any trailing slash is removed when joining.
|
||||
This function should be used to convert a list of strings to a path.
|
||||
Note that any trailing slash is removed on join.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -445,11 +444,11 @@ defmodule Path do
|
||||
"/foo/bar"
|
||||
|
||||
"""
|
||||
@spec join(nonempty_list(t)) :: binary
|
||||
def join([name1, name2 | rest]), do:
|
||||
join([join(name1, name2) | rest])
|
||||
@spec join([t]) :: binary
|
||||
def join([name1, name2|rest]), do:
|
||||
join([join(name1, name2)|rest])
|
||||
def join([name]), do:
|
||||
IO.chardata_to_string(name)
|
||||
name
|
||||
|
||||
@doc """
|
||||
Joins two paths.
|
||||
@@ -467,16 +466,15 @@ defmodule Path do
|
||||
def join(left, right) do
|
||||
left = IO.chardata_to_string(left)
|
||||
os_type = major_os_type()
|
||||
do_join(left, right, os_type) |> remove_dir_sep(os_type)
|
||||
do_join(left, right, os_type) |> remove_dirsep(os_type)
|
||||
end
|
||||
|
||||
defp do_join("", right, os_type), do: relative(right, os_type)
|
||||
defp do_join("/", right, os_type), do: "/" <> relative(right, os_type)
|
||||
defp do_join(left, right, os_type), do: remove_dir_sep(left, os_type) <> "/" <> relative(right, os_type)
|
||||
defp do_join(left, "", _os_type), do: left
|
||||
defp do_join(left, right, os_type), do: remove_dirsep(left, os_type) <> "/" <> relative(right, os_type)
|
||||
|
||||
defp remove_dir_sep("", _os_type), do: ""
|
||||
defp remove_dir_sep("/", _os_type), do: "/"
|
||||
defp remove_dir_sep(bin, os_type) do
|
||||
defp remove_dirsep("", _os_type), do: ""
|
||||
defp remove_dirsep(bin, os_type) do
|
||||
last = :binary.last(bin)
|
||||
if last == ?/ or (last == ?\\ and os_type == :win32) do
|
||||
binary_part(bin, 0, byte_size(bin) - 1)
|
||||
@@ -522,8 +520,13 @@ defmodule Path do
|
||||
call({:read_link_info, file})
|
||||
end
|
||||
|
||||
# For compatibility with buggy Erlang 17.1.
|
||||
def read_file_info(file) do
|
||||
call({:read_link_info, file})
|
||||
end
|
||||
|
||||
def list_dir(dir) do
|
||||
case call({:list_dir, dir}) do
|
||||
case call({:list_dir, dir}) do
|
||||
{:ok, files} ->
|
||||
{:ok, for(file <- files, hd(file) != ?., do: file)}
|
||||
other ->
|
||||
@@ -557,14 +560,10 @@ defmodule Path do
|
||||
* `**` - two adjacent `*`'s used as a single pattern will match all
|
||||
files and zero or more directories and subdirectories
|
||||
|
||||
* `[char1,char2,...]` - matches any of the characters listed; two
|
||||
characters separated by a hyphen will match a range of characters.
|
||||
Do not add spaces before and after the comma as it would then match
|
||||
paths containing the space character itself.
|
||||
* `[char1, char2, ...]` - matches any of the characters listed; two
|
||||
characters separated by a hyphen will match a range of characters
|
||||
|
||||
* `{item1,item2,...}` - matches one of the alternatives
|
||||
Do not add spaces before and after the comma as it would then match
|
||||
paths containing the space character itself.
|
||||
* `{item1, item2, ...}` - matches one of the alternatives
|
||||
|
||||
Other characters represent themselves. Only paths that have
|
||||
exactly the same character in the same position will match. Note
|
||||
@@ -620,7 +619,7 @@ defmodule Path do
|
||||
defp resolve_home(""), do: System.user_home!
|
||||
|
||||
defp resolve_home(rest) do
|
||||
case {rest, major_os_type()} do
|
||||
case {rest, major_os_type} do
|
||||
{"\\" <> _, :win32} ->
|
||||
System.user_home! <> rest
|
||||
{"/" <> _, _} ->
|
||||
@@ -639,17 +638,17 @@ defmodule Path do
|
||||
defp do_expand_dot(path),
|
||||
do: do_expand_dot(:binary.split(path, "/", [:global]), [])
|
||||
|
||||
defp do_expand_dot([".." | t], [_, _ | acc]),
|
||||
defp do_expand_dot([".."|t], [_, _|acc]),
|
||||
do: do_expand_dot(t, acc)
|
||||
defp do_expand_dot([".." | t], []),
|
||||
defp do_expand_dot([".."|t], []),
|
||||
do: do_expand_dot(t, [])
|
||||
defp do_expand_dot(["." | t], acc),
|
||||
defp do_expand_dot(["."|t], acc),
|
||||
do: do_expand_dot(t, acc)
|
||||
defp do_expand_dot([h | t], acc),
|
||||
do: do_expand_dot(t, ["/", h | acc])
|
||||
defp do_expand_dot([h|t], acc),
|
||||
do: do_expand_dot(t, ["/", h|acc])
|
||||
defp do_expand_dot([], []),
|
||||
do: ""
|
||||
defp do_expand_dot([], ["/" | acc]),
|
||||
defp do_expand_dot([], ["/"|acc]),
|
||||
do: IO.iodata_to_binary(:lists.reverse(acc))
|
||||
|
||||
defp major_os_type do
|
||||
|
||||
+55
-177
@@ -1,186 +1,32 @@
|
||||
defmodule Port do
|
||||
@moduledoc ~S"""
|
||||
Functions for interacting with the external world through ports.
|
||||
|
||||
Ports provide a mechanism to start operating system processes external
|
||||
to the Erlang VM and communicate with them via message passing.
|
||||
|
||||
## Example
|
||||
|
||||
iex> port = Port.open({:spawn, "cat"}, [:binary])
|
||||
iex> send port, {self(), {:command, "hello"}}
|
||||
iex> send port, {self(), {:command, "world"}}
|
||||
iex> flush()
|
||||
{#Port<0.1444>, {:data, "hello"}}
|
||||
{#Port<0.1444>, {:data, "world"}}
|
||||
iex> send port, {self(), :close}
|
||||
:ok
|
||||
iex> flush()
|
||||
{#Port<0.1464>, :closed}
|
||||
:ok
|
||||
|
||||
In the example above, we have created a new port that executes the
|
||||
program `cat`. `cat` is a program available on UNIX systems that
|
||||
receives data from multiple inputs and concatenates them in the output.
|
||||
|
||||
After the port was created, we sent it two commands in the form of
|
||||
messages using `Kernel.send/2`. The first command has the binary payload
|
||||
of "hello" and the second has "world".
|
||||
|
||||
After sending those two messages, we invoked the IEx helper `flush()`,
|
||||
which printed all messages received from the port, in this case we got
|
||||
"hello" and "world" back. Notice the messages are in binary because we
|
||||
passed the `:binary` option when opening the port in `Port.open/2`. Without
|
||||
such option, it would have yielded a list of bytes.
|
||||
|
||||
Once everything was done, we closed the port.
|
||||
|
||||
Elixir provides many conveniences for working with ports and some drawbacks.
|
||||
We will explore those below.
|
||||
|
||||
## Message and function APIs
|
||||
|
||||
There are two APIs for working with ports. It can be either asynchronous via
|
||||
message passing, as in the example above, or by calling the functions on this
|
||||
module.
|
||||
|
||||
The messages supported by ports and their counterpart function APIs are
|
||||
listed below:
|
||||
|
||||
* `{pid, {:command, binary}}` - sends the given data to the port.
|
||||
See `command/3`.
|
||||
|
||||
* `{pid, :close}` - closes the port. Unless the port is already closed,
|
||||
the port will reply with `{port, :closed}` message once it has flushed
|
||||
its buffers and effectively closed. See `close/1`.
|
||||
|
||||
* `{pid, {:connect, new_pid}}` - sets the `new_pid` as the new owner of
|
||||
the port. Once a port is opened, the port is linked and connected to the
|
||||
caller process and communication to the port only happens through the
|
||||
connected process. This message makes `new_pid` the new connected processes.
|
||||
Unless the port is dead, the port will reply to the old owner with
|
||||
`{port, :connected}`. See `connect/2`.
|
||||
|
||||
On its turn, the port will send the connected process the following messages:
|
||||
|
||||
* `{port, {:port, data}}` - data sent by the port
|
||||
* `{port, :closed}` - reply to the `{pid, :close}` message
|
||||
* `{port, :connected}` - reply to the `{pid, {:connect, new_pid}}` message
|
||||
* `{:EXIT, port, reason}` - exit signals in case the port crashes and the
|
||||
owner process is trapping exits
|
||||
|
||||
## Open mechanisms
|
||||
|
||||
The port can be opened through four main mechanisms.
|
||||
|
||||
As a short summary, prefer to using the `:spawn` and `:spawn_executable`
|
||||
options mentioned below. The other two options, `:spawn_driver` and `:fd`
|
||||
are for advanced usage within the VM. Also consider using `System.cmd/3`
|
||||
if all you want is to execute a program and retrieve its return value.
|
||||
|
||||
### spawn
|
||||
|
||||
The `:spawn` tuple receives a binary that is going to be executed as a
|
||||
full invocation. For example, we can use it to invoke "echo hello" directly:
|
||||
|
||||
iex> port = Port.open({:spawn, "echo oops"}, [:binary])
|
||||
iex> flush()
|
||||
{#Port<0.1444>, {:data, "oops\n"}}
|
||||
|
||||
`:spawn` will retrieve the program name from the argument and traverse your
|
||||
OS `$PATH` environment variable looking for a matching program.
|
||||
|
||||
Although the above is handy, it means it is impossible to invoke an executable
|
||||
that has whitespaces on its name or in any of its arguments. For those reasons,
|
||||
most times it is preferrable to execute `:spawn_executable`.
|
||||
|
||||
### spawn_executable
|
||||
|
||||
Spawn executable is a more restricted and explicit version of spawn. It expects
|
||||
full file paths to the executable you want to execute. If they are in your `$PATH`,
|
||||
they can be retrieved by calling `System.find_executable/1`:
|
||||
|
||||
iex> path = System.find_executable("echo")
|
||||
iex> port = Port.open({:spawn_executable, path}, [:binary, args: ["hello world"]])
|
||||
iex> flush()
|
||||
{#Port<0.1380>, {:data, "hello world\n"}}
|
||||
|
||||
When using `:spawn_executable`, the list of arguments can be passed via
|
||||
the `:args` option as done above. For the full list of options, see the
|
||||
documentation for the Erlang function `:erlang.open_port/2`.
|
||||
|
||||
### spawn_driver
|
||||
|
||||
Spawn driver is used to start Port Drivers, which are programs written in
|
||||
C that implements a specific communication protocols and are dynamically
|
||||
linked to the Erlang VM. Port drivers are an advanced topic and one of the
|
||||
mechanisms for integrating C code, alongside NIFs. For more information,
|
||||
[please check the Erlang docs](http://erlang.org/doc/reference_manual/ports.html).
|
||||
|
||||
### fd
|
||||
|
||||
The `:fd` name option allows developers to access `in` and `out` file
|
||||
descriptors used by the Erlang VM. You would use those only if you are
|
||||
reimplementing core part of the Runtime System, such as the `:user` and
|
||||
`:shell` processes.
|
||||
|
||||
## Zombie processes
|
||||
|
||||
A port can be closed via the `close/1` function or by sending a `{pid, :close}`
|
||||
message. However, if the VM crashes, a long-running program started by the port
|
||||
will have its stdin and stdout channels closed but **it won't be automatically
|
||||
terminated**.
|
||||
|
||||
While most UNIX command line tools will exit once its communication channels
|
||||
are closed, not all command line applications will do so. While we encourage
|
||||
graceful termination by detecting if stdin/stdout has been closed, we do not
|
||||
always have control over how 3rd party software terminates. In those cases,
|
||||
you can wrap the application in a script that checks for stdin. Here is such
|
||||
script in bash:
|
||||
|
||||
#!/bin/sh
|
||||
"$@"
|
||||
pid=$!
|
||||
while read line ; do
|
||||
:
|
||||
done
|
||||
kill -KILL $pid
|
||||
|
||||
|
||||
Now instead of:
|
||||
|
||||
Port.open({:spawn_executable, "/path/to/program"},
|
||||
[args: ["a", "b", "c"]])
|
||||
|
||||
You may invoke:
|
||||
|
||||
Port.open({:spawn_executable, "/path/to/wrapper"},
|
||||
[args: ["/path/to/program", "a", "b", "c"]])
|
||||
|
||||
@moduledoc """
|
||||
Functions related to Erlang ports.
|
||||
"""
|
||||
|
||||
@type name :: {:spawn, charlist | binary} |
|
||||
{:spawn_driver, charlist | binary} |
|
||||
{:spawn_executable, charlist | atom} |
|
||||
@type name :: {:spawn, char_list | binary} |
|
||||
{:spawn_driver, char_list | binary} |
|
||||
{:spawn_executable, char_list | atom} |
|
||||
{:fd, non_neg_integer, non_neg_integer}
|
||||
|
||||
@doc """
|
||||
Opens a port given a tuple `name` and a list of `options`.
|
||||
Opens an Erlang port given a tuple `name` and a list of `settings`.
|
||||
|
||||
The module documentation above contains documentation and examples
|
||||
for the supported `name` values, summarized below:
|
||||
## Name
|
||||
|
||||
* `{:spawn, command}` - runs an external program. `command` must contain
|
||||
the program name and optionally a list of arguments separated by space.
|
||||
If passing programs or arguments with space in their name, use the next option.
|
||||
* `{:spawn_executable, filename}` - runs the executable given by the absolute
|
||||
file name `filename`. Arguments can be passed via the `:args` option.
|
||||
* `{:spawn_driver, command}` - spawns so-called port drivers.
|
||||
* `{:fd, fd_in, fd_out}` - accesses file descriptors, `fd_in` and `fd_out`
|
||||
opened by the VM.
|
||||
The supported values for `name` are:
|
||||
|
||||
For more information and the list of options, see
|
||||
[`:erlang.open_port/2`](http://www.erlang.org/doc/man/erlang.html#open_port-2).
|
||||
* `{:spawn, command}` - to run an external program. The first space separated
|
||||
word of `command` will be considered as the name of the program to run, so
|
||||
use `{:spawn_executable, command}` to run a program having spaces in its name.
|
||||
* `{:spawn_driver, command}` - similar to `{:spawn, command}`, but to run a
|
||||
loaded driver.
|
||||
* `{:spawn_executable, filename}` - similar to `{:spawn, filename}`, but to run
|
||||
an external executable. With this option, `filename` in its whole is considered
|
||||
the name of the program to execute.
|
||||
* `{:fd, fd_in, fd_out}` - to access file descriptors used by Erlang, `fd_in`
|
||||
being used for standard input, `fd_out` for standard output.
|
||||
|
||||
For more information, see [`:erlang.open_port/2`](http://www.erlang.org/doc/man/erlang.html#open_port-2).
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@@ -226,7 +72,36 @@ defmodule Port do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns information about the `port` or `nil` if the port is closed.
|
||||
Sends a synchronous control command to the `port` and returns its reply as a binary.
|
||||
|
||||
Not all port drivers support this feature.
|
||||
|
||||
For more information, see [`:erlang.port_control/3`](http://www.erlang.org/doc/man/erlang.html#port_control-3).
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec control(port, integer, iodata) :: iodata | binary
|
||||
def control(port, operation, data) do
|
||||
:erlang.port_control(port, operation, data)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Makes a synchronous call to the `port` and returns its reply as a term.
|
||||
|
||||
Not all port drivers support this control feature.
|
||||
|
||||
For more information, see [`:erlang.port_call/3`](http://www.erlang.org/doc/man/erlang.html#port_call-3).
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec call(port, integer, term) :: term
|
||||
def call(port, operation, data) do
|
||||
:erlang.port_call(port, operation, data)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns information about the `port`
|
||||
or `nil` if the port is closed.
|
||||
|
||||
For more information, see [`:erlang.port_info/1`](http://www.erlang.org/doc/man/erlang.html#port_info-1).
|
||||
"""
|
||||
@@ -235,7 +110,8 @@ defmodule Port do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns information about the `port` or `nil` if the port is closed.
|
||||
Returns information about the `port`
|
||||
or `nil` if the port is closed.
|
||||
|
||||
For more information, see [`:erlang.port_info/2`](http://www.erlang.org/doc/man/erlang.html#port_info-2).
|
||||
"""
|
||||
@@ -254,7 +130,9 @@ defmodule Port do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list of all ports in the current node.
|
||||
Returns a list of the ports for the current node.
|
||||
|
||||
For more information, see [`:erlang.ports/0`](http://www.erlang.org/doc/man/erlang.html#ports-0).
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
|
||||
+55
-159
@@ -28,7 +28,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all key-value pairs in the process dictionary.
|
||||
Returns all key-values in the dictionary.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@@ -38,7 +38,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value for the given `key` or `default` if `key` is not set.
|
||||
Returns the value for the given `key`.
|
||||
"""
|
||||
@spec get(term) :: term
|
||||
@spec get(term, default :: term) :: term
|
||||
@@ -72,7 +72,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stores the given `key`-`value` pair in the process dictionary.
|
||||
Stores the given key-value in the process dictionary.
|
||||
|
||||
The return value is the value that was previously stored under the key `key`
|
||||
(or `nil` in case no value was stored under `key`).
|
||||
@@ -83,7 +83,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes the given `key` from the process dictionary.
|
||||
Deletes the given `key` from the dictionary.
|
||||
"""
|
||||
@spec delete(term) :: term | nil
|
||||
def delete(key) do
|
||||
@@ -91,26 +91,23 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends an exit signal with the given `reason` to the `pid`.
|
||||
Sends an exit signal with the given reason to the pid.
|
||||
|
||||
The following behaviour applies if `reason` is any term except `:normal`
|
||||
or `:kill`:
|
||||
The following behaviour applies if reason is any term except `:normal` or `:kill`:
|
||||
|
||||
1. If `pid` is not trapping exits, `pid` will exit with the given
|
||||
`reason`.
|
||||
1. If pid is not trapping exits, pid will exit with the given reason.
|
||||
|
||||
2. If `pid` is trapping exits, the exit signal is transformed into a
|
||||
message `{:EXIT, from, reason}` and delivered to the message queue
|
||||
of `pid`.
|
||||
2. If pid is trapping exits, the exit signal is transformed into a message
|
||||
`{:EXIT, from, reason}` and delivered to the message queue of pid.
|
||||
|
||||
If `reason` is the atom `:normal`, `pid` will not exit (unless `pid` is
|
||||
the calling process, in which case it will exit with the reason `:normal`).
|
||||
If it is trapping exits, the exit signal is transformed into a message
|
||||
`{:EXIT, from, :normal}` and delivered to its message queue.
|
||||
3. If reason is the atom `:normal`, pid will not exit (unless it is the calling
|
||||
process's pid, in which case it will exit with the reason `:normal`).
|
||||
If it is trapping exits, the exit signal is transformed into a message
|
||||
`{:EXIT, from, :normal}` and delivered to its message queue.
|
||||
|
||||
If `reason` is the atom `:kill`, that is if `exit(pid, :kill)` is called,
|
||||
an untrappable exit signal is sent to `pid` which will unconditionally exit
|
||||
with reason `:killed`.
|
||||
4. If reason is the atom `:kill`, that is if `exit(pid, :kill)` is called,
|
||||
an untrappable exit signal is sent to pid which will unconditionally
|
||||
exit with exit reason `:killed`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -124,80 +121,6 @@ defmodule Process do
|
||||
:erlang.exit(pid, reason)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sleeps the current process by `timeout`.
|
||||
|
||||
`timeout` is either the number of milliseconds to sleep as an
|
||||
integer or the atom `:infinity`. When `:infinity` is given,
|
||||
the current process will suspend forever.
|
||||
|
||||
**Use this function with extreme care**. For almost all situations
|
||||
where you would use `sleep/1` in Elixir, there is likely a
|
||||
more correct, faster and precise way of achieving it with
|
||||
message passing.
|
||||
|
||||
For example, if you are waiting a process to perform some
|
||||
action, it is better to communicate.
|
||||
|
||||
In other words, **do not**:
|
||||
|
||||
Task.start_link fn ->
|
||||
do_something()
|
||||
...
|
||||
end
|
||||
|
||||
# Wait until work is done
|
||||
Process.sleep(2000)
|
||||
|
||||
But **do**:
|
||||
|
||||
parent = self()
|
||||
Task.start_link fn ->
|
||||
do_something()
|
||||
send parent, :work_is_done
|
||||
...
|
||||
end
|
||||
|
||||
receive do
|
||||
:work_is_done -> :ok
|
||||
after
|
||||
30_000 -> :timeout # Optional timeout
|
||||
end
|
||||
|
||||
Or even use `Task.async/1` and `Task.await/2` in the example
|
||||
above.
|
||||
|
||||
Similarly, if you are waiting for a process to terminate,
|
||||
use monitor instead of sleep. **Do not**:
|
||||
|
||||
Task.start_link fn ->
|
||||
...
|
||||
end
|
||||
|
||||
# Wait until task terminates
|
||||
Process.sleep(2000)
|
||||
|
||||
Instead **do**:
|
||||
|
||||
{:ok, pid} =
|
||||
Task.start_link fn ->
|
||||
...
|
||||
end
|
||||
|
||||
ref = Process.monitor(pid)
|
||||
receive do
|
||||
{:DOWN, ^ref, _, _, _} -> :task_is_down
|
||||
after
|
||||
30_000 -> :timeout # Optional timeout
|
||||
end
|
||||
|
||||
"""
|
||||
def sleep(timeout)
|
||||
when is_integer(timeout) and timeout >= 0
|
||||
when timeout == :infinity do
|
||||
receive after: (timeout -> :ok)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends a message to the given process.
|
||||
|
||||
@@ -216,10 +139,10 @@ defmodule Process do
|
||||
:noconnect
|
||||
|
||||
"""
|
||||
@spec send(dest, msg, [option]) :: :ok | :noconnect | :nosuspend
|
||||
when dest: pid | port | atom | {atom, node},
|
||||
msg: any,
|
||||
option: :noconnect | :nosuspend
|
||||
@spec send(dest, msg, [option]) :: :ok | :noconnect | :nosuspend when
|
||||
dest: pid | port | atom | {atom, node},
|
||||
msg: any,
|
||||
option: :noconnect | :nosuspend
|
||||
def send(dest, msg, options) do
|
||||
:erlang.send(dest, msg, options)
|
||||
end
|
||||
@@ -227,7 +150,7 @@ defmodule Process do
|
||||
@doc """
|
||||
Sends `msg` to `dest` after `time` milliseconds.
|
||||
|
||||
If `dest` is a PID, it must be the PID of a local process, dead or alive.
|
||||
If `dest` is a pid, it must be the pid of a local process, dead or alive.
|
||||
If `dest` is an atom, it must be the name of a registered process
|
||||
which is looked up at the time of delivery. No error is given if the name does
|
||||
not refer to a process.
|
||||
@@ -235,31 +158,21 @@ defmodule Process do
|
||||
This function returns a timer reference, which can be read or canceled with
|
||||
`read_timer/1` and `cancel_timer/1`.
|
||||
|
||||
Finally, the timer will be automatically canceled if the given `dest` is a PID
|
||||
which is not alive or when the given PID exits. Note that timers will not be
|
||||
Finally, the timer will be automatically canceled if the given `dest` is a pid
|
||||
which is not alive or when the given pid exits. Note that timers will not be
|
||||
automatically canceled when `dest` is an atom (as the atom resolution is done
|
||||
on delivery).
|
||||
|
||||
## Options
|
||||
|
||||
* `:abs` - (boolean) when `false`, `time` is treated as relative to the
|
||||
current monotonic time. When `true`, `time` is the absolute value of the
|
||||
Erlang monotonic time at which `msg` should be delivered to `dest`.
|
||||
To read more about Erlang monotonic time and other time-related concepts,
|
||||
look at the documentation for the `System` module. Defaults to `false`.
|
||||
|
||||
"""
|
||||
@spec send_after(pid | atom, term, non_neg_integer, [option]) :: reference
|
||||
when option: {:abs, boolean}
|
||||
def send_after(dest, msg, time, opts \\ []) do
|
||||
:erlang.send_after(time, dest, msg, opts)
|
||||
@spec send_after(pid | atom, term, non_neg_integer) :: reference
|
||||
def send_after(dest, msg, time) do
|
||||
:erlang.send_after(time, dest, msg)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Cancels a timer created by `send_after/3`.
|
||||
|
||||
When the result is an integer, it represents the time in milliseconds
|
||||
left until the timer would have expired.
|
||||
When the result is an integer, it represents the time in milli-seconds
|
||||
left until the timer will expire.
|
||||
|
||||
When the result is `false`, a timer corresponding to `timer_ref` could
|
||||
not be found. This can be either because the timer expired, already has
|
||||
@@ -278,7 +191,7 @@ defmodule Process do
|
||||
@doc """
|
||||
Reads a timer created by `send_after/3`.
|
||||
|
||||
When the result is an integer, it represents the time in milliseconds
|
||||
When the result is an integer, it represents the time in milli-seconds
|
||||
left until the timer will expire.
|
||||
|
||||
When the result is `false`, a timer corresponding to `timer_ref` could
|
||||
@@ -302,12 +215,13 @@ defmodule Process do
|
||||
@type spawn_opts :: [spawn_opt]
|
||||
|
||||
@doc """
|
||||
Spawns the given function according to the given options.
|
||||
Spawns the given module and function passing the given args
|
||||
according to the given options.
|
||||
|
||||
The result depends on the given options. In particular,
|
||||
if `:monitor` is given as an option, it will return a tuple
|
||||
containing the PID and the monitoring reference, otherwise
|
||||
just the spawned process PID.
|
||||
containing the pid and the monitoring reference, otherwise
|
||||
just the spawned process pid.
|
||||
|
||||
It also accepts extra options, for the list of available options
|
||||
check [`:erlang.spawn_opt/4`](http://www.erlang.org/doc/man/erlang.html#spawn_opt-4).
|
||||
@@ -320,13 +234,13 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Spawns the given function from module `mod`, passing the given `args`
|
||||
Spawns the given module and function passing the given args
|
||||
according to the given options.
|
||||
|
||||
The result depends on the given options. In particular,
|
||||
if `:monitor` is given as an option, it will return a tuple
|
||||
containing the PID and the monitoring reference, otherwise
|
||||
just the spawned process PID.
|
||||
containing the pid and the monitoring reference, otherwise
|
||||
just the spawned process pid.
|
||||
|
||||
It also accepts extra options, for the list of available options
|
||||
check [`:erlang.spawn_opt/4`](http://www.erlang.org/doc/man/erlang.html#spawn_opt-4).
|
||||
@@ -339,7 +253,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
The calling process starts monitoring the given `item`.
|
||||
The calling process starts monitoring the item given.
|
||||
It returns the monitor reference.
|
||||
|
||||
See [the need for monitoring](http://elixir-lang.org/getting-started/mix-otp/genserver.html#the-need-for-monitoring)
|
||||
@@ -411,41 +325,23 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Registers the given `pid_or_port` under the given `name`.
|
||||
Associates the name with a pid or a port identifier. `name`, which must
|
||||
be an atom, can be used instead of the pid / port identifier with the
|
||||
`Kernel.send/2` function.
|
||||
|
||||
`name` must be an atom and can then be used instead of the
|
||||
PID/port identifier when sending messages with `Kernel.send/2`.
|
||||
|
||||
`register/2` will fail with `ArgumentError` if the PID/Port is
|
||||
not existing locally and alive, if the name is already registered
|
||||
or if the `pid_or_port` is already registered to a different `name`.
|
||||
|
||||
The following names are reserved and cannot be assigned to
|
||||
processes nor ports: `nil`, `false`, `true` or `:undefined`.
|
||||
`Process.register/2` will fail with `ArgumentError` if the pid supplied
|
||||
is no longer alive, (check with `alive?/1`) or if the name is
|
||||
already registered (check with `whereis/1`).
|
||||
"""
|
||||
@spec register(pid | port, atom) :: true
|
||||
def register(pid_or_port, name) when is_atom(name) and not name in [nil, false, true, :undefined] do
|
||||
:erlang.register(name, pid_or_port)
|
||||
catch
|
||||
:error, :badarg when node(pid_or_port) != node() ->
|
||||
message = "could not register the #{pid_or_port pid_or_port} because it belongs to another node"
|
||||
:erlang.error ArgumentError.exception(message), [pid_or_port, name]
|
||||
:error, :badarg ->
|
||||
message = "could not register the #{pid_or_port pid_or_port} with " <>
|
||||
"name #{inspect name}. Or it is not alive, or the name is already " <>
|
||||
"taken, or it has already been given another name"
|
||||
:erlang.error ArgumentError.exception(message), [pid_or_port, name]
|
||||
def register(pid, name) when not name in [nil, false, true] do
|
||||
:erlang.register(name, pid)
|
||||
end
|
||||
|
||||
defp pid_or_port(pid) when is_pid(pid), do: "pid #{inspect pid}"
|
||||
defp pid_or_port(port) when is_port(port), do: "port #{inspect port}"
|
||||
|
||||
@doc """
|
||||
Removes the registered `name`, associated with a PID
|
||||
or a port identifier.
|
||||
Removes the registered name, associated with a pid or a port identifier.
|
||||
|
||||
Fails with `ArgumentError` if the name is not registered
|
||||
to any PID or port.
|
||||
See [`:erlang.unregister/1`](http://www.erlang.org/doc/man/erlang.html#unregister-1) for more info.
|
||||
"""
|
||||
@spec unregister(atom) :: true
|
||||
def unregister(name) do
|
||||
@@ -453,7 +349,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the PID or port identifier with the registered `name`.
|
||||
Returns the pid or port identifier with the registered name.
|
||||
Returns `nil` if the name is not registered.
|
||||
|
||||
See [`:erlang.whereis/1`](http://www.erlang.org/doc/man/erlang.html#whereis-1) for more info.
|
||||
@@ -464,7 +360,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the PID of the group leader for the process which evaluates the function.
|
||||
Returns the pid of the group leader for the process which evaluates the function.
|
||||
"""
|
||||
@spec group_leader :: pid
|
||||
def group_leader do
|
||||
@@ -493,7 +389,7 @@ defmodule Process do
|
||||
:sensitive
|
||||
@doc """
|
||||
Sets certain flags for the process which calls this function.
|
||||
Returns the old value of the `flag`.
|
||||
Returns the old value of the flag.
|
||||
|
||||
See [`:erlang.process_flag/2`](http://www.erlang.org/doc/man/erlang.html#process_flag-2) for more info.
|
||||
"""
|
||||
@@ -504,8 +400,8 @@ defmodule Process do
|
||||
|
||||
@doc """
|
||||
Sets certain flags for the process `pid`, in the same manner as `flag/2`.
|
||||
Returns the old value of the `flag`. The allowed values for `flag` are
|
||||
only a subset of those allowed in `flag/2`, namely `:save_calls`.
|
||||
Returns the old value of the flag. The allowed values for `flag` are
|
||||
only a subset of those allowed in `flag/2`, namely: `save_calls`.
|
||||
|
||||
See [`:erlang.process_flag/3`](http://www.erlang.org/doc/man/erlang.html#process_flag-3) for more info.
|
||||
"""
|
||||
@@ -515,7 +411,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns information about the process identified by `pid`, or returns `nil` if the process
|
||||
Returns information about the process identified by `pid` or `nil` if the process
|
||||
is not alive.
|
||||
Use this only for debugging information.
|
||||
|
||||
@@ -527,8 +423,8 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns information about the process identified by `pid`,
|
||||
or returns `nil` if the process is not alive.
|
||||
Returns information about the process identified by `pid`
|
||||
or `nil` if the process is not alive.
|
||||
|
||||
See [`:erlang.process_info/2`](http://www.erlang.org/doc/man/erlang.html#process_info-2) for more info.
|
||||
"""
|
||||
|
||||
+47
-54
@@ -31,7 +31,7 @@ defmodule Protocol do
|
||||
name = unquote(name)
|
||||
arity = unquote(arity)
|
||||
|
||||
@functions [{name, arity} | @functions]
|
||||
@functions [{name, arity}|@functions]
|
||||
|
||||
# Generate a fake definition with the user
|
||||
# signature that will be used by docs
|
||||
@@ -56,7 +56,7 @@ defmodule Protocol do
|
||||
@doc """
|
||||
Checks if the given module is loaded and is protocol.
|
||||
|
||||
Returns `:ok` if so, otherwise raises `ArgumentError`.
|
||||
Returns `:ok` if so, otherwise raises ArgumentError.
|
||||
"""
|
||||
@spec assert_protocol!(module) :: :ok | no_return
|
||||
def assert_protocol!(module) do
|
||||
@@ -83,7 +83,7 @@ defmodule Protocol do
|
||||
Checks if the given module is loaded and is an implementation
|
||||
of the given protocol.
|
||||
|
||||
Returns `:ok` if so, otherwise raises `ArgumentError`.
|
||||
Returns `:ok` if so, otherwise raises ArgumentError.
|
||||
"""
|
||||
@spec assert_impl!(module, module) :: :ok | no_return
|
||||
def assert_impl!(protocol, base) do
|
||||
@@ -129,8 +129,8 @@ defmodule Protocol do
|
||||
@doc """
|
||||
Extracts all protocols from the given paths.
|
||||
|
||||
The paths can be either a charlist or a string. Internally
|
||||
they are worked on as charlists, so passing them as lists
|
||||
The paths can be either a char list or a string. Internally
|
||||
they are worked on as char lists, so passing them as lists
|
||||
avoid extra conversion.
|
||||
|
||||
Does not load any of the protocols.
|
||||
@@ -144,7 +144,7 @@ defmodule Protocol do
|
||||
true
|
||||
|
||||
"""
|
||||
@spec extract_protocols([charlist | String.t]) :: [atom]
|
||||
@spec extract_protocols([char_list | String.t]) :: [atom]
|
||||
def extract_protocols(paths) do
|
||||
extract_matching_by_attribute paths, 'Elixir.',
|
||||
fn module, attributes ->
|
||||
@@ -159,8 +159,8 @@ defmodule Protocol do
|
||||
Extracts all types implemented for the given protocol from
|
||||
the given paths.
|
||||
|
||||
The paths can be either a charlist or a string. Internally
|
||||
they are worked on as charlists, so passing them as lists
|
||||
The paths can be either a char list or a string. Internally
|
||||
they are worked on as char lists, so passing them as lists
|
||||
avoid extra conversion.
|
||||
|
||||
Does not load any of the implementations.
|
||||
@@ -174,9 +174,9 @@ defmodule Protocol do
|
||||
true
|
||||
|
||||
"""
|
||||
@spec extract_impls(module, [charlist | String.t]) :: [atom]
|
||||
@spec extract_impls(module, [char_list | String.t]) :: [atom]
|
||||
def extract_impls(protocol, paths) when is_atom(protocol) do
|
||||
prefix = Atom.to_charlist(protocol) ++ '.'
|
||||
prefix = Atom.to_char_list(protocol) ++ '.'
|
||||
extract_matching_by_attribute paths, prefix, fn
|
||||
_mod, attributes ->
|
||||
case attributes[:impl] do
|
||||
@@ -200,7 +200,7 @@ defmodule Protocol do
|
||||
end
|
||||
end
|
||||
|
||||
defp list_dir(path), do: list_dir(to_charlist(path))
|
||||
defp list_dir(path), do: list_dir(to_char_list(path))
|
||||
|
||||
defp extract_from_file(path, file, prefix, callback) do
|
||||
if :lists.prefix(prefix, file) and :filename.extension(file) == '.beam' do
|
||||
@@ -290,7 +290,7 @@ defmodule Protocol do
|
||||
# impl_for/1 dispatch version.
|
||||
defp change_debug_info({protocol, any, code, docs}, types) do
|
||||
types = if any, do: types, else: List.delete(types, Any)
|
||||
all = [Any] ++ for {_guard, mod} <- __builtin__(), do: mod
|
||||
all = [Any] ++ for {_guard, mod} <- builtin, do: mod
|
||||
structs = types -- all
|
||||
case change_impl_for(code, protocol, types, structs, false, []) do
|
||||
{:ok, ret} -> {:ok, ret, docs}
|
||||
@@ -298,7 +298,7 @@ defmodule Protocol do
|
||||
end
|
||||
end
|
||||
|
||||
defp change_impl_for([{:function, line, :__protocol__, 1, clauses} | t], protocol, types, structs, _, acc) do
|
||||
defp change_impl_for([{:function, line, :__protocol__, 1, clauses}|t], protocol, types, structs, _, acc) do
|
||||
clauses = :lists.map(fn
|
||||
{:clause, l, [{:atom, _, :consolidated?}], [], [{:atom, _, _}]} ->
|
||||
{:clause, l, [{:atom, 0, :consolidated?}], [], [{:atom, 0, true}]}
|
||||
@@ -307,34 +307,34 @@ defmodule Protocol do
|
||||
end, clauses)
|
||||
|
||||
change_impl_for(t, protocol, types, structs, true,
|
||||
[{:function, line, :__protocol__, 1, clauses} | acc])
|
||||
[{:function, line, :__protocol__, 1, clauses}|acc])
|
||||
end
|
||||
|
||||
defp change_impl_for([{:function, line, :impl_for, 1, _} | t], protocol, types, structs, is_protocol, acc) do
|
||||
defp change_impl_for([{:function, line, :impl_for, 1, _}|t], protocol, types, structs, is_protocol, acc) do
|
||||
fallback = if Any in types, do: load_impl(protocol, Any)
|
||||
|
||||
clauses = for {guard, mod} <- __builtin__(),
|
||||
clauses = for {guard, mod} <- builtin,
|
||||
mod in types,
|
||||
do: builtin_clause_for(mod, guard, protocol, line)
|
||||
|
||||
clauses = [struct_clause_for(line) | clauses] ++
|
||||
clauses = [struct_clause_for(line)|clauses] ++
|
||||
[fallback_clause_for(fallback, protocol, line)]
|
||||
|
||||
change_impl_for(t, protocol, types, structs, is_protocol,
|
||||
[{:function, line, :impl_for, 1, clauses} | acc])
|
||||
[{:function, line, :impl_for, 1, clauses}|acc])
|
||||
end
|
||||
|
||||
defp change_impl_for([{:function, line, :struct_impl_for, 1, _} | t], protocol, types, structs, is_protocol, acc) do
|
||||
defp change_impl_for([{:function, line, :struct_impl_for, 1, _}|t], protocol, types, structs, is_protocol, acc) do
|
||||
fallback = if Any in types, do: load_impl(protocol, Any)
|
||||
clauses = for struct <- structs, do: each_struct_clause_for(struct, protocol, line)
|
||||
clauses = clauses ++ [fallback_clause_for(fallback, protocol, line)]
|
||||
|
||||
change_impl_for(t, protocol, types, structs, is_protocol,
|
||||
[{:function, line, :struct_impl_for, 1, clauses} | acc])
|
||||
[{:function, line, :struct_impl_for, 1, clauses}|acc])
|
||||
end
|
||||
|
||||
defp change_impl_for([h | t], protocol, info, types, is_protocol, acc) do
|
||||
change_impl_for(t, protocol, info, types, is_protocol, [h | acc])
|
||||
defp change_impl_for([h|t], protocol, info, types, is_protocol, acc) do
|
||||
change_impl_for(t, protocol, info, types, is_protocol, [h|acc])
|
||||
end
|
||||
|
||||
defp change_impl_for([], protocol, _info, _types, is_protocol, acc) do
|
||||
@@ -386,12 +386,11 @@ defmodule Protocol do
|
||||
# Finally compile the module and emit its bytecode.
|
||||
defp compile({protocol, code}, docs) do
|
||||
opts = if Code.compiler_options[:debug_info], do: [:debug_info], else: []
|
||||
{:ok, ^protocol, binary, _warnings} = :compile.forms(code, [:return | opts])
|
||||
{:ok,
|
||||
case docs do
|
||||
:missing_chunk -> binary
|
||||
_ -> :elixir_module.add_beam_chunk(binary, @docs_chunk, docs)
|
||||
end}
|
||||
{:ok, ^protocol, binary, _warnings} = :compile.forms(code, [:return|opts])
|
||||
unless docs == :missing_chunk do
|
||||
binary = :elixir_module.add_beam_chunk(binary, @docs_chunk, docs)
|
||||
end
|
||||
{:ok, binary}
|
||||
end
|
||||
|
||||
## Definition callbacks
|
||||
@@ -420,15 +419,14 @@ defmodule Protocol do
|
||||
_ = unquote(block)
|
||||
|
||||
# Finalize expansion
|
||||
unquote(after_defprotocol())
|
||||
unquote(after_defprotocol)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp after_defprotocol do
|
||||
quote bind_quoted: [builtin: __builtin__()] do
|
||||
@doc false
|
||||
@spec impl_for(term) :: atom | nil
|
||||
quote bind_quoted: [builtin: builtin] do
|
||||
@spec impl_for(term) :: atom() | nil
|
||||
Kernel.def impl_for(data)
|
||||
|
||||
# Define the implementation for structs.
|
||||
@@ -439,39 +437,28 @@ defmodule Protocol do
|
||||
struct_impl_for(struct)
|
||||
end
|
||||
|
||||
# Define the implementation for built-ins
|
||||
# Define the implementation for builtins.
|
||||
:lists.foreach(fn {guard, mod} ->
|
||||
target = Module.concat(__MODULE__, mod)
|
||||
|
||||
Kernel.def impl_for(data) when :erlang.unquote(guard)(data) do
|
||||
case impl_for?(unquote(target)) do
|
||||
true -> unquote(target).__impl__(:target)
|
||||
false -> any_impl_for()
|
||||
false -> any_impl_for
|
||||
end
|
||||
end
|
||||
end, builtin)
|
||||
|
||||
# Define a catch-all impl_for/1 clause to pacify Dialyzer (since
|
||||
# destructuring opaque types is illegal, Dialyzer will think none of the
|
||||
# previous clauses matches opaque types, and without this clause, will
|
||||
# conclude that impl_for can't handle an opaque argument). This is a hack
|
||||
# since it relies on Dialyzer not being smart enough to conclude that all
|
||||
# opaque types will get the any_impl_for/0 implementation.
|
||||
Kernel.def impl_for(_) do
|
||||
any_impl_for()
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec impl_for!(term) :: atom | no_return
|
||||
@spec impl_for!(term) :: atom() | no_return()
|
||||
Kernel.def impl_for!(data) do
|
||||
impl_for(data) || raise(Protocol.UndefinedError, protocol: __MODULE__, value: data)
|
||||
end
|
||||
|
||||
# Internal handler for Any
|
||||
if @fallback_to_any do
|
||||
Kernel.defp any_impl_for(), do: __MODULE__.Any.__impl__(:target)
|
||||
Kernel.defp any_impl_for, do: __MODULE__.Any.__impl__(:target)
|
||||
else
|
||||
Kernel.defp any_impl_for(), do: nil
|
||||
Kernel.defp any_impl_for, do: nil
|
||||
end
|
||||
|
||||
# Internal handler for Structs
|
||||
@@ -479,7 +466,7 @@ defmodule Protocol do
|
||||
target = Module.concat(__MODULE__, struct)
|
||||
case impl_for?(target) do
|
||||
true -> target.__impl__(:target)
|
||||
false -> any_impl_for()
|
||||
false -> any_impl_for
|
||||
end
|
||||
end
|
||||
|
||||
@@ -514,7 +501,7 @@ defmodule Protocol do
|
||||
@doc false
|
||||
def __functions_spec__([]),
|
||||
do: []
|
||||
def __functions_spec__([h | t]),
|
||||
def __functions_spec__([h|t]),
|
||||
do: [:lists.foldl(&{:|, [], [&1, &2]}, h, t), quote(do: ...)]
|
||||
|
||||
@doc false
|
||||
@@ -546,8 +533,15 @@ defmodule Protocol do
|
||||
for = unquote(for)
|
||||
name = Module.concat(protocol, for)
|
||||
|
||||
Protocol.assert_protocol!(protocol)
|
||||
Protocol.__ensure_defimpl__(protocol, for, __ENV__)
|
||||
# TODO: Remove this by 1.3
|
||||
if Atom.to_string(protocol) =~ "Elixir.Access" do
|
||||
:elixir_errors.warn __ENV__.line, __ENV__.file,
|
||||
"implementation of the Access protocol is deprecated. For customization of " <>
|
||||
"the dict[key] syntax, please implement the Dict behaviour instead"
|
||||
else
|
||||
Protocol.assert_protocol!(protocol)
|
||||
Protocol.__ensure_defimpl__(protocol, for, __ENV__)
|
||||
end
|
||||
|
||||
defmodule name do
|
||||
@behaviour protocol
|
||||
@@ -645,8 +639,7 @@ defmodule Protocol do
|
||||
|
||||
## Helpers
|
||||
|
||||
@doc false
|
||||
def __builtin__ do
|
||||
defp builtin do
|
||||
[is_tuple: Tuple,
|
||||
is_atom: Atom,
|
||||
is_list: List,
|
||||
|
||||
+13
-25
@@ -11,7 +11,7 @@ defmodule Range do
|
||||
|
||||
A Range is represented internally as a struct. However,
|
||||
the most common form of creating and matching on ranges
|
||||
is via the `../2` macro, auto-imported from `Kernel`:
|
||||
is via the `../2` macro, auto-imported from Kernel:
|
||||
|
||||
iex> range = 1..3
|
||||
1..3
|
||||
@@ -21,28 +21,13 @@ defmodule Range do
|
||||
iex> last
|
||||
3
|
||||
|
||||
A Range implements the Enumerable protocol, which means
|
||||
all of the functions in the Enum module is available:
|
||||
|
||||
iex> range = 1..10
|
||||
1..10
|
||||
iex> Enum.reduce(range, 0, fn i, acc -> i * i + acc end)
|
||||
385
|
||||
iex> Enum.count(range)
|
||||
10
|
||||
iex> Enum.member?(range, 11)
|
||||
false
|
||||
iex> Enum.member?(range, 8)
|
||||
true
|
||||
|
||||
"""
|
||||
|
||||
defstruct first: nil, last: nil
|
||||
|
||||
@type t :: %Range{first: integer, last: integer}
|
||||
@type t :: %Range{}
|
||||
@type t(first, last) :: %Range{first: first, last: last}
|
||||
|
||||
|
||||
@doc """
|
||||
Creates a new range.
|
||||
"""
|
||||
@@ -58,7 +43,9 @@ defmodule Range do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if the given `term` is a valid range.
|
||||
Returns `true` if the given `term` is a range.
|
||||
|
||||
It does not check if the range is valid.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -69,14 +56,15 @@ defmodule Range do
|
||||
false
|
||||
|
||||
"""
|
||||
@spec range?(term) :: boolean
|
||||
@spec range?(%Range{}) :: true
|
||||
@spec range?(term) :: false
|
||||
def range?(term)
|
||||
def range?(first..last) when is_integer(first) and is_integer(last), do: true
|
||||
def range?(%Range{}), do: true
|
||||
def range?(_), do: false
|
||||
end
|
||||
|
||||
defimpl Enumerable, for: Range do
|
||||
def reduce(first..last, acc, fun) do
|
||||
def reduce(first .. last, acc, fun) do
|
||||
reduce(first, last, acc, fun, last >= first)
|
||||
end
|
||||
|
||||
@@ -100,7 +88,7 @@ defimpl Enumerable, for: Range do
|
||||
{:done, acc}
|
||||
end
|
||||
|
||||
def member?(first..last, value) when is_integer(value) do
|
||||
def member?(first .. last, value) when is_integer(value) do
|
||||
if first <= last do
|
||||
{:ok, first <= value and value <= last}
|
||||
else
|
||||
@@ -108,11 +96,11 @@ defimpl Enumerable, for: Range do
|
||||
end
|
||||
end
|
||||
|
||||
def member?(_.._, _value) do
|
||||
def member?(_ .. _, _value) do
|
||||
{:ok, false}
|
||||
end
|
||||
|
||||
def count(first..last) do
|
||||
def count(first .. last) do
|
||||
if first <= last do
|
||||
{:ok, last - first + 1}
|
||||
else
|
||||
@@ -124,7 +112,7 @@ end
|
||||
defimpl Inspect, for: Range do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(first..last, opts) do
|
||||
def inspect(first .. last, opts) do
|
||||
concat [to_doc(first, opts), "..", to_doc(last, opts)]
|
||||
end
|
||||
end
|
||||
|
||||
+52
-130
@@ -1,6 +1,6 @@
|
||||
defmodule Record do
|
||||
@moduledoc """
|
||||
Module to work with, define, and import records.
|
||||
Module to work with, define and import records.
|
||||
|
||||
Records are simply tuples where the first element is an atom:
|
||||
|
||||
@@ -17,14 +17,15 @@ defmodule Record do
|
||||
1. to work with short, internal data
|
||||
2. to interface with Erlang records
|
||||
|
||||
The macros `defrecord/3` and `defrecordp/3` can be used to create records
|
||||
while `extract/2` and `extract_all/1` can be used to extract records from
|
||||
Erlang files.
|
||||
The macros `defrecord/3` and `defrecordp/3` can be used to create
|
||||
records while `extract/2` can be used to extract records from Erlang
|
||||
files.
|
||||
|
||||
## Types
|
||||
|
||||
Types can be defined for tuples with the `record/2` macro (only available in
|
||||
typespecs). This macro will expand to a tuple as seen in the example below:
|
||||
Types can be defined for tuples with the `record/2` macro (only available
|
||||
in typespecs). Like with the generated record macros it will expand to
|
||||
a tuple.
|
||||
|
||||
defmodule MyModule do
|
||||
require Record
|
||||
@@ -33,34 +34,14 @@ defmodule Record do
|
||||
@type user :: record(:user, name: String.t, age: integer)
|
||||
# expands to: "@type user :: {:user, String.t, integer}"
|
||||
end
|
||||
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Extracts record information from an Erlang file.
|
||||
|
||||
Returns a quoted expression containing the fields as a list
|
||||
of tuples.
|
||||
|
||||
`name`, which is the name of the extracted record, is expected to be an atom
|
||||
*at compile time*.
|
||||
|
||||
## Options
|
||||
|
||||
This function accepts the following options, which are exclusive to each other
|
||||
(i.e., only one of them can be used in the same call):
|
||||
|
||||
* `:from` - (binary representing a path to a file) path to the Erlang file
|
||||
that contains the record definition to extract; with this option, this
|
||||
function uses the same path lookup used by the `-include` attribute used in
|
||||
Erlang modules.
|
||||
* `:from_lib` - (binary representing a path to a file) path to the Erlang
|
||||
file that contains the record definition to extract; with this option,
|
||||
this function uses the same path lookup used by the `-include_lib`
|
||||
attribute used in Erlang modules.
|
||||
|
||||
These options are expected to be literals (including the binary values) at
|
||||
compile time.
|
||||
of tuples. It expects the record name to be an atom and the
|
||||
library path to be a string at expansion time.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -78,33 +59,18 @@ defmodule Record do
|
||||
@doc """
|
||||
Extracts all records information from an Erlang file.
|
||||
|
||||
Returns a keyword list of `{record_name, fields}` tuples where `record_name`
|
||||
is the name of an extracted record and `fields` is a list of `{field, value}`
|
||||
tuples representing the fields for that record.
|
||||
Returns a keyword list containing extracted record names as keys, and
|
||||
lists of tuples describing the fields as values. It expects a named
|
||||
argument :from or :from_lib, which correspond to *include* or
|
||||
*include_lib* attribute from Erlang modules, respectively.
|
||||
|
||||
## Options
|
||||
|
||||
This function accepts the following options, which are exclusive to each other
|
||||
(i.e., only one of them can be used in the same call):
|
||||
|
||||
* `:from` - (binary representing a path to a file) path to the Erlang file
|
||||
that contains the record definitions to extract; with this option, this
|
||||
function uses the same path lookup used by the `-include` attribute used in
|
||||
Erlang modules.
|
||||
* `:from_lib` - (binary representing a path to a file) path to the Erlang
|
||||
file that contains the record definitions to extract; with this option,
|
||||
this function uses the same path lookup used by the `-include_lib`
|
||||
attribute used in Erlang modules.
|
||||
|
||||
These options are expected to be literals (including the binary values) at
|
||||
compile time.
|
||||
"""
|
||||
def extract_all(opts) when is_list(opts) do
|
||||
Record.Extractor.extract_all(opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the given `data` is a record of kind `kind`.
|
||||
Checks if the given `data` is a record of `kind`.
|
||||
|
||||
This is implemented as a macro so it can be used in guard clauses.
|
||||
|
||||
@@ -119,14 +85,14 @@ defmodule Record do
|
||||
case Macro.Env.in_guard?(__CALLER__) do
|
||||
true ->
|
||||
quote do
|
||||
is_atom(unquote(kind)) and is_tuple(unquote(data)) and tuple_size(unquote(data)) > 0 and
|
||||
elem(unquote(data), 0) == unquote(kind)
|
||||
is_tuple(unquote(data)) and tuple_size(unquote(data)) > 0
|
||||
and :erlang.element(1, unquote(data)) == unquote(kind)
|
||||
end
|
||||
false ->
|
||||
quote do
|
||||
result = unquote(data)
|
||||
kind = unquote(kind)
|
||||
is_atom(kind) and is_tuple(result) and tuple_size(result) > 0 and elem(result, 0) == kind
|
||||
is_tuple(result) and tuple_size(result) > 0
|
||||
and :erlang.element(1, result) == unquote(kind)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -150,39 +116,24 @@ defmodule Record do
|
||||
case Macro.Env.in_guard?(__CALLER__) do
|
||||
true ->
|
||||
quote do
|
||||
is_tuple(unquote(data)) and tuple_size(unquote(data)) > 0 and
|
||||
is_atom(elem(unquote(data), 0))
|
||||
is_tuple(unquote(data)) and tuple_size(unquote(data)) > 0
|
||||
and is_atom(:erlang.element(1, unquote(data)))
|
||||
end
|
||||
false ->
|
||||
quote do
|
||||
result = unquote(data)
|
||||
is_tuple(result) and tuple_size(result) > 0 and is_atom(elem(result, 0))
|
||||
is_tuple(result) and tuple_size(result) > 0
|
||||
and is_atom(:erlang.element(1, result))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Defines a set of macros to create, access, and pattern match
|
||||
on a record.
|
||||
Defines a set of macros to create and access a record.
|
||||
|
||||
The name of the generated macros will be `name` (which has to be an
|
||||
atom). `tag` is also an atom and is used as the "tag" for the record (i.e.,
|
||||
the first element of the record tuple); by default (if `nil`), it's the same
|
||||
as `name`. `kv` is a keyword list of `name: default_value` fields for the
|
||||
new record.
|
||||
|
||||
The following macros are generated:
|
||||
|
||||
* `name/0` to create a new record with default values for all fields
|
||||
* `name/1` to create a new record with the given fields and values,
|
||||
to get the zero-based index of the given field in a record or to
|
||||
convert the given record to a keyword list
|
||||
* `name/2` to update an existing record with the given fields and values
|
||||
or to access a given field in a given record
|
||||
|
||||
All these macros are public macros (as defined by `defmacro`).
|
||||
|
||||
See the "Examples" section for examples on how to use these macros.
|
||||
The macros are going to have `name`, a tag (which defaults)
|
||||
to the name if none is given, and a set of fields given by
|
||||
`kv`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -192,10 +143,7 @@ defmodule Record do
|
||||
end
|
||||
|
||||
In the example above, a set of macros named `user` but with different
|
||||
arities will be defined to manipulate the underlying record.
|
||||
|
||||
# Import the module to make the user macros locally available
|
||||
import User
|
||||
arities will be defined to manipulate the underlying record:
|
||||
|
||||
# To create records
|
||||
record = user() #=> {:user, "meg", 25}
|
||||
@@ -207,10 +155,6 @@ defmodule Record do
|
||||
# To update the record
|
||||
user(record, age: 26) #=> {:user, "meg", 26}
|
||||
|
||||
# To get the zero-based index of the field in record tuple
|
||||
# (index 0 is occupied by the record "tag")
|
||||
user(:name) #=> 1
|
||||
|
||||
# Convert a record to a keyword list
|
||||
user(record) #=> [name: "meg", age: 26]
|
||||
|
||||
@@ -222,8 +166,8 @@ defmodule Record do
|
||||
user(name: name) = record
|
||||
name #=> "meg"
|
||||
|
||||
By default, Elixir uses the record name as the first element of the tuple (the
|
||||
"tag"). However, a different tag can be specified when defining a record:
|
||||
By default, Elixir uses the record name as the first element of
|
||||
the tuple (the tag). But it can be changed to something else:
|
||||
|
||||
defmodule User do
|
||||
require Record
|
||||
@@ -233,12 +177,12 @@ defmodule Record do
|
||||
require User
|
||||
User.user() #=> {User, nil}
|
||||
|
||||
## Defining extracted records with anonymous functions in the values
|
||||
## Defining extracted records with anonymous functions
|
||||
|
||||
If a record defines an anonymous function in the default values, an
|
||||
`ArgumentError` will be raised. This can happen unintentionally when defining
|
||||
a record after extracting it from an Erlang library that uses anonymous
|
||||
functions for defaults.
|
||||
If a record defines an anonymous function, an ArgumentError
|
||||
will occur if you attempt to create a record with it.
|
||||
This can occur unintentionally when defining a record after extracting
|
||||
it from an Erlang library that uses anonymous functions for defaults.
|
||||
|
||||
Record.defrecord :my_rec, Record.extract(...)
|
||||
#=> ** (ArgumentError) invalid value for record field fun_field,
|
||||
@@ -252,7 +196,6 @@ defmodule Record do
|
||||
Record.defrecord :my_rec, Record.extract(...) |> Keyword.merge(fun_field: &__MODULE__.foo/2)
|
||||
def foo(bar, baz), do: IO.inspect({bar, baz})
|
||||
end
|
||||
|
||||
"""
|
||||
defmacro defrecord(name, tag \\ nil, kv) do
|
||||
quote bind_quoted: [name: name, tag: tag, kv: kv] do
|
||||
@@ -317,11 +260,11 @@ defmodule Record do
|
||||
create(atom, fields, args, caller)
|
||||
true ->
|
||||
case Macro.expand(args, caller) do
|
||||
{:{}, _, [^atom | list]} when length(list) == length(fields) ->
|
||||
record = List.to_tuple([atom | list])
|
||||
Record.__keyword__(atom, fields, record)
|
||||
{:{}, _, [^atom|list]} when length(list) == length(fields) ->
|
||||
record = List.to_tuple([atom|list])
|
||||
Macro.escape(Record.__keyword__(atom, fields, record))
|
||||
{^atom, arg} when length(fields) == 1 ->
|
||||
Record.__keyword__(atom, fields, {atom, arg})
|
||||
Macro.escape(Record.__keyword__(atom, fields, {atom, arg}))
|
||||
_ ->
|
||||
quote do: Record.__keyword__(unquote(atom), unquote(fields), unquote(args))
|
||||
end
|
||||
@@ -354,15 +297,17 @@ defmodule Record do
|
||||
# Creates a new record with the given default fields and keyword values.
|
||||
defp create(atom, fields, keyword, caller) do
|
||||
in_match = Macro.Env.in_match?(caller)
|
||||
keyword = apply_underscore(fields, keyword)
|
||||
|
||||
{match, remaining} =
|
||||
Enum.map_reduce(fields, keyword, fn({field, default}, each_keyword) ->
|
||||
new_fields =
|
||||
case Keyword.fetch(each_keyword, field) do
|
||||
{:ok, value} -> value
|
||||
:error when in_match -> {:_, [], nil}
|
||||
:error -> Macro.escape(default)
|
||||
case Keyword.has_key?(each_keyword, field) do
|
||||
true -> Keyword.get(each_keyword, field)
|
||||
false ->
|
||||
case in_match do
|
||||
true -> {:_, [], nil}
|
||||
false -> Macro.escape(default)
|
||||
end
|
||||
end
|
||||
|
||||
{new_fields, Keyword.delete(each_keyword, field)}
|
||||
@@ -370,7 +315,7 @@ defmodule Record do
|
||||
|
||||
case remaining do
|
||||
[] ->
|
||||
{:{}, [], [atom | match]}
|
||||
{:{}, [], [atom|match]}
|
||||
_ ->
|
||||
keys = for {key, _} <- remaining, do: key
|
||||
raise ArgumentError, "record #{inspect atom} does not have the key: #{inspect hd(keys)}"
|
||||
@@ -383,8 +328,6 @@ defmodule Record do
|
||||
raise ArgumentError, "cannot invoke update style macro inside match"
|
||||
end
|
||||
|
||||
keyword = apply_underscore(fields, keyword)
|
||||
|
||||
Enum.reduce keyword, var, fn({key, value}, acc) ->
|
||||
index = find_index(fields, key, 0)
|
||||
if index do
|
||||
@@ -409,45 +352,24 @@ defmodule Record do
|
||||
end
|
||||
end
|
||||
|
||||
defp find_index([{k, _} | _], k, i), do: i + 2
|
||||
defp find_index([{_, _} | t], k, i), do: find_index(t, k, i + 1)
|
||||
defp find_index([{k, _}|_], k, i), do: i + 2
|
||||
defp find_index([{_, _}|t], k, i), do: find_index(t, k, i + 1)
|
||||
defp find_index([], _k, _i), do: nil
|
||||
|
||||
# Returns a keyword list of the record
|
||||
@doc false
|
||||
def __keyword__(atom, fields, record) do
|
||||
if is_record(record, atom) do
|
||||
[_tag | values] = Tuple.to_list(record)
|
||||
case join_keyword(fields, values, []) do
|
||||
kv when is_list(kv) ->
|
||||
kv
|
||||
expected_fields ->
|
||||
msg = "expected argument to be a #{inspect atom} record with #{expected_fields} fields, got: #{inspect record}"
|
||||
raise ArgumentError, msg
|
||||
end
|
||||
[_tag|values] = Tuple.to_list(record)
|
||||
join_keyword(fields, values, [])
|
||||
else
|
||||
msg = "expected argument to be a literal atom, literal keyword or a #{inspect atom} record, got runtime: #{inspect record}"
|
||||
raise ArgumentError, msg
|
||||
end
|
||||
end
|
||||
|
||||
# Returns a keyword list, or expected number of fields on size mismatch
|
||||
defp join_keyword([{field, _default} | fields], [value | values], acc),
|
||||
do: join_keyword(fields, values, [{field, value} | acc])
|
||||
defp join_keyword([{field, _default}|fields], [value|values], acc),
|
||||
do: join_keyword(fields, values, [{field, value}| acc])
|
||||
defp join_keyword([], [], acc),
|
||||
do: :lists.reverse(acc)
|
||||
defp join_keyword(rest_fields, _rest_values, acc),
|
||||
do: length(acc) + length(rest_fields) # expected fields
|
||||
|
||||
defp apply_underscore(fields, keyword) do
|
||||
case Keyword.fetch(keyword, :_) do
|
||||
{:ok, default} ->
|
||||
fields
|
||||
|> Enum.map(fn {k, _} -> {k, default} end)
|
||||
|> Keyword.merge(keyword)
|
||||
|> Keyword.delete(:_)
|
||||
:error ->
|
||||
keyword
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -27,7 +27,7 @@ defmodule Record.Extractor do
|
||||
|
||||
# Find file using the same lookup as the *include* attribute from Erlang modules.
|
||||
defp from_file(file) do
|
||||
file = String.to_charlist(file)
|
||||
file = String.to_char_list(file)
|
||||
case :code.where_is_file(file) do
|
||||
:non_existing -> file
|
||||
realfile -> realfile
|
||||
@@ -36,12 +36,12 @@ defmodule Record.Extractor do
|
||||
|
||||
# Find file using the same lookup as the *include_lib* attribute from Erlang modules.
|
||||
defp from_lib_file(file) do
|
||||
[app | path] = :filename.split(String.to_charlist(file))
|
||||
[app|path] = :filename.split(String.to_char_list(file))
|
||||
case :code.lib_dir(List.to_atom(app)) do
|
||||
{:error, _} ->
|
||||
raise ArgumentError, "lib file #{file} could not be found"
|
||||
libpath ->
|
||||
:filename.join([libpath | path])
|
||||
:filename.join([libpath|path])
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
+50
-83
@@ -13,7 +13,7 @@ defmodule Regex do
|
||||
# A simple regular expressions that matches foo anywhere in the string
|
||||
~r/foo/
|
||||
|
||||
# A regular expression with case insensitive and Unicode options
|
||||
# A regular expression with case insensitive and unicode options
|
||||
~r/foo/iu
|
||||
|
||||
A Regex is represented internally as the `Regex` struct. Therefore,
|
||||
@@ -23,11 +23,11 @@ defmodule Regex do
|
||||
|
||||
The modifiers available when creating a Regex are:
|
||||
|
||||
* `unicode` (u) - enables Unicode specific patterns like `\p` and change
|
||||
modifiers like `\w`, `\W`, `\s` and friends to also match on Unicode.
|
||||
It expects valid Unicode strings to be given on match
|
||||
* `unicode` (u) - enables unicode specific patterns like `\p` and change
|
||||
modifiers like `\w`, `\W`, `\s` and friends to also match on unicode.
|
||||
It expects valid unicode strings to be given on match
|
||||
|
||||
* `caseless` (i) - adds case insensitivity
|
||||
* `caseless` (i) - add case insensitivity
|
||||
|
||||
* `dotall` (s) - causes dot to match newlines and also set newline to
|
||||
anycrlf; the new line setting can be overridden by setting `(*CR)` or
|
||||
@@ -70,7 +70,7 @@ defmodule Regex do
|
||||
explicitly captured subpatterns, but not the complete matching part of
|
||||
the string
|
||||
|
||||
* `:none` - does not return matching subpatterns at all
|
||||
* `:none` - do not return matching subpatterns at all
|
||||
|
||||
* `:all_names` - captures all names in the Regex
|
||||
|
||||
@@ -135,7 +135,7 @@ defmodule Regex do
|
||||
Compiles the regular expression according to the given options.
|
||||
Fails with `Regex.CompileError` if the regex cannot be compiled.
|
||||
"""
|
||||
@spec compile!(binary, binary | [term]) :: t
|
||||
@spec compile(binary, binary | [term]) :: t
|
||||
def compile!(source, options \\ "") do
|
||||
case compile(source, options) do
|
||||
{:ok, regex} -> regex
|
||||
@@ -173,7 +173,8 @@ defmodule Regex do
|
||||
false
|
||||
|
||||
"""
|
||||
@spec regex?(any) :: boolean
|
||||
@spec regex?(t) :: true
|
||||
@spec regex?(any) :: false
|
||||
def regex?(term)
|
||||
def regex?(%Regex{}), do: true
|
||||
def regex?(_), do: false
|
||||
@@ -184,7 +185,7 @@ defmodule Regex do
|
||||
|
||||
## Options
|
||||
|
||||
* `:return` - sets to `:index` to return indexes. Defaults to `:binary`.
|
||||
* `:return` - set to `:index` to return indexes. Defaults to `:binary`.
|
||||
* `:capture` - what to capture in the result. Check the moduledoc for `Regex`
|
||||
to see the possible capture values.
|
||||
|
||||
@@ -290,7 +291,7 @@ defmodule Regex do
|
||||
names
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@doc """
|
||||
Same as `run/3`, but scans the target several times collecting all
|
||||
matches of the regular expression.
|
||||
|
||||
@@ -299,7 +300,7 @@ defmodule Regex do
|
||||
|
||||
## Options
|
||||
|
||||
* `:return` - sets to `:index` to return indexes. Defaults to `:binary`.
|
||||
* `:return` - set to `:index` to return indexes. Defaults to `:binary`.
|
||||
* `:capture` - what to capture in the result. Check the moduledoc for `Regex`
|
||||
to see the possible capture values.
|
||||
|
||||
@@ -314,9 +315,6 @@ defmodule Regex do
|
||||
iex> Regex.scan(~r/e/, "abcd")
|
||||
[]
|
||||
|
||||
iex> Regex.scan(~r/\p{Sc}/u, "$, £, and €")
|
||||
[["$"], ["£"], ["€"]]
|
||||
|
||||
"""
|
||||
@spec scan(t, String.t, [term]) :: [[String.t]]
|
||||
def scan(regex, string, options \\ [])
|
||||
@@ -350,35 +348,26 @@ defmodule Regex do
|
||||
order. Defaults to `:first` which means captures inside the regex do not
|
||||
affect the splitting process.
|
||||
|
||||
* `:include_captures` - when `true`, includes in the result the matches of
|
||||
the regular expression. Defaults to `false`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Regex.split(~r{-}, "a-b-c")
|
||||
iex> Regex.split(~r/-/, "a-b-c")
|
||||
["a", "b", "c"]
|
||||
|
||||
iex> Regex.split(~r{-}, "a-b-c", [parts: 2])
|
||||
iex> Regex.split(~r/-/, "a-b-c", [parts: 2])
|
||||
["a", "b-c"]
|
||||
|
||||
iex> Regex.split(~r{-}, "abc")
|
||||
iex> Regex.split(~r/-/, "abc")
|
||||
["abc"]
|
||||
|
||||
iex> Regex.split(~r{}, "abc")
|
||||
iex> Regex.split(~r//, "abc")
|
||||
["a", "b", "c", ""]
|
||||
|
||||
iex> Regex.split(~r{a(?<second>b)c}, "abc")
|
||||
iex> Regex.split(~r/a(?<second>b)c/, "abc")
|
||||
["", ""]
|
||||
|
||||
iex> Regex.split(~r{a(?<second>b)c}, "abc", on: [:second])
|
||||
iex> Regex.split(~r/a(?<second>b)c/, "abc", on: [:second])
|
||||
["a", "c"]
|
||||
|
||||
iex> Regex.split(~r{(x)}, "Elixir", include_captures: true)
|
||||
["Eli", "x", "ir"]
|
||||
|
||||
iex> Regex.split(~r{a(?<second>b)c}, "abc", on: [:second], include_captures: true)
|
||||
["a", "b", "c"]
|
||||
|
||||
"""
|
||||
@spec split(t, String.t, [term]) :: [String.t]
|
||||
def split(regex, string, options \\ [])
|
||||
@@ -397,8 +386,7 @@ defmodule Regex do
|
||||
{:match, matches} ->
|
||||
do_split(matches, string, 0,
|
||||
parts_to_index(Keyword.get(opts, :parts, :infinity)),
|
||||
Keyword.get(opts, :trim, false),
|
||||
Keyword.get(opts, :include_captures, false))
|
||||
Keyword.get(opts, :trim, false))
|
||||
:match ->
|
||||
[string]
|
||||
:nomatch ->
|
||||
@@ -409,47 +397,30 @@ defmodule Regex do
|
||||
defp parts_to_index(:infinity), do: 0
|
||||
defp parts_to_index(n) when is_integer(n) and n > 0, do: n
|
||||
|
||||
defp do_split(_, string, offset, _counter, true, _with_captures) when byte_size(string) <= offset,
|
||||
defp do_split(_, string, offset, _counter, true) when byte_size(string) <= offset,
|
||||
do: []
|
||||
|
||||
defp do_split(_, string, offset, 1, _trim, _with_captures),
|
||||
defp do_split(_, string, offset, 1, _trim),
|
||||
do: [binary_part(string, offset, byte_size(string) - offset)]
|
||||
|
||||
defp do_split([], string, offset, _counter, _trim, _with_captures),
|
||||
defp do_split([], string, offset, _counter, _trim),
|
||||
do: [binary_part(string, offset, byte_size(string) - offset)]
|
||||
|
||||
defp do_split([[{pos, _} | h] | t], string, offset, counter, trim, with_captures) when pos - offset < 0,
|
||||
do: do_split([h | t], string, offset, counter, trim, with_captures)
|
||||
defp do_split([[{pos, _}|h]|t], string, offset, counter, trim) when pos - offset < 0,
|
||||
do: do_split([h|t], string, offset, counter, trim)
|
||||
|
||||
defp do_split([[] | t], string, offset, counter, trim, with_captures),
|
||||
do: do_split(t, string, offset, counter, trim, with_captures)
|
||||
defp do_split([[]|t], string, offset, counter, trim),
|
||||
do: do_split(t, string, offset, counter, trim)
|
||||
|
||||
defp do_split([[{pos, length} | h] | t], string, offset, counter, trim, true) do
|
||||
new_offset = pos + length
|
||||
keep = pos - offset
|
||||
|
||||
if keep == 0 and length == 0 do
|
||||
do_split([h | t], string, new_offset, counter, trim, true)
|
||||
else
|
||||
<<_::binary-size(offset), part::binary-size(keep), match::binary-size(length), _::binary>> = string
|
||||
|
||||
if keep == 0 and (length == 0 or trim) do
|
||||
[match | do_split([h | t], string, new_offset, counter - 1, trim, true)]
|
||||
else
|
||||
[part, match | do_split([h | t], string, new_offset, counter - 1, trim, true)]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp do_split([[{pos, length} | h] | t], string, offset, counter, trim, false) do
|
||||
defp do_split([[{pos, length}|h]|t], string, offset, counter, trim) do
|
||||
new_offset = pos + length
|
||||
keep = pos - offset
|
||||
|
||||
if keep == 0 and (length == 0 or trim) do
|
||||
do_split([h | t], string, new_offset, counter, trim, false)
|
||||
do_split([h|t], string, new_offset, counter, trim)
|
||||
else
|
||||
<<_::binary-size(offset), part::binary-size(keep), _::binary>> = string
|
||||
[part | do_split([h | t], string, new_offset, counter - 1, trim, false)]
|
||||
[part|do_split([h|t], string, new_offset, counter - 1, trim)]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -459,10 +430,8 @@ defmodule Regex do
|
||||
|
||||
The replacement can be either a string or a function. The string
|
||||
is used as a replacement for every match and it allows specific
|
||||
captures to be accessed via `\N` or `\g{N}`, where `N` is the
|
||||
capture. In case `\0` is used, the whole match is inserted. Note
|
||||
that in regexes the backslash needs to be escaped, hence in practice
|
||||
you'll need to use `\\N` and `\\g{N}`.
|
||||
captures to be accessed via `\\N` or `\g{N}`, where `N` is the
|
||||
capture. In case `\\0` is used, the whole match is inserted.
|
||||
|
||||
When the replacement is a function, the function may have arity
|
||||
N where each argument maps to a capture, with the first argument
|
||||
@@ -514,13 +483,13 @@ defmodule Regex do
|
||||
|
||||
defp do_replace(%Regex{re_pattern: compiled}, string, replacement, options) do
|
||||
opts = if Keyword.get(options, :global) != false, do: [:global], else: []
|
||||
opts = [{:capture, :all, :index} | opts]
|
||||
opts = [{:capture, :all, :index}|opts]
|
||||
|
||||
case :re.run(string, compiled, opts) do
|
||||
:nomatch ->
|
||||
string
|
||||
{:match, [mlist | t]} when is_list(mlist) ->
|
||||
apply_list(string, replacement, [mlist | t]) |> IO.iodata_to_binary
|
||||
{:match, [mlist|t]} when is_list(mlist) ->
|
||||
apply_list(string, replacement, [mlist|t]) |> IO.iodata_to_binary
|
||||
{:match, slist} ->
|
||||
apply_list(string, replacement, [slist]) |> IO.iodata_to_binary
|
||||
end
|
||||
@@ -540,7 +509,7 @@ defmodule Regex do
|
||||
|
||||
defp precompile_replacement(<<?\\, x, rest::binary>>) when x in ?0..?9 do
|
||||
{ns, rest} = pick_int(rest)
|
||||
[List.to_integer([x | ns]) | precompile_replacement(rest)]
|
||||
[List.to_integer([x|ns]) | precompile_replacement(rest)]
|
||||
end
|
||||
|
||||
defp precompile_replacement(<<x, rest::binary>>) do
|
||||
@@ -554,7 +523,7 @@ defmodule Regex do
|
||||
|
||||
defp pick_int(<<x, rest::binary>>) when x in ?0..?9 do
|
||||
{found, rest} = pick_int(rest)
|
||||
{[x | found], rest}
|
||||
{[x|found], rest}
|
||||
end
|
||||
|
||||
defp pick_int(bin) do
|
||||
@@ -622,14 +591,14 @@ defmodule Regex do
|
||||
end
|
||||
|
||||
defp get_indexes(string, [], arity) do
|
||||
["" | get_indexes(string, [], arity - 1)]
|
||||
[""|get_indexes(string, [], arity - 1)]
|
||||
end
|
||||
|
||||
defp get_indexes(string, [h | t], arity) do
|
||||
[get_index(string, h) | get_indexes(string, t, arity - 1)]
|
||||
defp get_indexes(string, [h|t], arity) do
|
||||
[get_index(string, h)|get_indexes(string, t, arity - 1)]
|
||||
end
|
||||
|
||||
{:ok, pattern} = :re.compile(~S"[.^$*+?()\[\]{}\\\|\s#-]", [:unicode])
|
||||
{:ok, pattern} = :re.compile(~S"[.^$*+?()[{\\\|\s#]", [:unicode])
|
||||
@escape_pattern pattern
|
||||
|
||||
@doc ~S"""
|
||||
@@ -663,19 +632,17 @@ defmodule Regex do
|
||||
|
||||
# Private Helpers
|
||||
|
||||
defp translate_options(<<?u, t::binary>>, acc), do: translate_options(t, [:unicode, :ucp | acc])
|
||||
defp translate_options(<<?i, t::binary>>, acc), do: translate_options(t, [:caseless | acc])
|
||||
defp translate_options(<<?x, t::binary>>, acc), do: translate_options(t, [:extended | acc])
|
||||
defp translate_options(<<?f, t::binary>>, acc), do: translate_options(t, [:firstline | acc])
|
||||
defp translate_options(<<?U, t::binary>>, acc), do: translate_options(t, [:ungreedy | acc])
|
||||
defp translate_options(<<?s, t::binary>>, acc), do: translate_options(t, [:dotall, {:newline, :anycrlf} | acc])
|
||||
defp translate_options(<<?m, t::binary>>, acc), do: translate_options(t, [:multiline | acc])
|
||||
defp translate_options(<<?u, t::binary>>, acc), do: translate_options(t, [:unicode, :ucp|acc])
|
||||
defp translate_options(<<?i, t::binary>>, acc), do: translate_options(t, [:caseless|acc])
|
||||
defp translate_options(<<?x, t::binary>>, acc), do: translate_options(t, [:extended|acc])
|
||||
defp translate_options(<<?f, t::binary>>, acc), do: translate_options(t, [:firstline|acc])
|
||||
defp translate_options(<<?U, t::binary>>, acc), do: translate_options(t, [:ungreedy|acc])
|
||||
defp translate_options(<<?s, t::binary>>, acc), do: translate_options(t, [:dotall, {:newline, :anycrlf}|acc])
|
||||
defp translate_options(<<?m, t::binary>>, acc), do: translate_options(t, [:multiline|acc])
|
||||
|
||||
# TODO: Remove on 2.0
|
||||
defp translate_options(<<?r, t::binary>>, acc) do
|
||||
IO.warn "the /r modifier in regular expressions is deprecated, please use /U instead"
|
||||
translate_options(t, [:ungreedy | acc])
|
||||
end
|
||||
# TODO: Deprecate by 1.2
|
||||
# TODO: Remove by 2.0
|
||||
defp translate_options(<<?r, t::binary>>, acc), do: translate_options(t, [:ungreedy|acc])
|
||||
|
||||
defp translate_options(<<>>, acc), do: acc
|
||||
defp translate_options(rest, _acc), do: {:error, rest}
|
||||
|
||||
@@ -1,925 +0,0 @@
|
||||
defmodule Registry do
|
||||
@moduledoc ~S"""
|
||||
A local, decentralized and scalable key-value process storage.
|
||||
|
||||
It allows developers to lookup one or more processes with a given key.
|
||||
If the registry has `:unique` keys, a key points to 0 or 1 processes.
|
||||
If the registry allows `:duplicate` keys, a single key may point to any
|
||||
number of processes. In both cases, different keys could identify the
|
||||
same process.
|
||||
|
||||
Each entry in the registry is associated to the process that has
|
||||
registered the key. If the process crashes, the keys associated to that
|
||||
process are automatically removed. All key comparisons in the registry
|
||||
are done using the match operation (`===`).
|
||||
|
||||
The registry can be used for different purposes, such as name lookups (using
|
||||
the `:via` option), storing properties, custom dispatching rules, or a pubsub
|
||||
implementation. We explore some of those use cases below.
|
||||
|
||||
The registry may also be transparently partitioned, which provides
|
||||
more scalable behaviour for running registries on highly concurrent
|
||||
environments with thousands or millions of entries.
|
||||
|
||||
## Using in `:via`
|
||||
|
||||
Once the registry is started with a given name (using
|
||||
`Registry.start_link/2`), it can be used to register and access named
|
||||
processes using the `{:via, Registry, {registry, key}}` tuple:
|
||||
|
||||
{:ok, _} = Registry.start_link(:unique, Registry.ViaTest)
|
||||
name = {:via, Registry, {Registry.ViaTest, "agent"}}
|
||||
{:ok, _} = Agent.start_link(fn -> 0 end, name: name)
|
||||
Agent.get(name, & &1)
|
||||
#=> 0
|
||||
Agent.update(name, & &1 + 1)
|
||||
Agent.get(name, & &1)
|
||||
#=> 1
|
||||
|
||||
Typically the registry is started as part of a supervision tree though:
|
||||
|
||||
supervisor(Registry, [:unique, Registry.ViaTest])
|
||||
|
||||
Only registries with unique keys can be used in `:via`. If the name is
|
||||
already taken, the case-specific `start_link` function (`Agent.start_link/2`
|
||||
in the example above) will return `{:error, {:already_started, current_pid}}`.
|
||||
|
||||
## Using as a dispatcher
|
||||
|
||||
`Registry` has a dispatch mechanism that allows developers to implement custom
|
||||
dispatch logic triggered from the caller. For example, let's say we have a
|
||||
duplicate registry started as so:
|
||||
|
||||
{:ok, _} = Registry.start_link(:duplicate, Registry.DispatcherTest)
|
||||
|
||||
By calling `register/3`, different processes can register under a given key
|
||||
and associate any value under that key. In this case, let's register the
|
||||
current process under the key `"hello"` and attach the `{IO, :inspect}` tuple
|
||||
to it:
|
||||
|
||||
{:ok, _} = Registry.register(Registry.DispatcherTest, "hello", {IO, :inspect})
|
||||
|
||||
Now, an entity interested in dispatching events for a given key may call
|
||||
`dispatch/3` passing in the key and a callback. This callback will be invoked
|
||||
with a list of all the values registered under the requested key, alongside
|
||||
the pid of the process that registered each value, in the form of `{pid,
|
||||
value}` tuples. In our example, `value` will be the `{module, function}` tuple
|
||||
in the code above:
|
||||
|
||||
Registry.dispatch(Registry.DispatcherTest, "hello", fn entries ->
|
||||
for {pid, {module, function}} <- entries, do: apply(module, function, [pid])
|
||||
end)
|
||||
# Prints #PID<...> where the pid is for the process that called register/3 above
|
||||
#=> :ok
|
||||
|
||||
Dispatching happens in the process that calls `dispatch/3` either serially or
|
||||
concurrently in case of multiple partitions (via spawned tasks). The
|
||||
registered processes are not involved in dispatching unless involving them is
|
||||
done explicitly (for example, by sending them a message in the callback).
|
||||
|
||||
Furthermore, if there is a failure when dispatching, due to a bad
|
||||
registration, dispatching will always fail and the registered process will not
|
||||
be notified. Therefore let's make sure we at least wrap and report those
|
||||
errors:
|
||||
|
||||
require Logger
|
||||
Registry.dispatch(Registry.DispatcherTest, "hello", fn entries ->
|
||||
for {pid, {module, function}} <- entries do
|
||||
try do
|
||||
apply(module, function, [pid])
|
||||
catch
|
||||
kind, reason ->
|
||||
formatted = Exception.format(kind, reason, System.stacktrace)
|
||||
Logger.error "Registry.dispatch/3 failed with #{formatted}"
|
||||
end
|
||||
end
|
||||
end)
|
||||
# Prints #PID<...>
|
||||
#=> :ok
|
||||
|
||||
You could also replace the whole `apply` system by explicitly sending
|
||||
messages. That's the example we will see next.
|
||||
|
||||
## Using as a PubSub
|
||||
|
||||
Registries can also be used to implement a local, non-distributed, scalable
|
||||
PubSub by relying on the `dispatch/3` function, similarly to the previous
|
||||
section: in this case, however, we will send messages to each associated
|
||||
process, instead of invoking a given module-function.
|
||||
|
||||
In this example, we will also set the number of partitions to the number of
|
||||
schedulers online, which will make the registry more performant on highly
|
||||
concurrent environments as each partition will spawn a new process, allowing
|
||||
dispatching to happen in parallel:
|
||||
|
||||
{:ok, _} = Registry.start_link(:duplicate, Registry.PubSubTest,
|
||||
partitions: System.schedulers_online)
|
||||
{:ok, _} = Registry.register(Registry.PubSubTest, "hello", [])
|
||||
Registry.dispatch(Registry.PubSubTest, "hello", fn entries ->
|
||||
for {pid, _} <- entries, do: send(pid, {:broadcast, "world"})
|
||||
end)
|
||||
#=> :ok
|
||||
|
||||
The example above broadcasted the message `{:broadcast, "world"}` to all
|
||||
processes registered under the "topic" (or "key" as we called it until now)
|
||||
`"hello"`.
|
||||
|
||||
The third argument given to `register/3` is a value associated to the
|
||||
current process. While in the previous section we used it when dispatching,
|
||||
in this particular example we are not interested in it, so we have set it
|
||||
to an empty list. You could store a more meaningful value if necessary.
|
||||
|
||||
## Registrations
|
||||
|
||||
Looking up, dispatching and registering are efficient and immediate at
|
||||
the cost of delayed unsubscription. For example, if a process crashes,
|
||||
its keys are automatically removed from the registry but the change may
|
||||
not propagate immediately. This means certain operations may return processes
|
||||
that are already dead. When such may happen, it will be explicitly stated
|
||||
in the function documentation.
|
||||
|
||||
However, keep in mind those cases are typically not an issue. After all, a
|
||||
process referenced by a pid may crash at any time, including between getting
|
||||
the value from the registry and sending it a message. Many parts of the standard
|
||||
library are designed to cope with that, such as `Process.monitor/1` which will
|
||||
deliver the `:DOWN` message immediately if the monitored process is already dead
|
||||
and `Kernel.send/2` which acts as a no-op for dead processes.
|
||||
|
||||
## ETS
|
||||
|
||||
Note that the registry uses one ETS table plus two ETS tables per partition.
|
||||
"""
|
||||
|
||||
# TODO: Decide if it should be started as part of Elixir's supervision tree.
|
||||
|
||||
@kind [:unique, :duplicate]
|
||||
@all_info -1
|
||||
@key_info -2
|
||||
|
||||
@typedoc "The registry identifier"
|
||||
@type registry :: atom
|
||||
|
||||
@typedoc "The type of the registry"
|
||||
@type kind :: :unique | :duplicate
|
||||
|
||||
@typedoc "The type of keys allowed on registration"
|
||||
@type key :: term
|
||||
|
||||
@typedoc "The type of values allowed on registration"
|
||||
@type value :: term
|
||||
|
||||
@typedoc "The type of registry metadata keys"
|
||||
@type meta_key :: atom | tuple
|
||||
|
||||
@typedoc "The type of registry metadata values"
|
||||
@type meta_value :: term
|
||||
|
||||
## Via callbacks
|
||||
|
||||
@doc false
|
||||
def whereis_name({registry, key}) do
|
||||
case key_info!(registry) do
|
||||
{:unique, partitions, key_ets} ->
|
||||
key_ets = key_ets || key_ets!(registry, key, partitions)
|
||||
case safe_lookup_second(key_ets, key) do
|
||||
{pid, _} ->
|
||||
if Process.alive?(pid), do: pid, else: :undefined
|
||||
_ ->
|
||||
:undefined
|
||||
end
|
||||
{kind, _, _} ->
|
||||
raise ArgumentError, ":via is not supported for #{kind} registries"
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def register_name({registry, key}, pid) when pid == self() do
|
||||
case register(registry, key, nil) do
|
||||
{:ok, _} -> :yes
|
||||
{:error, _} -> :no
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def send({registry, key}, msg) do
|
||||
case lookup(registry, key) do
|
||||
[{pid, _}] -> Kernel.send(pid, msg)
|
||||
[] -> :erlang.error(:badarg, [{registry, key}, msg])
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def unregister_name({registry, key}) do
|
||||
unregister(registry, key)
|
||||
end
|
||||
|
||||
## Registry API
|
||||
|
||||
@doc """
|
||||
Starts the registry as a supervisor process.
|
||||
|
||||
Manually it can be started as:
|
||||
|
||||
Registry.start_link(:unique, MyApp.Registry)
|
||||
|
||||
In your supervisor tree, you would write:
|
||||
|
||||
supervisor(Registry, [:unique, MyApp.Registry])
|
||||
|
||||
For intensive workloads, the registry may also be partitioned (by specifying
|
||||
the `:partitions` option). If partitioning is required then a good default is to
|
||||
set the number of partitions to the number of schedulers available:
|
||||
|
||||
Registry.start_link(:unique, MyApp.Registry, partitions: System.schedulers_online())
|
||||
|
||||
or:
|
||||
|
||||
supervisor(Registry, [:unique, MyApp.Registry, [partitions: System.schedulers_online()]])
|
||||
|
||||
## Options
|
||||
|
||||
The registry supports the following options:
|
||||
|
||||
* `:partitions` - the number of partitions in the registry. Defaults to `1`.
|
||||
* `:listeners` - a list of named processes which are notified of `:register`
|
||||
and `:unregister` events. The registered process must be monitored by the
|
||||
listener if the listener wants to be notified if the registered process
|
||||
crashes.
|
||||
* `:meta` - a keyword list of metadata to be attached to the registry.
|
||||
|
||||
"""
|
||||
@spec start_link(kind, registry, options) :: {:ok, pid} | {:error, term}
|
||||
when options: [partitions: pos_integer, listeners: [atom], meta: [{meta_key, meta_value}]]
|
||||
def start_link(kind, registry, options \\ []) when kind in @kind and is_atom(registry) do
|
||||
meta = Keyword.get(options, :meta, [])
|
||||
unless Keyword.keyword?(meta) do
|
||||
raise ArgumentError, "expected :meta to be a keyword list, got: #{inspect meta}"
|
||||
end
|
||||
|
||||
partitions = Keyword.get(options, :partitions, 1)
|
||||
unless is_integer(partitions) and partitions >= 1 do
|
||||
raise ArgumentError, "expected :partitions to be a positive integer, got: #{inspect partitions}"
|
||||
end
|
||||
|
||||
listeners = Keyword.get(options, :listeners, [])
|
||||
unless is_list(listeners) and Enum.all?(listeners, &is_atom/1) do
|
||||
raise ArgumentError, "expected :listeners to be a list of named processes, got: #{inspect listeners}"
|
||||
end
|
||||
|
||||
# The @info format must be kept in sync with Registry.Partition optimization.
|
||||
entries = [{@all_info, {kind, partitions, nil, nil, listeners}},
|
||||
{@key_info, {kind, partitions, nil}} | meta]
|
||||
Registry.Supervisor.start_link(kind, registry, partitions, listeners, entries)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates the value for `key` for the current process in the unique `registry`.
|
||||
|
||||
Returns a `{new_value, old_value}` tuple or `:error` if there
|
||||
is no such key assigned to the current process.
|
||||
|
||||
If a non-unique registry is given, an error is raised.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Registry.start_link(:unique, Registry.UpdateTest)
|
||||
iex> {:ok, _} = Registry.register(Registry.UpdateTest, "hello", 1)
|
||||
iex> Registry.lookup(Registry.UpdateTest, "hello")
|
||||
[{self(), 1}]
|
||||
iex> Registry.update_value(Registry.UpdateTest, "hello", & &1 + 1)
|
||||
{2, 1}
|
||||
iex> Registry.lookup(Registry.UpdateTest, "hello")
|
||||
[{self(), 2}]
|
||||
|
||||
"""
|
||||
@spec update_value(registry, key, (value -> value)) :: {new_value :: term, old_value :: term} | :error
|
||||
def update_value(registry, key, callback) when is_atom(registry) and is_function(callback, 1) do
|
||||
case key_info!(registry) do
|
||||
{:unique, partitions, key_ets} ->
|
||||
key_ets = key_ets || key_ets!(registry, key, partitions)
|
||||
try do
|
||||
:ets.lookup_element(key_ets, key, 2)
|
||||
catch
|
||||
:error, :badarg -> :error
|
||||
else
|
||||
{pid, old_value} when pid == self() ->
|
||||
new_value = callback.(old_value)
|
||||
:ets.insert(key_ets, {key, {pid, new_value}})
|
||||
{new_value, old_value}
|
||||
{_, _} ->
|
||||
:error
|
||||
end
|
||||
{kind, _, _} ->
|
||||
raise ArgumentError, "Registry.update_value/3 is not supported for #{kind} registries"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Invokes the callback with all entries under `key` in each partition
|
||||
for the given `registry`.
|
||||
|
||||
The list of `entries` is a non-empty list of two-element tuples where
|
||||
the first element is the pid and the second element is the value
|
||||
associated to the pid. If there are no entries for the given key,
|
||||
the callback is never invoked.
|
||||
|
||||
If the registry is not partitioned, the callback is invoked in the process
|
||||
that calls `dispatch/3`. If the registry is partitioned, the callback is
|
||||
invoked concurrently per partition by starting a task linked to the
|
||||
caller. The callback, however, is only invoked if there are entries for that
|
||||
partition.
|
||||
|
||||
See the module documentation for examples of using the `dispatch/3`
|
||||
function for building custom dispatching or a pubsub system.
|
||||
"""
|
||||
@spec dispatch(registry, key, (entries :: [{pid, value}] -> term)) :: :ok
|
||||
def dispatch(registry, key, mfa_or_fun)
|
||||
when is_atom(registry) and is_function(mfa_or_fun, 1)
|
||||
when is_atom(registry) and tuple_size(mfa_or_fun) == 3 do
|
||||
case key_info!(registry) do
|
||||
{:unique, partitions, key_ets} ->
|
||||
(key_ets || key_ets!(registry, key, partitions))
|
||||
|> safe_lookup_second(key)
|
||||
|> List.wrap()
|
||||
|> apply_non_empty_to_mfa_or_fun(mfa_or_fun)
|
||||
{:duplicate, 1, key_ets} ->
|
||||
key_ets
|
||||
|> safe_lookup_second(key)
|
||||
|> apply_non_empty_to_mfa_or_fun(mfa_or_fun)
|
||||
{:duplicate, partitions, _} ->
|
||||
registry
|
||||
|> dispatch_task(key, mfa_or_fun, partitions)
|
||||
|> Enum.each(&Task.await(&1, :infinity))
|
||||
end
|
||||
:ok
|
||||
end
|
||||
|
||||
defp dispatch_task(_registry, _key, _mfa_or_fun, 0) do
|
||||
[]
|
||||
end
|
||||
defp dispatch_task(registry, key, mfa_or_fun, partition) do
|
||||
partition = partition - 1
|
||||
task = Task.async(fn ->
|
||||
registry
|
||||
|> key_ets!(partition)
|
||||
|> safe_lookup_second(key)
|
||||
|> apply_non_empty_to_mfa_or_fun(mfa_or_fun)
|
||||
:ok
|
||||
end)
|
||||
[task | dispatch_task(registry, key, mfa_or_fun, partition)]
|
||||
end
|
||||
|
||||
defp apply_non_empty_to_mfa_or_fun([], _mfa_or_fun) do
|
||||
:ok
|
||||
end
|
||||
defp apply_non_empty_to_mfa_or_fun(entries, {module, function, args}) do
|
||||
apply(module, function, [entries | args])
|
||||
end
|
||||
defp apply_non_empty_to_mfa_or_fun(entries, fun) do
|
||||
fun.(entries)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Finds the `{pid, value}` pair for the given `key` in `registry` in no particular order.
|
||||
|
||||
An empty list if there is no match.
|
||||
|
||||
For unique registries, a single partition lookup is necessary. For
|
||||
duplicate registries, all partitions must be looked up.
|
||||
|
||||
## Examples
|
||||
|
||||
In the example below we register the current process and look it up
|
||||
both from itself and other processes:
|
||||
|
||||
iex> Registry.start_link(:unique, Registry.UniqueLookupTest)
|
||||
iex> Registry.lookup(Registry.UniqueLookupTest, "hello")
|
||||
[]
|
||||
iex> {:ok, _} = Registry.register(Registry.UniqueLookupTest, "hello", :world)
|
||||
iex> Registry.lookup(Registry.UniqueLookupTest, "hello")
|
||||
[{self(), :world}]
|
||||
iex> Task.async(fn -> Registry.lookup(Registry.UniqueLookupTest, "hello") end) |> Task.await
|
||||
[{self(), :world}]
|
||||
|
||||
The same applies to duplicate registries:
|
||||
|
||||
iex> Registry.start_link(:duplicate, Registry.DuplicateLookupTest)
|
||||
iex> Registry.lookup(Registry.DuplicateLookupTest, "hello")
|
||||
[]
|
||||
iex> {:ok, _} = Registry.register(Registry.DuplicateLookupTest, "hello", :world)
|
||||
iex> Registry.lookup(Registry.DuplicateLookupTest, "hello")
|
||||
[{self(), :world}]
|
||||
iex> {:ok, _} = Registry.register(Registry.DuplicateLookupTest, "hello", :another)
|
||||
iex> Enum.sort(Registry.lookup(Registry.DuplicateLookupTest, "hello"))
|
||||
[{self(), :another}, {self(), :world}]
|
||||
|
||||
"""
|
||||
@spec lookup(registry, key) :: [{pid, value}]
|
||||
def lookup(registry, key) when is_atom(registry) do
|
||||
case key_info!(registry) do
|
||||
{:unique, partitions, key_ets} ->
|
||||
key_ets = key_ets || key_ets!(registry, key, partitions)
|
||||
case safe_lookup_second(key_ets, key) do
|
||||
{_, _} = pair ->
|
||||
[pair]
|
||||
_ ->
|
||||
[]
|
||||
end
|
||||
|
||||
{:duplicate, 1, key_ets} ->
|
||||
safe_lookup_second(key_ets, key)
|
||||
|
||||
{:duplicate, partitions, _key_ets} ->
|
||||
for partition <- 0..(partitions - 1),
|
||||
pair <- safe_lookup_second(key_ets!(registry, partition), key),
|
||||
do: pair
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `{pid, value}` pairs under the given `key` in `registry` that match `pattern`.
|
||||
|
||||
Pattern must be an atom or a tuple that will match the structure of the
|
||||
value stored in the registry. The atom `:_` can be used to ignore a given
|
||||
value or tuple element, while :"$1" can be used to temporarily assign part
|
||||
of pattern to a variable for a subsequent comparison.
|
||||
|
||||
An empty list will be returned if there is no match.
|
||||
|
||||
For unique registries, a single partition lookup is necessary. For
|
||||
duplicate registries, all partitions must be looked up.
|
||||
|
||||
## Examples
|
||||
|
||||
In the example below we register the current process under the same
|
||||
key in a duplicate registry but with different values:
|
||||
|
||||
iex> Registry.start_link(:duplicate, Registry.MatchTest)
|
||||
iex> {:ok, _} = Registry.register(Registry.MatchTest, "hello", {1, :atom, 1})
|
||||
iex> {:ok, _} = Registry.register(Registry.MatchTest, "hello", {2, :atom, 2})
|
||||
iex> Registry.match(Registry.MatchTest, "hello", {1, :_, :_})
|
||||
[{self(), {1, :atom, 1}}]
|
||||
iex> Registry.match(Registry.MatchTest, "hello", {2, :_, :_})
|
||||
[{self(), {2, :atom, 2}}]
|
||||
iex> Registry.match(Registry.MatchTest, "hello", {:_, :atom, :_}) |> Enum.sort()
|
||||
[{self(), {1, :atom, 1}}, {self(), {2, :atom, 2}}]
|
||||
iex> Registry.match(Registry.MatchTest, "hello", {:"$1", :_, :"$1"}) |> Enum.sort()
|
||||
[{self(), {1, :atom, 1}}, {self(), {2, :atom, 2}}]
|
||||
|
||||
"""
|
||||
@spec match(registry, key, match_pattern :: atom() | tuple()) :: [{pid, term}]
|
||||
def match(registry, key, pattern) when is_atom(registry) do
|
||||
spec = [{{key, {:_, pattern}}, [], [{:element, 2, :"$_"}]}]
|
||||
|
||||
case key_info!(registry) do
|
||||
{:unique, partitions, key_ets} ->
|
||||
key_ets = key_ets || key_ets!(registry, key, partitions)
|
||||
:ets.select(key_ets, spec)
|
||||
|
||||
{:duplicate, 1, key_ets} ->
|
||||
:ets.select(key_ets, spec)
|
||||
|
||||
{:duplicate, partitions, _key_ets} ->
|
||||
for partition <- 0..(partitions - 1),
|
||||
pair <- :ets.select(key_ets!(registry, partition), spec),
|
||||
do: pair
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the known keys for the given `pid` in `registry` in no particular order.
|
||||
|
||||
If the registry is unique, the keys are unique. Otherwise
|
||||
they may contain duplicates if the process was registered
|
||||
under the same key multiple times. The list will be empty
|
||||
if the process is dead or it has no keys in this registry.
|
||||
|
||||
## Examples
|
||||
|
||||
Registering under a unique registry does not allow multiple entries:
|
||||
|
||||
iex> Registry.start_link(:unique, Registry.UniqueKeysTest)
|
||||
iex> Registry.keys(Registry.UniqueKeysTest, self())
|
||||
[]
|
||||
iex> {:ok, _} = Registry.register(Registry.UniqueKeysTest, "hello", :world)
|
||||
iex> Registry.register(Registry.UniqueKeysTest, "hello", :later) # registry is :unique
|
||||
{:error, {:already_registered, self()}}
|
||||
iex> Registry.keys(Registry.UniqueKeysTest, self())
|
||||
["hello"]
|
||||
|
||||
Such is possible for duplicate registries though:
|
||||
|
||||
iex> Registry.start_link(:duplicate, Registry.DuplicateKeysTest)
|
||||
iex> Registry.keys(Registry.DuplicateKeysTest, self())
|
||||
[]
|
||||
iex> {:ok, _} = Registry.register(Registry.DuplicateKeysTest, "hello", :world)
|
||||
iex> {:ok, _} = Registry.register(Registry.DuplicateKeysTest, "hello", :world)
|
||||
iex> Registry.keys(Registry.DuplicateKeysTest, self())
|
||||
["hello", "hello"]
|
||||
|
||||
"""
|
||||
@spec keys(registry, pid) :: [key]
|
||||
def keys(registry, pid) when is_atom(registry) and is_pid(pid) do
|
||||
{kind, partitions, _, pid_ets, _} = info!(registry)
|
||||
{_, pid_ets} = pid_ets || pid_ets!(registry, pid, partitions)
|
||||
keys = safe_lookup_second(pid_ets, pid)
|
||||
|
||||
cond do
|
||||
kind == :unique -> Enum.uniq(keys)
|
||||
true -> keys
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Unregisters all entries for the given `key` associated to the current
|
||||
process in `registry`.
|
||||
|
||||
Always returns `:ok` and automatically unlinks the current process from
|
||||
the owner if there are no more keys associated to the current process. See
|
||||
also `register/3` to read more about the "owner".
|
||||
|
||||
## Examples
|
||||
|
||||
It unregister all entries for `key` for unique registries:
|
||||
|
||||
iex> Registry.start_link(:unique, Registry.UniqueUnregisterTest)
|
||||
iex> Registry.register(Registry.UniqueUnregisterTest, "hello", :world)
|
||||
iex> Registry.keys(Registry.UniqueUnregisterTest, self())
|
||||
["hello"]
|
||||
iex> Registry.unregister(Registry.UniqueUnregisterTest, "hello")
|
||||
:ok
|
||||
iex> Registry.keys(Registry.UniqueUnregisterTest, self())
|
||||
[]
|
||||
|
||||
As well as duplicate registries:
|
||||
|
||||
iex> Registry.start_link(:duplicate, Registry.DuplicateUnregisterTest)
|
||||
iex> Registry.register(Registry.DuplicateUnregisterTest, "hello", :world)
|
||||
iex> Registry.register(Registry.DuplicateUnregisterTest, "hello", :world)
|
||||
iex> Registry.keys(Registry.DuplicateUnregisterTest, self())
|
||||
["hello", "hello"]
|
||||
iex> Registry.unregister(Registry.DuplicateUnregisterTest, "hello")
|
||||
:ok
|
||||
iex> Registry.keys(Registry.DuplicateUnregisterTest, self())
|
||||
[]
|
||||
|
||||
"""
|
||||
@spec unregister(registry, key) :: :ok
|
||||
def unregister(registry, key) when is_atom(registry) do
|
||||
self = self()
|
||||
{kind, partitions, key_ets, pid_ets, listeners} = info!(registry)
|
||||
{key_partition, pid_partition} = partitions(kind, key, self, partitions)
|
||||
key_ets = key_ets || key_ets!(registry, key_partition)
|
||||
{pid_server, pid_ets} = pid_ets || pid_ets!(registry, pid_partition)
|
||||
|
||||
# Remove first from the key_ets because in case of crashes
|
||||
# the pid_ets will still be able to clean up. The last step is
|
||||
# to clean if we have no more entries.
|
||||
true = :ets.match_delete(key_ets, {key, {self, :_}})
|
||||
true = :ets.delete_object(pid_ets, {self, key, key_ets})
|
||||
|
||||
unlink_if_unregistered(pid_server, pid_ets, self)
|
||||
|
||||
for listener <- listeners do
|
||||
Kernel.send(listener, {:unregister, registry, key, self})
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
Registers the current process under the given `key` in `registry`.
|
||||
|
||||
A value to be associated with this registration must also be given.
|
||||
This value will be retrieved whenever dispatching or doing a key
|
||||
lookup.
|
||||
|
||||
This function returns `{:ok, owner}` or `{:error, reason}`.
|
||||
The `owner` is the pid is the registry partition responsible for
|
||||
the pid. The owner is automatically linked to the caller.
|
||||
|
||||
If the registry has unique keys, it will return `{:ok, owner}` unless
|
||||
the key is already associated to a pid, in which case it returns
|
||||
`{:error, {:already_registered, pid}}`.
|
||||
|
||||
If the registry has duplicate keys, multiple registrations from the
|
||||
current process under the same key are allowed.
|
||||
|
||||
## Examples
|
||||
|
||||
Registering under a unique registry does not allow multiple entries:
|
||||
|
||||
iex> Registry.start_link(:unique, Registry.UniqueRegisterTest)
|
||||
iex> {:ok, _} = Registry.register(Registry.UniqueRegisterTest, "hello", :world)
|
||||
iex> Registry.register(Registry.UniqueRegisterTest, "hello", :later)
|
||||
{:error, {:already_registered, self()}}
|
||||
iex> Registry.keys(Registry.UniqueRegisterTest, self())
|
||||
["hello"]
|
||||
|
||||
Such is possible for duplicate registries though:
|
||||
|
||||
iex> Registry.start_link(:duplicate, Registry.DuplicateRegisterTest)
|
||||
iex> {:ok, _} = Registry.register(Registry.DuplicateRegisterTest, "hello", :world)
|
||||
iex> {:ok, _} = Registry.register(Registry.DuplicateRegisterTest, "hello", :world)
|
||||
iex> Registry.keys(Registry.DuplicateRegisterTest, self())
|
||||
["hello", "hello"]
|
||||
|
||||
"""
|
||||
@spec register(registry, key, value) :: {:ok, pid} | {:error, {:already_registered, pid}}
|
||||
def register(registry, key, value) when is_atom(registry) do
|
||||
self = self()
|
||||
{kind, partitions, key_ets, pid_ets, listeners} = info!(registry)
|
||||
{key_partition, pid_partition} = partitions(kind, key, self, partitions)
|
||||
key_ets = key_ets || key_ets!(registry, key_partition)
|
||||
{pid_server, pid_ets} = pid_ets || pid_ets!(registry, pid_partition)
|
||||
|
||||
# Notice we write first to the pid ets table because it will
|
||||
# always be able to do the clean up. If we register first to the
|
||||
# key one and the process crashes, the key will stay there forever.
|
||||
Process.link(pid_server)
|
||||
true = :ets.insert(pid_ets, {self, key, key_ets})
|
||||
case register_key(kind, pid_server, key_ets, key, {key, {self, value}}) do
|
||||
{:ok, _} = ok ->
|
||||
for listener <- listeners do
|
||||
Kernel.send(listener, {:register, registry, key, self, value})
|
||||
end
|
||||
ok
|
||||
{:error, {:already_registered, ^self}} = error ->
|
||||
error
|
||||
{:error, _} = error ->
|
||||
true = :ets.delete_object(pid_ets, {self, key, key_ets})
|
||||
unlink_if_unregistered(pid_server, pid_ets, self)
|
||||
error
|
||||
end
|
||||
end
|
||||
|
||||
defp register_key(:duplicate, pid_server, key_ets, _key, entry) do
|
||||
true = :ets.insert(key_ets, entry)
|
||||
{:ok, pid_server}
|
||||
end
|
||||
defp register_key(:unique, pid_server, key_ets, key, entry) do
|
||||
if :ets.insert_new(key_ets, entry) do
|
||||
{:ok, pid_server}
|
||||
else
|
||||
# Notice we have to call register_key recursively
|
||||
# because we are always at odds of a race condition.
|
||||
case :ets.lookup(key_ets, key) do
|
||||
[{^key, {pid, _}} = current] ->
|
||||
if Process.alive?(pid) do
|
||||
{:error, {:already_registered, pid}}
|
||||
else
|
||||
:ets.delete_object(key_ets, current)
|
||||
register_key(:unique, pid_server, key_ets, key, entry)
|
||||
end
|
||||
[] ->
|
||||
register_key(:unique, pid_server, key_ets, key, entry)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads registry metadata given on `start_link/3`.
|
||||
|
||||
Atoms and tuples are allowed as keys.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Registry.start_link(:unique, Registry.MetaTest, meta: [custom_key: "custom_value"])
|
||||
iex> Registry.meta(Registry.MetaTest, :custom_key)
|
||||
{:ok, "custom_value"}
|
||||
iex> Registry.meta(Registry.MetaTest, :unknown_key)
|
||||
:error
|
||||
|
||||
"""
|
||||
@spec meta(registry, meta_key) :: {:ok, meta_value} | :error
|
||||
def meta(registry, key) when is_atom(registry) and (is_atom(key) or is_tuple(key)) do
|
||||
try do
|
||||
:ets.lookup(registry, key)
|
||||
catch
|
||||
:error, :badarg ->
|
||||
raise ArgumentError, "unknown registry: #{inspect registry}"
|
||||
else
|
||||
[{^key, value}] -> {:ok, value}
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stores registry metadata.
|
||||
|
||||
Atoms and tuples are allowed as keys.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Registry.start_link(:unique, Registry.PutMetaTest)
|
||||
iex> Registry.put_meta(Registry.PutMetaTest, :custom_key, "custom_value")
|
||||
:ok
|
||||
iex> Registry.meta(Registry.PutMetaTest, :custom_key)
|
||||
{:ok, "custom_value"}
|
||||
iex> Registry.put_meta(Registry.PutMetaTest, {:tuple, :key}, "tuple_value")
|
||||
:ok
|
||||
iex> Registry.meta(Registry.PutMetaTest, {:tuple, :key})
|
||||
{:ok, "tuple_value"}
|
||||
|
||||
"""
|
||||
@spec put_meta(registry, meta_key, meta_value) :: :ok
|
||||
def put_meta(registry, key, value) when is_atom(registry) and (is_atom(key) or is_tuple(key)) do
|
||||
try do
|
||||
:ets.insert(registry, {key, value})
|
||||
:ok
|
||||
catch
|
||||
:error, :badarg ->
|
||||
raise ArgumentError, "unknown registry: #{inspect registry}"
|
||||
end
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
@compile {:inline, hash: 2}
|
||||
|
||||
defp hash(term, limit) do
|
||||
:erlang.phash2(term, limit)
|
||||
end
|
||||
|
||||
defp info!(registry) do
|
||||
try do
|
||||
:ets.lookup_element(registry, @all_info, 2)
|
||||
catch
|
||||
:error, :badarg ->
|
||||
raise ArgumentError, "unknown registry: #{inspect registry}"
|
||||
end
|
||||
end
|
||||
|
||||
defp key_info!(registry) do
|
||||
try do
|
||||
:ets.lookup_element(registry, @key_info, 2)
|
||||
catch
|
||||
:error, :badarg ->
|
||||
raise ArgumentError, "unknown registry: #{inspect registry}"
|
||||
end
|
||||
end
|
||||
|
||||
defp key_ets!(registry, key, partitions) do
|
||||
:ets.lookup_element(registry, hash(key, partitions), 2)
|
||||
end
|
||||
|
||||
defp key_ets!(registry, partition) do
|
||||
:ets.lookup_element(registry, partition, 2)
|
||||
end
|
||||
|
||||
defp pid_ets!(registry, key, partitions) do
|
||||
:ets.lookup_element(registry, hash(key, partitions), 3)
|
||||
end
|
||||
|
||||
defp pid_ets!(registry, partition) do
|
||||
:ets.lookup_element(registry, partition, 3)
|
||||
end
|
||||
|
||||
defp safe_lookup_second(ets, key) do
|
||||
try do
|
||||
:ets.lookup_element(ets, key, 2)
|
||||
catch
|
||||
:error, :badarg -> []
|
||||
end
|
||||
end
|
||||
|
||||
defp partitions(:unique, key, pid, partitions) do
|
||||
{hash(key, partitions), hash(pid, partitions)}
|
||||
end
|
||||
defp partitions(:duplicate, _key, pid, partitions) do
|
||||
partition = hash(pid, partitions)
|
||||
{partition, partition}
|
||||
end
|
||||
|
||||
defp unlink_if_unregistered(pid_server, pid_ets, self) do
|
||||
unless :ets.member(pid_ets, self) do
|
||||
Process.unlink(pid_server)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Registry.Supervisor do
|
||||
@moduledoc false
|
||||
use Supervisor
|
||||
|
||||
def start_link(kind, registry, partitions, listeners, entries) do
|
||||
Supervisor.start_link(__MODULE__, {kind, registry, partitions, listeners, entries}, name: registry)
|
||||
end
|
||||
|
||||
def init({kind, registry, partitions, listeners, entries}) do
|
||||
^registry = :ets.new(registry, [:set, :public, :named_table, read_concurrency: true])
|
||||
true = :ets.insert(registry, entries)
|
||||
|
||||
children =
|
||||
for i <- 0..partitions-1 do
|
||||
key_partition = Registry.Partition.key_name(registry, i)
|
||||
pid_partition = Registry.Partition.pid_name(registry, i)
|
||||
arg = {kind, registry, i, partitions, key_partition, pid_partition, listeners}
|
||||
worker(Registry.Partition, [pid_partition, arg], id: pid_partition)
|
||||
end
|
||||
|
||||
supervise(children, strategy: strategy_for_kind(kind))
|
||||
end
|
||||
|
||||
# Unique registries have their key partition hashed by key.
|
||||
# This means that, if a pid partition crashes, it may have
|
||||
# entries from all key partitions, so we need to crash all.
|
||||
defp strategy_for_kind(:unique), do: :one_for_all
|
||||
|
||||
# Duplicate registries have both key and pid partitions hashed
|
||||
# by pid. This means that, if a pid partition crashes, all of
|
||||
# its associated entries are in its sibling table, so we crash one.
|
||||
defp strategy_for_kind(:duplicate), do: :one_for_one
|
||||
end
|
||||
|
||||
defmodule Registry.Partition do
|
||||
@moduledoc false
|
||||
|
||||
# This process owns the equivalent key and pid ets tables
|
||||
# and is responsible for monitoring processes that map to
|
||||
# the its own pid table.
|
||||
use GenServer
|
||||
@all_info -1
|
||||
@key_info -2
|
||||
|
||||
@doc """
|
||||
Returns the name of key partition table.
|
||||
"""
|
||||
@spec key_name(atom, non_neg_integer) :: atom
|
||||
def key_name(registry, partition) do
|
||||
Module.concat(registry, "KeyPartition" <> Integer.to_string(partition))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the name of pid partition table.
|
||||
"""
|
||||
@spec pid_name(atom, non_neg_integer) :: atom
|
||||
def pid_name(name, partition) do
|
||||
Module.concat(name, "PIDPartition" <> Integer.to_string(partition))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts the registry partition.
|
||||
|
||||
The process is only responsible for monitoring, demonitoring
|
||||
and cleaning up when monitored processes crash.
|
||||
"""
|
||||
def start_link(registry, arg) do
|
||||
GenServer.start_link(__MODULE__, arg, name: registry)
|
||||
end
|
||||
|
||||
## Callbacks
|
||||
|
||||
def init({kind, registry, i, partitions, key_partition, pid_partition, listeners}) do
|
||||
Process.flag(:trap_exit, true)
|
||||
key_ets = init_key_ets(kind, key_partition)
|
||||
pid_ets = init_pid_ets(kind, pid_partition)
|
||||
|
||||
# If we have only one partition, we do an optimization which
|
||||
# is to write the table information alongside the registry info.
|
||||
if partitions == 1 do
|
||||
entries =
|
||||
[{@key_info, {kind, partitions, key_ets}},
|
||||
{@all_info, {kind, partitions, key_ets, {self(), pid_ets}, listeners}}]
|
||||
true = :ets.insert(registry, entries)
|
||||
else
|
||||
true = :ets.insert(registry, {i, key_ets, {self(), pid_ets}})
|
||||
end
|
||||
|
||||
{:ok, pid_ets}
|
||||
end
|
||||
|
||||
# The key partition is a set for unique keys,
|
||||
# duplicate bag for duplicate ones.
|
||||
defp init_key_ets(:unique, key_partition) do
|
||||
:ets.new(key_partition, [:set, :public, read_concurrency: true, write_concurrency: true])
|
||||
end
|
||||
defp init_key_ets(:duplicate, key_partition) do
|
||||
:ets.new(key_partition, [:duplicate_bag, :public, read_concurrency: true, write_concurrency: true])
|
||||
end
|
||||
|
||||
# A process can always have multiple keys, so the
|
||||
# pid partition is always a duplicate bag.
|
||||
defp init_pid_ets(_, pid_partition) do
|
||||
:ets.new(pid_partition, [:duplicate_bag, :public, read_concurrency: true, write_concurrency: true])
|
||||
end
|
||||
|
||||
def handle_call(:sync, _, state) do
|
||||
{:reply, :ok, state}
|
||||
end
|
||||
|
||||
def handle_info({:EXIT, pid, _reason}, ets) do
|
||||
entries = :ets.take(ets, pid)
|
||||
for {_pid, key, key_ets} <- entries do
|
||||
try do
|
||||
:ets.match_delete(key_ets, {key, {pid, :_}})
|
||||
catch
|
||||
:error, :badarg -> :badarg
|
||||
end
|
||||
end
|
||||
{:noreply, ets}
|
||||
end
|
||||
def handle_info(msg, state) do
|
||||
super(msg, state)
|
||||
end
|
||||
end
|
||||
+14
-2
@@ -9,8 +9,20 @@ defmodule Set do
|
||||
@type values :: [ value ]
|
||||
@type t :: map
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
# (hard-deprecated in elixir_dispatch)
|
||||
# TODO: Remove callbacks on 1.3
|
||||
# TODO: Deprecate every function on 1.3
|
||||
@callback new :: t
|
||||
@callback delete(t, value) :: t
|
||||
@callback difference(t, t) :: t
|
||||
@callback disjoint?(t, t) :: boolean
|
||||
@callback equal?(t, t) :: boolean
|
||||
@callback intersection(t, t) :: t
|
||||
@callback member?(t, value) :: boolean
|
||||
@callback put(t, value) :: t
|
||||
@callback size(t) :: non_neg_integer
|
||||
@callback subset?(t, t) :: boolean
|
||||
@callback to_list(t) :: list()
|
||||
@callback union(t, t) :: t
|
||||
|
||||
defmacrop target(set) do
|
||||
quote do
|
||||
|
||||
+145
-303
@@ -96,6 +96,7 @@ defmodule Stream do
|
||||
@type element :: any
|
||||
@type index :: non_neg_integer
|
||||
@type default :: any
|
||||
@opaque t :: %__MODULE__{}
|
||||
|
||||
# Require Stream.Reducers and its callbacks
|
||||
require Stream.Reducers, as: R
|
||||
@@ -104,18 +105,18 @@ defmodule Stream do
|
||||
{:cont, acc}
|
||||
end
|
||||
|
||||
defmacrop next(fun, entry, acc) do
|
||||
quote do: unquote(fun).(unquote(entry), unquote(acc))
|
||||
defmacrop next(f, entry, acc) do
|
||||
quote do: unquote(f).(unquote(entry), unquote(acc))
|
||||
end
|
||||
|
||||
defmacrop acc(head, state, tail) do
|
||||
quote do: [unquote(head), unquote(state) | unquote(tail)]
|
||||
defmacrop acc(h, n, t) do
|
||||
quote do: [unquote(h), unquote(n)|unquote(t)]
|
||||
end
|
||||
|
||||
defmacrop next_with_acc(fun, entry, head, state, tail) do
|
||||
defmacrop next_with_acc(f, entry, h, n, t) do
|
||||
quote do
|
||||
{reason, [head | tail]} = unquote(fun).(unquote(entry), [unquote(head) | unquote(tail)])
|
||||
{reason, [head, unquote(state) | tail]}
|
||||
{reason, [h|t]} = unquote(f).(unquote(entry), [unquote(h)|unquote(t)])
|
||||
{reason, [h, unquote(n)|t]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -124,7 +125,7 @@ defmodule Stream do
|
||||
@doc """
|
||||
Shortcut to `chunk(enum, n, n)`.
|
||||
"""
|
||||
@spec chunk(Enumerable.t, pos_integer) :: Enumerable.t
|
||||
@spec chunk(Enumerable.t, non_neg_integer) :: Enumerable.t
|
||||
def chunk(enum, n), do: chunk(enum, n, n, nil)
|
||||
|
||||
@doc """
|
||||
@@ -134,9 +135,9 @@ defmodule Stream do
|
||||
`step` is optional and, if not passed, defaults to `n`, i.e.
|
||||
chunks do not overlap. If the final chunk does not have `n`
|
||||
elements to fill the chunk, elements are taken as necessary
|
||||
from `leftover` if it was passed. If `leftover` is passed and
|
||||
does not have enough elements to fill the chunk, then the chunk is
|
||||
returned anyway with less than `n` elements. If `leftover` is not
|
||||
from `pad` if it was passed. If `pad` is passed and does not
|
||||
have enough elements to fill the chunk, then the chunk is
|
||||
returned anyway with less than `n` elements. If `pad` is not
|
||||
passed at all or is `nil`, then the partial chunk is discarded
|
||||
from the result.
|
||||
|
||||
@@ -157,15 +158,14 @@ defmodule Stream do
|
||||
"""
|
||||
@spec chunk(Enumerable.t, pos_integer, pos_integer) :: Enumerable.t
|
||||
@spec chunk(Enumerable.t, pos_integer, pos_integer, Enumerable.t | nil) :: Enumerable.t
|
||||
def chunk(enum, n, step, leftover \\ nil)
|
||||
when is_integer(n) and n > 0 and is_integer(step) and step > 0 do
|
||||
def chunk(enum, n, step, pad \\ nil) when n > 0 and step > 0 do
|
||||
limit = :erlang.max(n, step)
|
||||
if is_nil(leftover) do
|
||||
if is_nil(pad) do
|
||||
lazy enum, {[], 0}, fn(f1) -> R.chunk(n, step, limit, f1) end
|
||||
else
|
||||
lazy enum, {[], 0},
|
||||
fn(f1) -> R.chunk(n, step, limit, f1) end,
|
||||
&do_chunk(&1, n, leftover, &2)
|
||||
&do_chunk(&1, n, pad, &2)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -173,8 +173,8 @@ defmodule Stream do
|
||||
{:cont, acc}
|
||||
end
|
||||
|
||||
defp do_chunk(acc(h, {buffer, count} = old, t), n, leftover, f1) do
|
||||
buffer = :lists.reverse(buffer, Enum.take(leftover, n - count))
|
||||
defp do_chunk(acc(h, {buffer, count} = old, t), n, pad, f1) do
|
||||
buffer = :lists.reverse(buffer, Enum.take(pad, n - count))
|
||||
next_with_acc(f1, buffer, h, old, t)
|
||||
end
|
||||
|
||||
@@ -270,54 +270,23 @@ defmodule Stream do
|
||||
fn
|
||||
entry, [h, {count, buf1, []} | t] ->
|
||||
do_drop(:cont, n, entry, h, count, buf1, [], t)
|
||||
entry, [h, {count, buf1, [next | buf2]} | t] ->
|
||||
{reason, [h | t]} = f1.(next, [h | t])
|
||||
entry, [h, {count, buf1, [next|buf2]} | t] ->
|
||||
{reason, [h|t]} = f1.(next, [h|t])
|
||||
do_drop(reason, n, entry, h, count, buf1, buf2, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp do_drop(reason, n, entry, h, count, buf1, buf2, t) do
|
||||
buf1 = [entry | buf1]
|
||||
buf1 = [entry|buf1]
|
||||
count = count + 1
|
||||
if count == n do
|
||||
{reason, [h, {0, [], :lists.reverse(buf1)} | t]}
|
||||
{reason, [h, {0, [], :lists.reverse(buf1)}|t]}
|
||||
else
|
||||
{reason, [h, {count, buf1, buf2} | t]}
|
||||
{reason, [h, {count, buf1, buf2}|t]}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a stream that drops every `nth` item from the enumerable.
|
||||
|
||||
The first item is always dropped, unless `nth` is 0.
|
||||
|
||||
`nth` must be a non-negative integer.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> stream = Stream.drop_every(1..10, 2)
|
||||
iex> Enum.to_list(stream)
|
||||
[2, 4, 6, 8, 10]
|
||||
|
||||
iex> stream = Stream.drop_every(1..1000, 1)
|
||||
iex> Enum.to_list(stream)
|
||||
[]
|
||||
|
||||
iex> stream = Stream.drop_every([1, 2, 3, 4, 5], 0)
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 2, 3, 4, 5]
|
||||
|
||||
"""
|
||||
@spec drop_every(Enumerable.t, non_neg_integer) :: Enumerable.t
|
||||
def drop_every(enum, nth)
|
||||
def drop_every(enum, 0), do: %Stream{enum: enum}
|
||||
def drop_every([], _nth), do: %Stream{enum: []}
|
||||
|
||||
def drop_every(enum, nth) when is_integer(nth) and nth > 0 do
|
||||
lazy enum, nth, fn(f1) -> R.drop_every(nth, f1) end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily drops elements of the enumerable while the given
|
||||
function returns `true`.
|
||||
@@ -341,7 +310,7 @@ defmodule Stream do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> stream = Stream.each([1, 2, 3], fn(x) -> send self(), x end)
|
||||
iex> stream = Stream.each([1, 2, 3], fn(x) -> send self, x end)
|
||||
iex> Enum.to_list(stream)
|
||||
iex> receive do: (x when is_integer(x) -> x)
|
||||
1
|
||||
@@ -352,7 +321,7 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec each(Enumerable.t, (element -> term)) :: Enumerable.t
|
||||
def each(enum, fun) when is_function(fun, 1) do
|
||||
def each(enum, fun) do
|
||||
lazy enum, fn(f1) ->
|
||||
fn(x, acc) ->
|
||||
fun.(x)
|
||||
@@ -363,7 +332,7 @@ defmodule Stream do
|
||||
|
||||
@doc """
|
||||
Creates a stream that will apply the given function on enumeration and
|
||||
flatten the result, but only one level deep.
|
||||
flatten the result.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -371,10 +340,6 @@ defmodule Stream do
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 2, 2, 4, 3, 6]
|
||||
|
||||
iex> stream = Stream.flat_map([1, 2, 3], fn(x) -> [[x]] end)
|
||||
iex> Enum.to_list(stream)
|
||||
[[1], [2], [3]]
|
||||
|
||||
"""
|
||||
@spec flat_map(Enumerable.t, (element -> Enumerable.t)) :: Enumerable.t
|
||||
def flat_map(enum, mapper) do
|
||||
@@ -416,16 +381,9 @@ defmodule Stream do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a stream that emits a value after the given period `n`
|
||||
in milliseconds.
|
||||
Creates a stream that emits a value after the given period `n` in milliseconds.
|
||||
|
||||
The values emitted are an increasing counter starting at `0`.
|
||||
This operation will block the caller by the given interval
|
||||
every time a new item is streamed.
|
||||
|
||||
Do not use this function to generate a sequence of numbers.
|
||||
If blocking the caller process is not necessary, use
|
||||
`Stream.iterate(0, & &1 + 1)` instead.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -433,10 +391,11 @@ defmodule Stream do
|
||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
|
||||
"""
|
||||
# TODO: Allow it to handle system messages.
|
||||
@spec interval(non_neg_integer) :: Enumerable.t
|
||||
def interval(n) do
|
||||
unfold 0, fn(count) ->
|
||||
Process.sleep(n)
|
||||
unfold 0, fn (count) ->
|
||||
:timer.sleep(n)
|
||||
{count, count + 1}
|
||||
end
|
||||
end
|
||||
@@ -447,33 +406,33 @@ defmodule Stream do
|
||||
This function is often used with `run/1` since any evaluation
|
||||
is delayed until the stream is executed. See `run/1` for an example.
|
||||
"""
|
||||
@spec into(Enumerable.t, Collectable.t, (term -> term)) :: Enumerable.t
|
||||
def into(enum, collectable, transform \\ fn x -> x end) when is_function(transform, 1) do
|
||||
@spec into(Enumerable.t, Collectable.t) :: Enumerable.t
|
||||
def into(enum, collectable, transform \\ fn x -> x end) do
|
||||
&do_into(enum, collectable, transform, &1, &2)
|
||||
end
|
||||
|
||||
defp do_into(enum, collectable, transform, acc, fun) do
|
||||
{initial, into} = Collectable.into(collectable)
|
||||
composed = fn x, [acc | collectable] ->
|
||||
composed = fn x, [acc|collectable] ->
|
||||
collectable = into.(collectable, {:cont, transform.(x)})
|
||||
{reason, acc} = fun.(x, acc)
|
||||
{reason, [acc | collectable]}
|
||||
{reason, [acc|collectable]}
|
||||
end
|
||||
do_into(&Enumerable.reduce(enum, &1, composed), initial, into, acc)
|
||||
end
|
||||
|
||||
defp do_into(reduce, collectable, into, {command, acc}) do
|
||||
try do
|
||||
reduce.({command, [acc | collectable]})
|
||||
reduce.({command, [acc|collectable]})
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
into.(collectable, :halt)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
else
|
||||
{:suspended, [acc | collectable], continuation} ->
|
||||
{:suspended, [acc|collectable], continuation} ->
|
||||
{:suspended, acc, &do_into(continuation, collectable, into, &1)}
|
||||
{reason, [acc | collectable]} ->
|
||||
{reason, [acc|collectable]} ->
|
||||
into.(collectable, :done)
|
||||
{reason, acc}
|
||||
end
|
||||
@@ -495,41 +454,6 @@ defmodule Stream do
|
||||
lazy enum, fn(f1) -> R.map(fun, f1) end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a stream that will apply the given function on
|
||||
every `nth` item from the enumerable.
|
||||
|
||||
The first item is always passed to the given function.
|
||||
|
||||
`nth` must be a non-negative integer.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> stream = Stream.map_every(1..10, 2, fn(x) -> x * 2 end)
|
||||
iex> Enum.to_list(stream)
|
||||
[2, 2, 6, 4, 10, 6, 14, 8, 18, 10]
|
||||
|
||||
iex> stream = Stream.map_every([1, 2, 3, 4, 5], 1, fn(x) -> x * 2 end)
|
||||
iex> Enum.to_list(stream)
|
||||
[2, 4, 6, 8, 10]
|
||||
|
||||
iex> stream = Stream.map_every(1..5, 0, fn(x) -> x * 2 end)
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 2, 3, 4, 5]
|
||||
|
||||
"""
|
||||
@spec map_every(Enumerable.t, non_neg_integer, (element -> any)) :: Enumerable.t
|
||||
def map_every(enum, nth, fun)
|
||||
|
||||
def map_every(enum, 1, fun), do: map(enum, fun)
|
||||
def map_every(enum, 0, _fun), do: %Stream{enum: enum}
|
||||
def map_every([], _nth, _fun), do: %Stream{enum: []}
|
||||
|
||||
def map_every(enum, nth, fun) when is_integer(nth) and nth > 0 do
|
||||
lazy enum, nth, fn(f1) -> R.map_every(nth, fun, f1) end
|
||||
end
|
||||
|
||||
|
||||
@doc """
|
||||
Creates a stream that will reject elements according to
|
||||
the given function on enumeration.
|
||||
@@ -585,7 +509,7 @@ defmodule Stream do
|
||||
"""
|
||||
@spec scan(Enumerable.t, (element, acc -> any)) :: Enumerable.t
|
||||
def scan(enum, fun) do
|
||||
lazy enum, :first, fn(f1) -> R.scan2(fun, f1) end
|
||||
lazy enum, :first, fn(f1) -> R.scan_2(fun, f1) end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -602,7 +526,7 @@ defmodule Stream do
|
||||
"""
|
||||
@spec scan(Enumerable.t, acc, (element, acc -> any)) :: Enumerable.t
|
||||
def scan(enum, acc, fun) do
|
||||
lazy enum, acc, fn(f1) -> R.scan3(fun, f1) end
|
||||
lazy enum, acc, fn(f1) -> R.scan_3(fun, f1) end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -647,7 +571,7 @@ defmodule Stream do
|
||||
|
||||
The first item is always included, unless `nth` is 0.
|
||||
|
||||
`nth` must be a non-negative integer.
|
||||
`nth` must be a non-negative integer, or `FunctionClauseError` will be thrown.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -665,7 +589,6 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec take_every(Enumerable.t, non_neg_integer) :: Enumerable.t
|
||||
def take_every(enum, nth)
|
||||
def take_every(_enum, 0), do: %Stream{enum: []}
|
||||
def take_every([], _nth), do: %Stream{enum: []}
|
||||
|
||||
@@ -692,8 +615,7 @@ defmodule Stream do
|
||||
@doc """
|
||||
Creates a stream that emits a single value after `n` milliseconds.
|
||||
|
||||
The value emitted is `0`. This operation will block the caller by
|
||||
the given time until the item is streamed.
|
||||
The value emitted is `0`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -733,9 +655,9 @@ defmodule Stream do
|
||||
[1, 2, 3]
|
||||
|
||||
"""
|
||||
@spec transform(Enumerable.t, acc, fun) :: Enumerable.t
|
||||
when fun: (element, acc -> {Enumerable.t, acc} | {:halt, acc}),
|
||||
acc: any
|
||||
@spec transform(Enumerable.t, acc, fun) :: Enumerable.t when
|
||||
fun: (element, acc -> {Enumerable.t, acc} | {:halt, acc}),
|
||||
acc: any
|
||||
def transform(enum, acc, reducer) when is_function(reducer, 2) do
|
||||
&do_transform(enum, fn -> acc end, reducer, &1, &2, nil)
|
||||
end
|
||||
@@ -750,9 +672,9 @@ defmodule Stream do
|
||||
This function can be seen as a combination of `Stream.resource/3` with
|
||||
`Stream.transform/3`.
|
||||
"""
|
||||
@spec transform(Enumerable.t, (() -> acc), fun, (acc -> term)) :: Enumerable.t
|
||||
when fun: (element, acc -> {Enumerable.t, acc} | {:halt, acc}),
|
||||
acc: any
|
||||
@spec transform(Enumerable.t, (() -> acc), fun, (acc -> term)) :: Enumerable.t when
|
||||
fun: (element, acc -> {Enumerable.t, acc} | {:halt, acc}),
|
||||
acc: any
|
||||
def transform(enum, start_fun, reducer, after_fun)
|
||||
when is_function(start_fun, 0) and is_function(reducer, 2) and is_function(after_fun, 1) do
|
||||
&do_transform(enum, start_fun, reducer, &1, &2, after_fun)
|
||||
@@ -762,125 +684,107 @@ defmodule Stream do
|
||||
inner = &do_transform_each(&1, &2, fun)
|
||||
step = &do_transform_step(&1, &2)
|
||||
next = &Enumerable.reduce(enumerables, &1, step)
|
||||
do_transform(user_acc.(), user, fun, :cont, next, inner_acc, inner, after_fun)
|
||||
do_transform(user_acc.(), user, fun, [], next, inner_acc, inner, after_fun)
|
||||
end
|
||||
|
||||
defp do_transform(user_acc, _user, _fun, _next_op, next, {:halt, inner_acc}, _inner, after_fun) do
|
||||
next.({:halt, []})
|
||||
defp do_transform(user_acc, _user, _fun, _next_acc, _next, {:halt, inner_acc}, _inner, after_fun) do
|
||||
do_after(after_fun, user_acc)
|
||||
{:halted, inner_acc}
|
||||
end
|
||||
|
||||
defp do_transform(user_acc, user, fun, next_op, next, {:suspend, inner_acc}, inner, after_fun) do
|
||||
{:suspended, inner_acc, &do_transform(user_acc, user, fun, next_op, next, &1, inner, after_fun)}
|
||||
defp do_transform(user_acc, user, fun, next_acc, next, {:suspend, inner_acc}, inner, after_fun) do
|
||||
{:suspended, inner_acc, &do_transform(user_acc, user, fun, next_acc, next, &1, inner, after_fun)}
|
||||
end
|
||||
|
||||
defp do_transform(user_acc, _user, _fun, :halt, _next, {_, inner_acc}, _inner, after_fun) do
|
||||
do_after(after_fun, user_acc)
|
||||
{:halted, inner_acc}
|
||||
end
|
||||
|
||||
defp do_transform(user_acc, user, fun, :cont, next, inner_acc, inner, after_fun) do
|
||||
try do
|
||||
next.({:cont, []})
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
defp do_transform(user_acc, user, fun, next_acc, next, inner_acc, inner, after_fun) do
|
||||
case next.({:cont, next_acc}) do
|
||||
{:suspended, [val|next_acc], next} ->
|
||||
try do
|
||||
user.(val, user_acc)
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
next.({:halt, next_acc})
|
||||
do_after(after_fun, user_acc)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
else
|
||||
{[], user_acc} ->
|
||||
do_transform(user_acc, user, fun, next_acc, next, inner_acc, inner, after_fun)
|
||||
{list, user_acc} when is_list(list) ->
|
||||
do_list_transform(user_acc, user, fun, next_acc, next, inner_acc, inner,
|
||||
&Enumerable.List.reduce(list, &1, fun), after_fun)
|
||||
{:halt, user_acc} ->
|
||||
next.({:halt, next_acc})
|
||||
do_after(after_fun, user_acc)
|
||||
{:halted, elem(inner_acc, 1)}
|
||||
{other, user_acc} ->
|
||||
do_enum_transform(user_acc, user, fun, next_acc, next, inner_acc, inner,
|
||||
&Enumerable.reduce(other, &1, inner), after_fun)
|
||||
end
|
||||
{reason, _} ->
|
||||
do_after(after_fun, user_acc)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
else
|
||||
{:suspended, vals, next} ->
|
||||
do_transform_user(:lists.reverse(vals), user_acc, user, fun, :cont, next, inner_acc, inner, after_fun)
|
||||
{_, vals} ->
|
||||
do_transform_user(:lists.reverse(vals), user_acc, user, fun, :halt, next, inner_acc, inner, after_fun)
|
||||
{reason, elem(inner_acc, 1)}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_transform_user([], user_acc, user, fun, next_op, next, inner_acc, inner, after_fun) do
|
||||
do_transform(user_acc, user, fun, next_op, next, inner_acc, inner, after_fun)
|
||||
end
|
||||
|
||||
defp do_transform_user([val | vals], user_acc, user, fun, next_op, next, inner_acc, inner, after_fun) do
|
||||
user.(val, user_acc)
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
next.({:halt, []})
|
||||
do_after(after_fun, user_acc)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
else
|
||||
{[], user_acc} ->
|
||||
do_transform_user(vals, user_acc, user, fun, next_op, next, inner_acc, inner, after_fun)
|
||||
{list, user_acc} when is_list(list) ->
|
||||
do_list_transform(vals, user_acc, user, fun, next_op, next, inner_acc, inner,
|
||||
&Enumerable.List.reduce(list, &1, fun), after_fun)
|
||||
{:halt, user_acc} ->
|
||||
next.({:halt, []})
|
||||
do_after(after_fun, user_acc)
|
||||
{:halted, elem(inner_acc, 1)}
|
||||
{other, user_acc} ->
|
||||
do_enum_transform(vals, user_acc, user, fun, next_op, next, inner_acc, inner,
|
||||
&Enumerable.reduce(other, &1, inner), after_fun)
|
||||
end
|
||||
|
||||
defp do_list_transform(vals, user_acc, user, fun, next_op, next, inner_acc, inner, reduce, after_fun) do
|
||||
defp do_list_transform(user_acc, user, fun, next_acc, next, inner_acc, inner, reduce, after_fun) do
|
||||
try do
|
||||
reduce.(inner_acc)
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
next.({:halt, []})
|
||||
next.({:halt, next_acc})
|
||||
do_after(after_fun, user_acc)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
else
|
||||
{:done, acc} ->
|
||||
do_transform_user(vals, user_acc, user, fun, next_op, next, {:cont, acc}, inner, after_fun)
|
||||
do_transform(user_acc, user, fun, next_acc, next, {:cont, acc}, inner, after_fun)
|
||||
{:halted, acc} ->
|
||||
next.({:halt, []})
|
||||
next.({:halt, next_acc})
|
||||
do_after(after_fun, user_acc)
|
||||
{:halted, acc}
|
||||
{:suspended, acc, c} ->
|
||||
{:suspended, acc, &do_list_transform(vals, user_acc, user, fun, next_op, next, &1, inner, c, after_fun)}
|
||||
{:suspended, acc, &do_list_transform(user_acc, user, fun, next_acc, next, &1, inner, c, after_fun)}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_enum_transform(vals, user_acc, user, fun, next_op, next, {op, inner_acc}, inner, reduce, after_fun) do
|
||||
defp do_enum_transform(user_acc, user, fun, next_acc, next, {op, inner_acc}, inner, reduce, after_fun) do
|
||||
try do
|
||||
reduce.({op, [:outer | inner_acc]})
|
||||
reduce.({op, [:outer|inner_acc]})
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
next.({:halt, []})
|
||||
next.({:halt, next_acc})
|
||||
do_after(after_fun, user_acc)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
else
|
||||
# Only take into account outer halts when the op is not halt itself.
|
||||
# Otherwise, we were the ones wishing to halt, so we should just stop.
|
||||
{:halted, [:outer | acc]} when op != :halt ->
|
||||
do_transform_user(vals, user_acc, user, fun, next_op, next, {:cont, acc}, inner, after_fun)
|
||||
{:halted, [_ | acc]} ->
|
||||
next.({:halt, []})
|
||||
{:halted, [:outer|acc]} when op != :halt ->
|
||||
do_transform(user_acc, user, fun, next_acc, next, {:cont, acc}, inner, after_fun)
|
||||
{:halted, [_|acc]} ->
|
||||
next.({:halt, next_acc})
|
||||
do_after(after_fun, user_acc)
|
||||
{:halted, acc}
|
||||
{:done, [_ | acc]} ->
|
||||
do_transform_user(vals, user_acc, user, fun, next_op, next, {:cont, acc}, inner, after_fun)
|
||||
{:suspended, [_ | acc], c} ->
|
||||
{:suspended, acc, &do_enum_transform(vals, user_acc, user, fun, next_op, next, &1, inner, c, after_fun)}
|
||||
{:done, [_|acc]} ->
|
||||
do_transform(user_acc, user, fun, next_acc, next, {:cont, acc}, inner, after_fun)
|
||||
{:suspended, [_|acc], c} ->
|
||||
{:suspended, acc, &do_enum_transform(user_acc, user, fun, next_acc, next, &1, inner, c, after_fun)}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_after(nil, _user_acc), do: :ok
|
||||
defp do_after(fun, user_acc), do: fun.(user_acc)
|
||||
|
||||
defp do_transform_each(x, [:outer | acc], f) do
|
||||
defp do_transform_each(x, [:outer|acc], f) do
|
||||
case f.(x, acc) do
|
||||
{:halt, res} -> {:halt, [:inner | res]}
|
||||
{op, res} -> {op, [:outer | res]}
|
||||
{:halt, res} -> {:halt, [:inner|res]}
|
||||
{op, res} -> {op, [:outer|res]}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_transform_step(x, acc) do
|
||||
{:suspend, [x | acc]}
|
||||
{:suspend, [x|acc]}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -896,49 +800,20 @@ defmodule Stream do
|
||||
iex> Stream.uniq([1, 2, 3, 3, 2, 1]) |> Enum.to_list
|
||||
[1, 2, 3]
|
||||
|
||||
"""
|
||||
@spec uniq(Enumerable.t) :: Enumerable.t
|
||||
def uniq(enum) do
|
||||
uniq_by(enum, fn x -> x end)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def uniq(enum, fun) do
|
||||
uniq_by(enum, fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a stream that only emits elements if they are unique, by removing the
|
||||
elements for which function `fun` returned duplicate items.
|
||||
|
||||
The function `fun` maps every element to a term which is used to
|
||||
determine if two elements are duplicates.
|
||||
|
||||
Keep in mind that, in order to know if an element is unique
|
||||
or not, this function needs to store all unique values emitted
|
||||
by the stream. Therefore, if the stream is infinite, the number
|
||||
of items stored will grow infinitely, never being garbage collected.
|
||||
|
||||
## Example
|
||||
|
||||
iex> Stream.uniq_by([{1, :x}, {2, :y}, {1, :z}], fn {x, _} -> x end) |> Enum.to_list
|
||||
iex> Stream.uniq([{1, :x}, {2, :y}, {2, :z}, {1, :x}], fn {x, _} -> x end) |> Enum.to_list
|
||||
[{1, :x}, {2, :y}]
|
||||
|
||||
iex> Stream.uniq_by([a: {:tea, 2}, b: {:tea, 2}, c: {:coffee, 1}], fn {_, y} -> y end) |> Enum.to_list
|
||||
[a: {:tea, 2}, c: {:coffee, 1}]
|
||||
|
||||
"""
|
||||
@spec uniq_by(Enumerable.t, (element -> term)) :: Enumerable.t
|
||||
def uniq_by(enum, fun) do
|
||||
lazy enum, %{}, fn f1 -> R.uniq_by(fun, f1) end
|
||||
@spec uniq(Enumerable.t) :: Enumerable.t
|
||||
@spec uniq(Enumerable.t, (element -> term)) :: Enumerable.t
|
||||
def uniq(enum, fun \\ fn x -> x end) do
|
||||
lazy enum, %{}, fn f1 -> R.uniq(fun, f1) end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a stream where each item in the enumerable will
|
||||
be wrapped in a tuple alongside its index.
|
||||
|
||||
If an `offset` is given, we will index from the given offset instead of from zero.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> stream = Stream.with_index([1, 2, 3])
|
||||
@@ -1008,35 +883,15 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec zip(Enumerable.t, Enumerable.t) :: Enumerable.t
|
||||
def zip(left, right), do: zip([left, right])
|
||||
|
||||
@doc """
|
||||
Zips corresponding elements from a collection of enumerables
|
||||
into one stream of tuples.
|
||||
|
||||
The zipping finishes as soon as any enumerable completes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> concat = Stream.concat(1..3, 4..6)
|
||||
iex> cycle = Stream.cycle(["foo", "bar", "baz"])
|
||||
iex> Stream.zip([concat, [:a, :b, :c], cycle]) |> Enum.to_list
|
||||
[{1, :a, "foo"}, {2, :b, "bar"}, {3, :c, "baz"}]
|
||||
|
||||
"""
|
||||
@spec zip([Enumerable.t]) :: Enumerable.t
|
||||
def zip(enumerables) do
|
||||
def zip(left, right) do
|
||||
step = &do_zip_step(&1, &2)
|
||||
enum_funs = Enum.map(enumerables, fn enum ->
|
||||
{&Enumerable.reduce(enum, &1, step), :cont}
|
||||
end)
|
||||
left_fun = &Enumerable.reduce(left, &1, step)
|
||||
right_fun = &Enumerable.reduce(right, &1, step)
|
||||
|
||||
&do_zip(enum_funs, &1, &2)
|
||||
# Return a function as a lazy enumerator.
|
||||
&do_zip([{left_fun, []}, {right_fun, []}], &1, &2)
|
||||
end
|
||||
|
||||
# This implementation of do_zip/3 works for any number of
|
||||
# streams to zip, even if right now zip/2 only zips two streams.
|
||||
|
||||
defp do_zip(zips, {:halt, acc}, _fun) do
|
||||
do_zip_close(zips)
|
||||
{:halted, acc}
|
||||
@@ -1048,7 +903,7 @@ defmodule Stream do
|
||||
|
||||
defp do_zip(zips, {:cont, acc}, callback) do
|
||||
try do
|
||||
do_zip_next_tuple(zips, acc, callback, [], [])
|
||||
do_zip(zips, acc, callback, [], [])
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
@@ -1057,47 +912,34 @@ defmodule Stream do
|
||||
else
|
||||
{:next, buffer, acc} ->
|
||||
do_zip(buffer, acc, callback)
|
||||
{:done, _acc} = other ->
|
||||
other
|
||||
{:done, _} = o ->
|
||||
o
|
||||
end
|
||||
end
|
||||
|
||||
# do_zip_next_tuple/5 computes the next tuple formed by
|
||||
# the next element of each zipped stream.
|
||||
|
||||
defp do_zip_next_tuple([{_, :halt} | zips], acc, _callback, _yielded_elems, buffer) do
|
||||
do_zip_close(:lists.reverse(buffer, zips))
|
||||
{:done, acc}
|
||||
end
|
||||
|
||||
defp do_zip_next_tuple([{fun, :cont} | zips], acc, callback, yielded_elems, buffer) do
|
||||
case fun.({:cont, []}) do
|
||||
{:suspended, [elem], fun} ->
|
||||
do_zip_next_tuple(zips, acc, callback, [elem | yielded_elems], [{fun, :cont} | buffer])
|
||||
{_, [elem]} ->
|
||||
do_zip_next_tuple(zips, acc, callback, [elem | yielded_elems], [{fun, :halt} | buffer])
|
||||
{_, []} ->
|
||||
# The current zipped stream terminated, so we close all the streams
|
||||
# and return {:halted, acc} (which is returned as is by do_zip/3).
|
||||
do_zip_close(:lists.reverse(buffer, zips))
|
||||
defp do_zip([{fun, fun_acc}|t], acc, callback, list, buffer) do
|
||||
case fun.({:cont, fun_acc}) do
|
||||
{:suspended, [i|fun_acc], fun} ->
|
||||
do_zip(t, acc, callback, [i|list], [{fun, fun_acc}|buffer])
|
||||
{_, _} ->
|
||||
do_zip_close(:lists.reverse(buffer, t))
|
||||
{:done, acc}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_zip_next_tuple([] = _zips, acc, callback, yielded_elems, buffer) do
|
||||
# "yielded_elems" is a reversed list of results for the current iteration of
|
||||
# zipping: it needs to be reversed and converted to a tuple to have the next
|
||||
# tuple in the list resulting from zipping.
|
||||
zipped = List.to_tuple(:lists.reverse(yielded_elems))
|
||||
defp do_zip([], acc, callback, list, buffer) do
|
||||
zipped = List.to_tuple(:lists.reverse(list))
|
||||
{:next, :lists.reverse(buffer), callback.(zipped, acc)}
|
||||
end
|
||||
|
||||
defp do_zip_close(zips) do
|
||||
:lists.foreach(fn {fun, _} -> fun.({:halt, []}) end, zips)
|
||||
defp do_zip_close([]), do: :ok
|
||||
defp do_zip_close([{fun, acc}|t]) do
|
||||
fun.({:halt, acc})
|
||||
do_zip_close(t)
|
||||
end
|
||||
|
||||
defp do_zip_step(x, []) do
|
||||
{:suspend, [x]}
|
||||
defp do_zip_step(x, acc) do
|
||||
{:suspend, [x|acc]}
|
||||
end
|
||||
|
||||
## Sources
|
||||
@@ -1212,7 +1054,7 @@ defmodule Stream do
|
||||
@doc """
|
||||
Emits a sequence of values for the given resource.
|
||||
|
||||
Similar to `transform/3` but the initial accumulated value is
|
||||
Similar to `transform/2` but the initial accumulated value is
|
||||
computed lazily via `start_fun` and executes an `after_fun` at
|
||||
the end of enumeration (both in cases of success and failure).
|
||||
|
||||
@@ -1237,7 +1079,7 @@ defmodule Stream do
|
||||
fn file -> File.close(file) end)
|
||||
|
||||
"""
|
||||
@spec resource((() -> acc), (acc -> {[element], acc} | {:halt, acc}), (acc -> term)) :: Enumerable.t
|
||||
@spec resource((() -> acc), (acc -> {element, acc} | nil), (acc -> term)) :: Enumerable.t
|
||||
def resource(start_fun, next_fun, after_fun) do
|
||||
&do_resource(start_fun.(), next_fun, &1, &2, after_fun)
|
||||
end
|
||||
@@ -1299,28 +1141,28 @@ defmodule Stream do
|
||||
|
||||
defp do_enum_resource(next_acc, next_fun, {op, acc}, fun, after_fun, reduce) do
|
||||
try do
|
||||
reduce.({op, [:outer | acc]})
|
||||
reduce.({op, [:outer|acc]})
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
after_fun.(next_acc)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
else
|
||||
{:halted, [:outer | acc]} ->
|
||||
{:halted, [:outer|acc]} ->
|
||||
do_resource(next_acc, next_fun, {:cont, acc}, fun, after_fun)
|
||||
{:halted, [:inner | acc]} ->
|
||||
{:halted, [:inner|acc]} ->
|
||||
do_resource(next_acc, next_fun, {:halt, acc}, fun, after_fun)
|
||||
{:done, [_ | acc]} ->
|
||||
{:done, [_|acc]} ->
|
||||
do_resource(next_acc, next_fun, {:cont, acc}, fun, after_fun)
|
||||
{:suspended, [_ | acc], c} ->
|
||||
{:suspended, [_|acc], c} ->
|
||||
{:suspended, acc, &do_enum_resource(next_acc, next_fun, &1, fun, after_fun, c)}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_resource_each(x, [:outer | acc], f) do
|
||||
defp do_resource_each(x, [:outer|acc], f) do
|
||||
case f.(x, acc) do
|
||||
{:halt, res} -> {:halt, [:inner | res]}
|
||||
{op, res} -> {op, [:outer | res]}
|
||||
{:halt, res} -> {:halt, [:inner|res]}
|
||||
{op, res} -> {op, [:outer|res]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1362,17 +1204,17 @@ defmodule Stream do
|
||||
@compile {:inline, lazy: 2, lazy: 3, lazy: 4}
|
||||
|
||||
defp lazy(%Stream{done: nil, funs: funs} = lazy, fun),
|
||||
do: %{lazy | funs: [fun | funs] }
|
||||
do: %{lazy | funs: [fun|funs] }
|
||||
defp lazy(enum, fun),
|
||||
do: %Stream{enum: enum, funs: [fun]}
|
||||
|
||||
defp lazy(%Stream{done: nil, funs: funs, accs: accs} = lazy, acc, fun),
|
||||
do: %{lazy | funs: [fun | funs], accs: [acc | accs] }
|
||||
do: %{lazy | funs: [fun|funs], accs: [acc|accs] }
|
||||
defp lazy(enum, acc, fun),
|
||||
do: %Stream{enum: enum, funs: [fun], accs: [acc]}
|
||||
|
||||
defp lazy(%Stream{done: nil, funs: funs, accs: accs} = lazy, acc, fun, done),
|
||||
do: %{lazy | funs: [fun | funs], accs: [acc | accs], done: done}
|
||||
do: %{lazy | funs: [fun|funs], accs: [acc|accs], done: done}
|
||||
defp lazy(enum, acc, fun, done),
|
||||
do: %Stream{enum: enum, funs: [fun], accs: [acc], done: done}
|
||||
end
|
||||
@@ -1402,8 +1244,8 @@ defimpl Enumerable, for: Stream do
|
||||
end
|
||||
|
||||
defp do_each(reduce, done, accs, {command, acc}) do
|
||||
case reduce.({command, [acc | accs]}) do
|
||||
{:suspended, [acc | accs], continuation} ->
|
||||
case reduce.({command, [acc|accs]}) do
|
||||
{:suspended, [acc|accs], continuation} ->
|
||||
{:suspended, acc, &do_each(continuation, done, accs, &1)}
|
||||
{:halted, accs} ->
|
||||
do_done {:halted, accs}, done
|
||||
@@ -1412,13 +1254,13 @@ defimpl Enumerable, for: Stream do
|
||||
end
|
||||
end
|
||||
|
||||
defp do_done({reason, [acc | _]}, nil), do: {reason, acc}
|
||||
defp do_done({reason, [acc | t]}, {done, fun}) do
|
||||
[h | _] = Enum.reverse(t)
|
||||
defp do_done({reason, [acc|_]}, nil), do: {reason, acc}
|
||||
defp do_done({reason, [acc|t]}, {done, fun}) do
|
||||
[h|_] = Enum.reverse(t)
|
||||
case done.([acc, h], fun) do
|
||||
{:cont, [acc | _]} -> {reason, acc}
|
||||
{:halt, [acc | _]} -> {:halted, acc}
|
||||
{:suspend, [acc | _]} -> {:suspended, acc, &({:done, elem(&1, 1)})}
|
||||
{:cont, [acc|_]} -> {reason, acc}
|
||||
{:halt, [acc|_]} -> {:halted, acc}
|
||||
{:suspend, [acc|_]} -> {:suspended, acc, &({:done, elem(&1, 1)})}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,13 +2,13 @@ defmodule Stream.Reducers do
|
||||
# Collection of reducers shared by Enum and Stream.
|
||||
@moduledoc false
|
||||
|
||||
defmacro chunk(amount, step, limit, fun \\ nil) do
|
||||
defmacro chunk(n, step, limit, f \\ nil) do
|
||||
quote do
|
||||
fn entry, acc(head, {buffer, count}, tail) ->
|
||||
buffer = [entry | buffer]
|
||||
count = count + 1
|
||||
fn entry, acc(h, {buffer, count}, t) ->
|
||||
buffer = [entry|buffer]
|
||||
count = count + 1
|
||||
|
||||
new_state =
|
||||
new =
|
||||
if count >= unquote(limit) do
|
||||
left = count - unquote(step)
|
||||
{Enum.take(buffer, left), left}
|
||||
@@ -16,82 +16,71 @@ defmodule Stream.Reducers do
|
||||
{buffer, count}
|
||||
end
|
||||
|
||||
if count == unquote(amount) do
|
||||
next_with_acc(unquote(fun), :lists.reverse(buffer), head, new_state, tail)
|
||||
if count == unquote(n) do
|
||||
next_with_acc(unquote(f), :lists.reverse(buffer), h, new, t)
|
||||
else
|
||||
skip(acc(head, new_state, tail))
|
||||
skip(acc(h, new, t))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro chunk_by(callback, fun \\ nil) do
|
||||
defmacro chunk_by(callback, f \\ nil) do
|
||||
quote do
|
||||
fn
|
||||
entry, acc(head, {buffer, value}, tail) ->
|
||||
entry, acc(h, {buffer, value}, t) ->
|
||||
new_value = unquote(callback).(entry)
|
||||
if new_value == value do
|
||||
skip(acc(head, {[entry | buffer], value}, tail))
|
||||
skip(acc(h, {[entry|buffer], value}, t))
|
||||
else
|
||||
next_with_acc(unquote(fun), :lists.reverse(buffer), head, {[entry], new_value}, tail)
|
||||
next_with_acc(unquote(f), :lists.reverse(buffer), h, {[entry], new_value}, t)
|
||||
end
|
||||
entry, acc(head, nil, tail) ->
|
||||
skip(acc(head, {[entry], unquote(callback).(entry)}, tail))
|
||||
entry, acc(h, nil, t) ->
|
||||
skip(acc(h, {[entry], unquote(callback).(entry)}, t))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro dedup(callback, fun \\ nil) do
|
||||
defmacro dedup(callback, f \\ nil) do
|
||||
quote do
|
||||
fn(entry, acc(head, prev, tail) = acc) ->
|
||||
fn(entry, acc(h, prev, t) = acc) ->
|
||||
value = unquote(callback).(entry)
|
||||
case prev do
|
||||
{:value, ^value} -> skip(acc)
|
||||
_ -> next_with_acc(unquote(fun), entry, head, {:value, value}, tail)
|
||||
{:value, ^value} -> skip(acc)
|
||||
_ -> next_with_acc(unquote(f), entry, h, {:value, value}, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro drop(fun \\ nil) do
|
||||
defmacro drop(f \\ nil) do
|
||||
quote do
|
||||
fn
|
||||
_entry, acc(head, amount, tail) when amount > 0 ->
|
||||
skip(acc(head, amount - 1, tail))
|
||||
entry, acc(head, amount, tail) ->
|
||||
next_with_acc(unquote(fun), entry, head, amount, tail)
|
||||
_entry, acc(h, n, t) when n > 0 ->
|
||||
skip(acc(h, n-1, t))
|
||||
entry, acc(h, n, t) ->
|
||||
next_with_acc(unquote(f), entry, h, n, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro drop_every(nth, fun \\ nil) do
|
||||
defmacro drop_while(callback, f \\ nil) do
|
||||
quote do
|
||||
fn
|
||||
entry, acc(head, curr, tail) when curr in [unquote(nth), :first] ->
|
||||
skip(acc(head, 1, tail))
|
||||
entry, acc(head, curr, tail) ->
|
||||
next_with_acc(unquote(fun), entry, head, curr + 1, tail)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro drop_while(callback, fun \\ nil) do
|
||||
quote do
|
||||
fn entry, acc(head, bool, tail) = original ->
|
||||
fn entry, acc(h, bool, t) = orig ->
|
||||
if bool and unquote(callback).(entry) do
|
||||
skip(original)
|
||||
skip(orig)
|
||||
else
|
||||
next_with_acc(unquote(fun), entry, head, false, tail)
|
||||
next_with_acc(unquote(f), entry, h, false, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro filter(callback, fun \\ nil) do
|
||||
defmacro filter(callback, f \\ nil) do
|
||||
quote do
|
||||
fn(entry, acc) ->
|
||||
if unquote(callback).(entry) do
|
||||
next(unquote(fun), entry, acc)
|
||||
next(unquote(f), entry, acc)
|
||||
else
|
||||
skip(acc)
|
||||
end
|
||||
@@ -99,11 +88,11 @@ defmodule Stream.Reducers do
|
||||
end
|
||||
end
|
||||
|
||||
defmacro filter_map(filter, mapper, fun \\ nil) do
|
||||
defmacro filter_map(filter, mapper, f \\ nil) do
|
||||
quote do
|
||||
fn(entry, acc) ->
|
||||
if unquote(filter).(entry) do
|
||||
next(unquote(fun), unquote(mapper).(entry), acc)
|
||||
next(unquote(f), unquote(mapper).(entry), acc)
|
||||
else
|
||||
skip(acc)
|
||||
end
|
||||
@@ -111,30 +100,19 @@ defmodule Stream.Reducers do
|
||||
end
|
||||
end
|
||||
|
||||
defmacro map(callback, fun \\ nil) do
|
||||
defmacro map(callback, f \\ nil) do
|
||||
quote do
|
||||
fn(entry, acc) ->
|
||||
next(unquote(fun), unquote(callback).(entry), acc)
|
||||
next(unquote(f), unquote(callback).(entry), acc)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro map_every(nth, mapper, fun \\ nil) do
|
||||
quote do
|
||||
fn
|
||||
entry, acc(head, curr, tail) when curr in [unquote(nth), :first] ->
|
||||
next_with_acc(unquote(fun), unquote(mapper).(entry), head, 1, tail)
|
||||
entry, acc(head, curr, tail) ->
|
||||
next_with_acc(unquote(fun), entry, head, curr + 1, tail)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro reject(callback, fun \\ nil) do
|
||||
defmacro reject(callback, f \\ nil) do
|
||||
quote do
|
||||
fn(entry, acc) ->
|
||||
unless unquote(callback).(entry) do
|
||||
next(unquote(fun), entry, acc)
|
||||
next(unquote(f), entry, acc)
|
||||
else
|
||||
skip(acc)
|
||||
end
|
||||
@@ -142,59 +120,62 @@ defmodule Stream.Reducers do
|
||||
end
|
||||
end
|
||||
|
||||
defmacro scan2(callback, fun \\ nil) do
|
||||
defmacro scan_2(callback, f \\ nil) do
|
||||
quote do
|
||||
fn
|
||||
entry, acc(head, :first, tail) ->
|
||||
next_with_acc(unquote(fun), entry, head, {:ok, entry}, tail)
|
||||
entry, acc(head, {:ok, acc}, tail) ->
|
||||
entry, acc(h, :first, t) ->
|
||||
next_with_acc(unquote(f), entry, h, {:ok, entry}, t)
|
||||
entry, acc(h, {:ok, acc}, t) ->
|
||||
value = unquote(callback).(entry, acc)
|
||||
next_with_acc(unquote(fun), value, head, {:ok, value}, tail)
|
||||
next_with_acc(unquote(f), value, h, {:ok, value}, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro scan3(callback, fun \\ nil) do
|
||||
defmacro scan_3(callback, f \\ nil) do
|
||||
quote do
|
||||
fn(entry, acc(head, acc, tail)) ->
|
||||
fn(entry, acc(h, acc, t)) ->
|
||||
value = unquote(callback).(entry, acc)
|
||||
next_with_acc(unquote(fun), value, head, value, tail)
|
||||
next_with_acc(unquote(f), value, h, value, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro take(fun \\ nil) do
|
||||
defmacro take(f \\ nil) do
|
||||
quote do
|
||||
fn(entry, acc(head, curr, tail) = original) ->
|
||||
case curr do
|
||||
fn(entry, acc(h, n, t) = orig) ->
|
||||
case n do
|
||||
0 ->
|
||||
{:halt, original}
|
||||
{:halt, orig}
|
||||
1 ->
|
||||
{_, acc} = next_with_acc(unquote(fun), entry, head, 0, tail)
|
||||
{:halt, acc}
|
||||
case next_with_acc(unquote(f), entry, h, n-1, t) do
|
||||
{:cont, acc} -> {:halt, acc}
|
||||
reason -> reason
|
||||
end
|
||||
_ ->
|
||||
next_with_acc(unquote(fun), entry, head, curr - 1, tail)
|
||||
next_with_acc(unquote(f), entry, h, n-1, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro take_every(nth, fun \\ nil) do
|
||||
defmacro take_every(nth, f \\ nil) do
|
||||
quote do
|
||||
fn
|
||||
entry, acc(head, curr, tail) when curr in [unquote(nth), :first] ->
|
||||
next_with_acc(unquote(fun), entry, head, 1, tail)
|
||||
entry, acc(head, curr, tail) ->
|
||||
skip(acc(head, curr + 1, tail))
|
||||
entry, acc(h, n, t) when n === :first
|
||||
when n === unquote(nth) ->
|
||||
next_with_acc(unquote(f), entry, h, 1, t)
|
||||
entry, acc(h, n, t) ->
|
||||
skip(acc(h, n+1, t))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro take_while(callback, fun \\ nil) do
|
||||
defmacro take_while(callback, f \\ nil) do
|
||||
quote do
|
||||
fn(entry, acc) ->
|
||||
if unquote(callback).(entry) do
|
||||
next(unquote(fun), entry, acc)
|
||||
next(unquote(f), entry, acc)
|
||||
else
|
||||
{:halt, acc}
|
||||
end
|
||||
@@ -202,23 +183,23 @@ defmodule Stream.Reducers do
|
||||
end
|
||||
end
|
||||
|
||||
defmacro uniq_by(callback, fun \\ nil) do
|
||||
defmacro uniq(callback, f \\ nil) do
|
||||
quote do
|
||||
fn(entry, acc(head, prev, tail) = original) ->
|
||||
fn(entry, acc(h, prev, t) = acc) ->
|
||||
value = unquote(callback).(entry)
|
||||
if Map.has_key?(prev, value) do
|
||||
skip(original)
|
||||
skip(acc)
|
||||
else
|
||||
next_with_acc(unquote(fun), entry, head, Map.put(prev, value, true), tail)
|
||||
next_with_acc(unquote(f), entry, h, Map.put(prev, value, true), t)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro with_index(fun \\ nil) do
|
||||
defmacro with_index(f \\ nil) do
|
||||
quote do
|
||||
fn(entry, acc(head, counter, tail)) ->
|
||||
next_with_acc(unquote(fun), {entry, counter}, head, counter + 1, tail)
|
||||
fn(entry, acc(h, counter, t)) ->
|
||||
next_with_acc(unquote(f), {entry, counter}, h, counter + 1, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+271
-463
File diff suppressed because it is too large
Load Diff
@@ -3,18 +3,18 @@ import Kernel, except: [to_string: 1]
|
||||
defprotocol String.Chars do
|
||||
@moduledoc ~S"""
|
||||
The `String.Chars` protocol is responsible for
|
||||
converting a structure to a binary (only if applicable).
|
||||
converting a structure to a Binary (only if applicable).
|
||||
The only function required to be implemented is
|
||||
`to_string` which does the conversion.
|
||||
|
||||
The `to_string/1` function automatically imported
|
||||
by `Kernel` invokes this protocol. String
|
||||
The `to_string` function automatically imported
|
||||
by Kernel invokes this protocol. String
|
||||
interpolation also invokes `to_string` in its
|
||||
arguments. For example, `"foo#{bar}"` is the same
|
||||
as `"foo" <> to_string(bar)`.
|
||||
"""
|
||||
|
||||
def to_string(term)
|
||||
def to_string(thing)
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: Atom do
|
||||
@@ -28,30 +28,30 @@ defimpl String.Chars, for: Atom do
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: BitString do
|
||||
def to_string(term) when is_binary(term) do
|
||||
term
|
||||
def to_string(thing) when is_binary(thing) do
|
||||
thing
|
||||
end
|
||||
|
||||
def to_string(term) do
|
||||
def to_string(thing) do
|
||||
raise Protocol.UndefinedError,
|
||||
protocol: @protocol,
|
||||
value: term,
|
||||
value: thing,
|
||||
description: "cannot convert a bitstring to a string"
|
||||
end
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: List do
|
||||
def to_string(charlist), do: List.to_string(charlist)
|
||||
def to_string(char_list), do: List.to_string(char_list)
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: Integer do
|
||||
def to_string(term) do
|
||||
Integer.to_string(term)
|
||||
def to_string(thing) do
|
||||
Integer.to_string(thing)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: Float do
|
||||
def to_string(term) do
|
||||
IO.iodata_to_binary(:io_lib_format.fwrite_g(term))
|
||||
def to_string(thing) do
|
||||
IO.iodata_to_binary(:io_lib_format.fwrite_g(thing))
|
||||
end
|
||||
end
|
||||
|
||||
+35
-61
@@ -1,9 +1,6 @@
|
||||
defmodule StringIO do
|
||||
@moduledoc """
|
||||
Controls an IO device process that wraps a string.
|
||||
|
||||
A `StringIO` IO device can be passed as a "device" to
|
||||
most of the functions in the `IO` module.
|
||||
This module provides an IO device that wraps a string.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -18,9 +15,6 @@ defmodule StringIO do
|
||||
@doc """
|
||||
Creates an IO device.
|
||||
|
||||
`string` will be the initial input of the newly created
|
||||
device.
|
||||
|
||||
If the `:capture_prompt` option is set to `true`,
|
||||
prompts (specified as arguments to `IO.get*` functions)
|
||||
are captured.
|
||||
@@ -46,8 +40,7 @@ defmodule StringIO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current input/output buffers for the given IO
|
||||
device.
|
||||
Returns current buffers.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -63,7 +56,7 @@ defmodule StringIO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Flushes the output buffer and returns its current contents.
|
||||
Flushes output buffer.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -81,8 +74,7 @@ defmodule StringIO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the IO device and returns the remaining input/output
|
||||
buffers.
|
||||
Stops the IO device and returns remaining buffers.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -135,20 +127,21 @@ defmodule StringIO do
|
||||
s
|
||||
end
|
||||
|
||||
defp io_request({:put_chars, chars} = req, s) do
|
||||
put_chars(:latin1, chars, req, s)
|
||||
defp io_request({:put_chars, chars}, %{output: output} = s) do
|
||||
{:ok, %{s | output: <<output::binary, IO.chardata_to_string(chars)::binary>>}}
|
||||
end
|
||||
|
||||
defp io_request({:put_chars, m, f, as} = req, s) do
|
||||
put_chars(:latin1, apply(m, f, as), req, s)
|
||||
defp io_request({:put_chars, m, f, as}, %{output: output} = s) do
|
||||
chars = apply(m, f, as)
|
||||
{:ok, %{s | output: <<output::binary, IO.chardata_to_string(chars)::binary>>}}
|
||||
end
|
||||
|
||||
defp io_request({:put_chars, encoding, chars} = req, s) do
|
||||
put_chars(encoding, chars, req, s)
|
||||
defp io_request({:put_chars, _encoding, chars}, s) do
|
||||
io_request({:put_chars, chars}, s)
|
||||
end
|
||||
|
||||
defp io_request({:put_chars, encoding, mod, func, args} = req, s) do
|
||||
put_chars(encoding, apply(mod, func, args), req, s)
|
||||
defp io_request({:put_chars, _encoding, mod, func, args}, s) do
|
||||
io_request({:put_chars, mod, func, args}, s)
|
||||
end
|
||||
|
||||
defp io_request({:get_chars, prompt, n}, s) when n >= 0 do
|
||||
@@ -203,17 +196,6 @@ defmodule StringIO do
|
||||
{{:error, :request}, s}
|
||||
end
|
||||
|
||||
## put_chars
|
||||
|
||||
defp put_chars(encoding, chars, req, %{output: output} = s) do
|
||||
case :unicode.characters_to_binary(chars, encoding, :unicode) do
|
||||
string when is_binary(string) ->
|
||||
{:ok, %{s | output: output <> string}}
|
||||
{_, _, _} ->
|
||||
{{:error, req}, s}
|
||||
end
|
||||
end
|
||||
|
||||
## get_chars
|
||||
|
||||
defp get_chars(encoding, prompt, n,
|
||||
@@ -222,14 +204,11 @@ defmodule StringIO do
|
||||
{:error, _} = error ->
|
||||
{error, s}
|
||||
{result, input} ->
|
||||
s =
|
||||
if capture_prompt do
|
||||
%{s | output: <<output::binary, IO.chardata_to_string(prompt)::binary>>}
|
||||
else
|
||||
s
|
||||
end
|
||||
if capture_prompt do
|
||||
output = <<output::binary, IO.chardata_to_string(prompt)::binary>>
|
||||
end
|
||||
|
||||
{result, %{s | input: input}}
|
||||
{result, %{s | input: input, output: output}}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -273,14 +252,11 @@ defmodule StringIO do
|
||||
chars ->
|
||||
{result, input} = do_get_line(chars, encoding)
|
||||
|
||||
s =
|
||||
if capture_prompt do
|
||||
%{s | output: <<output::binary, IO.chardata_to_string(prompt)::binary>>}
|
||||
else
|
||||
s
|
||||
end
|
||||
if capture_prompt do
|
||||
output = <<output::binary, IO.chardata_to_string(prompt)::binary>>
|
||||
end
|
||||
|
||||
{result, %{s | input: input}}
|
||||
{result, %{s | input: input, output: output}}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -306,20 +282,17 @@ defmodule StringIO do
|
||||
chars ->
|
||||
{result, input, count} = do_get_until(chars, encoding, mod, fun, args)
|
||||
|
||||
if capture_prompt do
|
||||
output = <<output::binary, :binary.copy(IO.chardata_to_string(prompt), count)::binary>>
|
||||
end
|
||||
|
||||
input =
|
||||
case input do
|
||||
:eof -> ""
|
||||
_ -> :unicode.characters_to_binary(input, encoding)
|
||||
end
|
||||
|
||||
s =
|
||||
if capture_prompt do
|
||||
%{s | output: <<output::binary, :binary.copy(IO.chardata_to_string(prompt), count)::binary>>}
|
||||
else
|
||||
s
|
||||
end
|
||||
|
||||
{result, %{s | input: input}}
|
||||
{result, %{s | input: input, output: output}}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -338,10 +311,11 @@ defmodule StringIO do
|
||||
{line, rest} = collect_line(chars)
|
||||
|
||||
case apply(mod, fun, [continuation, line | args]) do
|
||||
{:done, result, :eof} ->
|
||||
{:done, result, rest1} ->
|
||||
unless rest1 == :eof do
|
||||
rest = rest1 ++ rest
|
||||
end
|
||||
{result, rest, count + 1}
|
||||
{:done, result, extra} ->
|
||||
{result, extra ++ rest, count + 1}
|
||||
{:more, next_continuation} ->
|
||||
do_get_until(rest, encoding, mod, fun, args, next_continuation, count + 1)
|
||||
end
|
||||
@@ -349,7 +323,7 @@ defmodule StringIO do
|
||||
|
||||
## io_requests
|
||||
|
||||
defp io_requests([r | rs], {:ok, s}) do
|
||||
defp io_requests([r|rs], {:ok, s}) do
|
||||
io_requests(rs, io_request(r, s))
|
||||
end
|
||||
|
||||
@@ -368,15 +342,15 @@ defmodule StringIO do
|
||||
end
|
||||
|
||||
defp collect_line([?\r, ?\n | rest], stack) do
|
||||
{:lists.reverse([?\n | stack]), rest}
|
||||
{:lists.reverse([?\n|stack]), rest}
|
||||
end
|
||||
|
||||
defp collect_line([?\n | rest], stack) do
|
||||
{:lists.reverse([?\n | stack]), rest}
|
||||
{:lists.reverse([?\n|stack]), rest}
|
||||
end
|
||||
|
||||
defp collect_line([h | t], stack) do
|
||||
collect_line(t, [h | stack])
|
||||
defp collect_line([h|t], stack) do
|
||||
collect_line(t, [h|stack])
|
||||
end
|
||||
|
||||
defp io_reply(from, reply_as, reply) do
|
||||
|
||||
+135
-175
@@ -2,16 +2,16 @@ defmodule Supervisor do
|
||||
@moduledoc ~S"""
|
||||
A behaviour module for implementing supervision functionality.
|
||||
|
||||
A supervisor is a process which supervises other processes, which we refer
|
||||
to as *child processes*. Supervisors are used to build a hierarchical process
|
||||
structure called a *supervision tree*. Supervision trees are a nice way to
|
||||
structure fault-tolerant applications.
|
||||
A supervisor is a process which supervises other processes, called
|
||||
child processes. Supervisors are used to build a hierarchical process
|
||||
structure called a supervision tree, a nice way to structure fault-tolerant
|
||||
applications.
|
||||
|
||||
A supervisor implemented using this module has a standard set
|
||||
of interface functions and includes functionality for tracing and error
|
||||
reporting. It also fits into a supervision tree.
|
||||
A supervisor implemented using this module will have a standard set
|
||||
of interface functions and include functionality for tracing and error
|
||||
reporting. It will also fit into a supervision tree.
|
||||
|
||||
## Examples
|
||||
## Example
|
||||
|
||||
In order to define a supervisor, we need to first define a child process
|
||||
that is going to be supervised. In order to do so, we will define a GenServer
|
||||
@@ -24,12 +24,12 @@ defmodule Supervisor do
|
||||
GenServer.start_link(__MODULE__, state, opts)
|
||||
end
|
||||
|
||||
def handle_call(:pop, _from, [h | t]) do
|
||||
def handle_call(:pop, _from, [h|t]) do
|
||||
{:reply, h, t}
|
||||
end
|
||||
|
||||
def handle_cast({:push, h}, t) do
|
||||
{:noreply, [h | t]}
|
||||
{:noreply, [h|t]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -38,64 +38,58 @@ defmodule Supervisor do
|
||||
# Import helpers for defining supervisors
|
||||
import Supervisor.Spec
|
||||
|
||||
# Supervise the Stack server which will be started with
|
||||
# two arguments. The initial stack, [:hello], and a
|
||||
# keyword list containing the GenServer options that
|
||||
# set the registered name of the server to MyStack.
|
||||
# We are going to supervise the Stack server which
|
||||
# will be started with a single argument [:hello]
|
||||
# and the default name of :sup_stack.
|
||||
children = [
|
||||
worker(Stack, [[:hello], [name: MyStack]])
|
||||
worker(Stack, [[:hello], [name: :sup_stack]])
|
||||
]
|
||||
|
||||
# Start the supervisor with our child
|
||||
# Start the supervisor with our one child
|
||||
{:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one)
|
||||
|
||||
# There is one child worker started
|
||||
Supervisor.count_children(pid)
|
||||
#=> %{active: 1, specs: 1, supervisors: 0, workers: 1}
|
||||
|
||||
Notice that when starting the GenServer, we are registering it
|
||||
with name `MyStack`, which allows us to call it directly and
|
||||
with name `:sup_stack`, which allows us to call it directly and
|
||||
get what is on the stack:
|
||||
|
||||
GenServer.call(MyStack, :pop)
|
||||
GenServer.call(:sup_stack, :pop)
|
||||
#=> :hello
|
||||
|
||||
GenServer.cast(MyStack, {:push, :world})
|
||||
GenServer.cast(:sup_stack, {:push, :world})
|
||||
#=> :ok
|
||||
|
||||
GenServer.call(MyStack, :pop)
|
||||
GenServer.call(:sup_stack, :pop)
|
||||
#=> :world
|
||||
|
||||
However, there is a bug in our stack server. If we call `:pop` and
|
||||
the stack is empty, it is going to crash because no clause matches:
|
||||
the stack is empty, it is going to crash because no clause matches.
|
||||
Let's try it:
|
||||
|
||||
GenServer.call(MyStack, :pop)
|
||||
** (exit) exited in: GenServer.call(MyStack, :pop, 5000)
|
||||
GenServer.call(:sup_stack, :pop)
|
||||
** (exit) exited in: GenServer.call(:sup_stack, :pop, 5000)
|
||||
|
||||
Luckily, since the server is being supervised by a supervisor, the
|
||||
supervisor will automatically start a new one, with the initial stack
|
||||
of `[:hello]`:
|
||||
supervisor will automatically start a new one, with the default stack
|
||||
of `[:hello]` like before:
|
||||
|
||||
GenServer.call(MyStack, :pop)
|
||||
#=> :hello
|
||||
GenServer.call(:sup_stack, :pop) == :hello
|
||||
|
||||
Supervisors support different strategies; in the example above, we
|
||||
have chosen `:one_for_one`. Furthermore, each supervisor can have many
|
||||
workers and supervisors as children, each of them with their specific
|
||||
configuration, shutdown values, and restart strategies.
|
||||
|
||||
The rest of this documentation will cover supervision strategies; also read
|
||||
the documentation for the `Supervisor.Spec` module to learn about the
|
||||
specification for workers and supervisors.
|
||||
Continue reading this moduledoc to learn more about supervision strategies
|
||||
and then proceed to the `Supervisor.Spec` module documentation to learn
|
||||
about the specification for workers and supervisors.
|
||||
|
||||
## Module-based supervisors
|
||||
|
||||
In the example above, a supervisor was started by passing the supervision
|
||||
structure to `start_link/2`. However, supervisors can also be created by
|
||||
explicitly defining a supervision module:
|
||||
In the example above, a supervisor was dynamically created by passing
|
||||
the supervision structure to `start_link/2`. However, supervisors
|
||||
can also be created by explicitly defining a supervision module:
|
||||
|
||||
defmodule MyApp.Supervisor do
|
||||
# Automatically imports Supervisor.Spec
|
||||
use Supervisor
|
||||
|
||||
def start_link do
|
||||
@@ -107,7 +101,6 @@ defmodule Supervisor do
|
||||
worker(Stack, [[:hello]])
|
||||
]
|
||||
|
||||
# supervise/2 is imported from Supervisor.Spec
|
||||
supervise(children, strategy: :one_for_one)
|
||||
end
|
||||
end
|
||||
@@ -126,9 +119,6 @@ defmodule Supervisor do
|
||||
|
||||
## Strategies
|
||||
|
||||
Supervisors support different supervision strategies (through the `:strategy`
|
||||
option, as seen above):
|
||||
|
||||
* `:one_for_one` - if a child process terminates, only that
|
||||
process is restarted.
|
||||
|
||||
@@ -137,7 +127,7 @@ defmodule Supervisor do
|
||||
the terminated one) are restarted.
|
||||
|
||||
* `:rest_for_one` - if a child process terminates, the "rest" of
|
||||
the child processes, i.e., the child processes after the terminated
|
||||
the child processes, i.e. the child processes after the terminated
|
||||
one in start order, are terminated. Then the terminated child
|
||||
process and the rest of the child processes are restarted.
|
||||
|
||||
@@ -149,10 +139,10 @@ defmodule Supervisor do
|
||||
|
||||
## Simple one for one
|
||||
|
||||
The `:simple_one_for_one` supervisor is useful when you want to dynamically
|
||||
start and stop supervised children. For example, imagine you want to
|
||||
dynamically create multiple stacks. We can do so by defining a `:simple_one_for_one`
|
||||
supervisor:
|
||||
The simple one for one supervisor is useful when you want to dynamically
|
||||
start and stop supervisor children. For example, imagine you want to
|
||||
dynamically create multiple stacks. We can do so by defining a simple one
|
||||
for one supervisor:
|
||||
|
||||
# Import helpers for defining supervisors
|
||||
import Supervisor.Spec
|
||||
@@ -163,25 +153,20 @@ defmodule Supervisor do
|
||||
worker(Stack, [], restart: :transient)
|
||||
]
|
||||
|
||||
# Start the supervisor with our one child as a template
|
||||
# Start the supervisor with our one child
|
||||
{:ok, sup_pid} = Supervisor.start_link(children, strategy: :simple_one_for_one)
|
||||
|
||||
# No child worker is active yet until start_child is called
|
||||
Supervisor.count_children(sup_pid)
|
||||
#=> %{active: 0, specs: 1, supervisors: 0, workers: 0}
|
||||
|
||||
There are a couple differences here:
|
||||
|
||||
* the simple one for one specification can define only one child which
|
||||
* The simple one for one specification can define only one child which
|
||||
works as a template for when we call `start_child/2`
|
||||
|
||||
* we have defined the child to have a restart strategy of `:transient`. This
|
||||
means that, if the child process exits due to a `:normal`, `:shutdown`,
|
||||
* We have defined the child to have a restart strategy of transient. This
|
||||
means that, if the child process exits due to a `:normal`, `:shutdown`
|
||||
or `{:shutdown, term}` reason, it won't be restarted. This is useful
|
||||
as it allows our workers to politely shutdown and be removed from the
|
||||
`:simple_one_for_one` supervisor, without being restarted. You can find
|
||||
more information about restart strategies in the documentation for the
|
||||
`Supervisor.Spec` module
|
||||
simple one for one supervisor, without being restarted. You can find
|
||||
more information about restart strategies on `Supervisor.Spec`
|
||||
|
||||
With the supervisor defined, let's dynamically start stacks:
|
||||
|
||||
@@ -198,51 +183,38 @@ defmodule Supervisor do
|
||||
|
||||
## Exit reasons
|
||||
|
||||
From the example above, you may have noticed that the `:transient` restart
|
||||
strategy for the worker does not restart the child in case it exits with
|
||||
From the example above, you may have noticed that the transient restart
|
||||
strategy for the worker does not restart the child in case it crashes with
|
||||
reason `:normal`, `:shutdown` or `{:shutdown, term}`.
|
||||
|
||||
So one may ask: which exit reason should I choose when exiting my worker?
|
||||
There are three options:
|
||||
|
||||
* `:normal` - in such cases, the exit won't be logged, there is no restart
|
||||
in transient mode, and linked processes do not exit
|
||||
in transient mode and linked processes do not exit
|
||||
|
||||
* `:shutdown` or `{:shutdown, term}` - in such cases, the exit won't be
|
||||
logged, there is no restart in transient mode, and linked processes exit
|
||||
with the same reason unless they're trapping exits
|
||||
logged, there is no restart in transient mode and linked processes exit
|
||||
with the same reason unless trapping exits
|
||||
|
||||
* any other term - in such cases, the exit will be logged, there are
|
||||
restarts in transient mode, and linked processes exit with the same reason
|
||||
unless they're trapping exits
|
||||
restarts in transient mode and linked processes exit with the same reason
|
||||
unless trapping exits
|
||||
|
||||
## Name registration
|
||||
## Name Registration
|
||||
|
||||
A supervisor is bound to the same name registration rules as a `GenServer`.
|
||||
Read more about these rules in the documentation for `GenServer`.
|
||||
|
||||
Read more about it in the `GenServer` docs.
|
||||
"""
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote location: :keep do
|
||||
@behaviour Supervisor
|
||||
@behaviour :supervisor
|
||||
import Supervisor.Spec
|
||||
|
||||
@doc false
|
||||
def init(arg)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Callback invoked to start the supervisor and during hot code upgrades.
|
||||
"""
|
||||
# TODO: Support {:ok, [child_spec], Keyword.t}
|
||||
# TODO: Document options here and update Supervisor.Spec
|
||||
@callback init(args :: term) ::
|
||||
{:ok, {:supervisor.sup_flags, [Supervisor.Spec.spec]}} |
|
||||
:ignore
|
||||
|
||||
@typedoc "Return values of `start_link` functions"
|
||||
@type on_start :: {:ok, pid} | :ignore |
|
||||
{:error, {:already_started, pid} | {:shutdown, term} | term}
|
||||
@@ -268,29 +240,30 @@ defmodule Supervisor do
|
||||
@doc """
|
||||
Starts a supervisor with the given children.
|
||||
|
||||
A strategy is required to be provided through the `:strategy` option.
|
||||
Furthermore, the `:max_restarts` and `:max_seconds` options can be
|
||||
configured as described in the documentation for `Supervisor.Spec.supervise/2`.
|
||||
A strategy is required to be given as an option. Furthermore,
|
||||
the `:max_restarts` and `:max_seconds` value can be configured
|
||||
as described in `Supervisor.Spec.supervise/2` docs.
|
||||
|
||||
The options can also be used to register a supervisor name.
|
||||
The supported values are described under the "Name registration"
|
||||
The supported values are described under the `Name Registration`
|
||||
section in the `GenServer` module docs.
|
||||
|
||||
If the supervisor and its child processes are successfully created
|
||||
(i.e., if the start function of each child process returns `{:ok, child}`,
|
||||
`{:ok, child, info}`, or `:ignore`) this function returns
|
||||
`{:ok, pid}`, where `pid` is the PID of the supervisor. If a process with the
|
||||
specified name already exists, the function returns `{:error,
|
||||
{:already_started, pid}}`, where `pid` is the PID of that process.
|
||||
(i.e. if the start function of all child processes returns `{:ok, child}`,
|
||||
`{:ok, child, info}`, or `:ignore`) the function returns
|
||||
`{:ok, pid}`, where `pid` is the pid of the supervisor. If there
|
||||
already exists a process with the specified name, the function returns
|
||||
`{:error, {:already_started, pid}}`, where pid is the pid of that
|
||||
process.
|
||||
|
||||
If the start function of any of the child processes fails or returns an error
|
||||
tuple or an erroneous value, the supervisor first terminates with reason
|
||||
`:shutdown` all the child processes that have already been started, and then
|
||||
terminates itself and returns `{:error, {:shutdown, reason}}`.
|
||||
If any of the child process start functions fail or return an error tuple or
|
||||
an erroneous value, the supervisor will first terminate all already
|
||||
started child processes with reason `:shutdown` and then terminate
|
||||
itself and return `{:error, {:shutdown, reason}}`.
|
||||
|
||||
Note that a supervisor started with this function is linked to the parent
|
||||
process and exits not only on crashes but also if the parent process exits
|
||||
with `:normal` reason.
|
||||
Note that the `Supervisor` is linked to the parent process
|
||||
and will exit not only on crashes but also if the parent process
|
||||
exits with `:normal` reason.
|
||||
"""
|
||||
@spec start_link([Supervisor.Spec.spec], options) :: on_start
|
||||
def start_link(children, options) when is_list(children) do
|
||||
@@ -299,22 +272,24 @@ defmodule Supervisor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a supervisor process with the given `module` and `arg`.
|
||||
Starts a supervisor module with the given `arg`.
|
||||
|
||||
To start the supervisor, the `c:init/1` callback will be invoked in the given
|
||||
`module`, with `arg` as its argument. The `c:init/1` callback must return a
|
||||
supervisor specification which can be created with the help of the functions
|
||||
in the `Supervisor.Spec` module (especially `Supervisor.Spec.supervise/2`).
|
||||
To start the supervisor, the `init/1` callback will be invoked
|
||||
in the given module. The `init/1` callback must return a
|
||||
supervision specification which can be created with the help
|
||||
of `Supervisor.Spec` module.
|
||||
|
||||
If the `c:init/1` callback returns `:ignore`, this function returns
|
||||
If the `init/1` callback returns `:ignore`, this function returns
|
||||
`:ignore` as well and the supervisor terminates with reason `:normal`.
|
||||
If it fails or returns an incorrect value, this function returns
|
||||
`{:error, term}` where `term` is a term with information about the
|
||||
error, and the supervisor terminates with reason `term`.
|
||||
|
||||
The `:name` option can also be given in order to register a supervisor
|
||||
name, the supported values are described in the "Name registration"
|
||||
name, the supported values are described under the `Name Registration`
|
||||
section in the `GenServer` module docs.
|
||||
|
||||
Other failure conditions are specified in `start_link/2` docs.
|
||||
"""
|
||||
@spec start_link(module, term) :: on_start
|
||||
@spec start_link(module, term, options) :: on_start
|
||||
@@ -324,53 +299,39 @@ defmodule Supervisor do
|
||||
:supervisor.start_link(module, arg)
|
||||
atom when is_atom(atom) ->
|
||||
:supervisor.start_link({:local, atom}, module, arg)
|
||||
{:global, _term} = tuple ->
|
||||
:supervisor.start_link(tuple, module, arg)
|
||||
{:via, via_module, _term} = tuple when is_atom(via_module) ->
|
||||
:supervisor.start_link(tuple, module, arg)
|
||||
other ->
|
||||
raise ArgumentError, """
|
||||
expected :name option to be one of:
|
||||
|
||||
* nil
|
||||
* atom
|
||||
* {:global, term}
|
||||
* {:via, module, term}
|
||||
|
||||
Got: #{inspect(other)}
|
||||
"""
|
||||
other when is_tuple(other) ->
|
||||
:supervisor.start_link(other, module, arg)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Dynamically adds a child specification to `supervisor` and starts that child.
|
||||
Dynamically adds and starts a child specification to the supervisor.
|
||||
|
||||
`child_spec` should be a valid child specification (unless the supervisor
|
||||
is a `:simple_one_for_one` supervisor, see below). The child process will
|
||||
be started as defined in the child specification.
|
||||
|
||||
In the case of `:simple_one_for_one`, the child specification defined in
|
||||
the supervisor is used and instead of a `child_spec`, an arbitrary list
|
||||
the supervisor will be used and instead of a `child_spec`, an arbitrary list
|
||||
of terms is expected. The child process will then be started by appending
|
||||
the given list to the existing function arguments in the child specification.
|
||||
|
||||
If a child specification with the specified id already exists, `child_spec` is
|
||||
discarded and this function returns an error with `:already_started` or
|
||||
`:already_present` if the corresponding child process is running or not,
|
||||
respectively.
|
||||
If a child specification with the specified id already exists,
|
||||
`child_spec` is discarded and the function returns an error with `:already_started`
|
||||
or `:already_present` if the corresponding child process is running or not.
|
||||
|
||||
If the child process start function returns `{:ok, child}` or `{:ok, child,
|
||||
info}`, then child specification and PID are added to the supervisor and
|
||||
this function returns the same value.
|
||||
If the child process starts, function returns `{:ok, child}` or `{:ok, child, info}`,
|
||||
the child specification and pid is added to the supervisor and the function returns
|
||||
the same value.
|
||||
|
||||
If the child process start function returns `:ignore`, the child specification
|
||||
is added to the supervisor, the PID is set to `:undefined` and this function
|
||||
returns `{:ok, :undefined}`.
|
||||
If the child process starts, function returns `:ignore`, the child specification is
|
||||
added to the supervisor, the pid is set to undefined and the function returns
|
||||
`{:ok, :undefined}`.
|
||||
|
||||
If the child process start function returns an error tuple or an erroneous
|
||||
value, or if it fails, the child specification is discarded and this function
|
||||
returns `{:error, error}` where `error` is a term containing information about
|
||||
the error and child specification.
|
||||
If the child process starts, function returns an error tuple or an erroneous value,
|
||||
or if it fails, the child specification is discarded and the function returns
|
||||
`{:error, error}` where `error` is a term containing information about the error
|
||||
and child specification.
|
||||
"""
|
||||
@spec start_child(supervisor, Supervisor.Spec.spec | [term]) :: on_start_child
|
||||
def start_child(supervisor, child_spec_or_args) do
|
||||
@@ -378,23 +339,22 @@ defmodule Supervisor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Terminates the given children, identified by PID or child id.
|
||||
Terminates the given pid or child id.
|
||||
|
||||
If the supervisor is not a `:simple_one_for_one`, the child id is expected
|
||||
and the process, if there's one, is terminated; the child specification is
|
||||
If the supervisor is not a `simple_one_for_one`, the child id is expected
|
||||
and the process, if there is one, is terminated; the child specification is
|
||||
kept unless the child is temporary.
|
||||
|
||||
In case of a `:simple_one_for_one` supervisor, a PID is expected. If the child
|
||||
specification identifier is given instead of a `pid`, this function returns
|
||||
`{:error, :simple_one_for_one}`.
|
||||
In case of a `simple_one_for_one` supervisor, a pid is expected. If the child
|
||||
specification identifier is given instead of a `pid`, the function will
|
||||
return `{:error, :simple_one_for_one}`.
|
||||
|
||||
A non-temporary child process may later be restarted by the supervisor. The child
|
||||
process can also be restarted explicitly by calling `restart_child/2`. Use
|
||||
`delete_child/2` to remove the child specification.
|
||||
|
||||
If successful, this function returns `:ok`. If there is no child specification
|
||||
for the given child id or there is no process with the given PID, this
|
||||
function returns `{:error, :not_found}`.
|
||||
If successful, the function returns `:ok`. If there is no child specification or
|
||||
pid, the function returns `{:error, :not_found}`.
|
||||
"""
|
||||
@spec terminate_child(supervisor, pid | Supervisor.Spec.child_id) :: :ok | {:error, error}
|
||||
when error: :not_found | :simple_one_for_one
|
||||
@@ -405,14 +365,14 @@ defmodule Supervisor do
|
||||
@doc """
|
||||
Deletes the child specification identified by `child_id`.
|
||||
|
||||
The corresponding child process must not be running; use `terminate_child/2`
|
||||
to terminate it if it's running.
|
||||
The corresponding child process must not be running, use `terminate_child/2`
|
||||
to terminate it.
|
||||
|
||||
If successful, this function returns `:ok`. This function may return an error
|
||||
with an appropriate error tuple if the `child_id` is not found, or if the
|
||||
current process is running or being restarted.
|
||||
If successful, the function returns `:ok`. This function may error with an
|
||||
appropriate error tuple if the `child_id` is not found, or if the current
|
||||
process is running or being restarted.
|
||||
|
||||
This operation is not supported by `:simple_one_for_one` supervisors.
|
||||
This operation is not supported by `simple_one_for_one` supervisors.
|
||||
"""
|
||||
@spec delete_child(supervisor, Supervisor.Spec.child_id) :: :ok | {:error, error}
|
||||
when error: :not_found | :simple_one_for_one | :running | :restarting
|
||||
@@ -429,20 +389,20 @@ defmodule Supervisor do
|
||||
Note that for temporary children, the child specification is automatically deleted
|
||||
when the child terminates, and thus it is not possible to restart such children.
|
||||
|
||||
If the child process start function returns `{:ok, child}` or `{:ok, child, info}`,
|
||||
the PID is added to the supervisor and this function returns the same value.
|
||||
If the child process start function returns `{:ok, child}` or
|
||||
`{:ok, child, info}`, the pid is added to the supervisor and the function returns
|
||||
the same value.
|
||||
|
||||
If the child process start function returns `:ignore`, the PID remains set to
|
||||
`:undefined` and this function returns `{:ok, :undefined}`.
|
||||
If the child process start function returns `:ignore`, the pid remains set to
|
||||
`:undefined` and the function returns `{:ok, :undefined}`.
|
||||
|
||||
This function may return an error with an appropriate error tuple if the
|
||||
`child_id` is not found, or if the current process is running or being
|
||||
restarted.
|
||||
This function may error with an appropriate error tuple if the `child_id` is not
|
||||
found, or if the current process is running or being restarted.
|
||||
|
||||
If the child process start function returns an error tuple or an erroneous value,
|
||||
or if it fails, this function returns `{:error, error}`.
|
||||
or if it fails, the function returns `{:error, error}`.
|
||||
|
||||
This operation is not supported by `:simple_one_for_one` supervisors.
|
||||
This operation is not supported by `simple_one_for_one` supervisors.
|
||||
"""
|
||||
@spec restart_child(supervisor, Supervisor.Spec.child_id) ::
|
||||
{:ok, child} | {:ok, child, term} | {:error, error}
|
||||
@@ -452,24 +412,23 @@ defmodule Supervisor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list with information about all children of the given supervisor.
|
||||
Returns a list with information about all children.
|
||||
|
||||
Note that calling this function when supervising a large number of children
|
||||
under low memory conditions can cause an out of memory exception.
|
||||
|
||||
This function returns a list of `{id, child, type, modules}` tuples, where:
|
||||
This function returns a list of tuples containing:
|
||||
|
||||
* `id` - as defined in the child specification or `:undefined` in the case
|
||||
of a `simple_one_for_one` supervisor
|
||||
|
||||
* `child` - the PID of the corresponding child process, `:restarting` if the
|
||||
process is about to be restarted, or `:undefined` if there is no such
|
||||
process
|
||||
* `child` - the pid of the corresponding child process, the atom
|
||||
`:restarting` if the process is about to be restarted, or `:undefined` if
|
||||
there is no such process
|
||||
|
||||
* `type` - `:worker` or `:supervisor`, as specified by the child specification
|
||||
|
||||
* `modules` - as specified by the child specification
|
||||
* `type` - `:worker` or `:supervisor` as defined in the child specification
|
||||
|
||||
* `modules` - as defined in the child specification
|
||||
"""
|
||||
@spec which_children(supervisor) ::
|
||||
[{Supervisor.Spec.child_id | :undefined,
|
||||
@@ -481,7 +440,7 @@ defmodule Supervisor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a map containing count values for the given supervisor.
|
||||
Returns a map containing count values for the supervisor.
|
||||
|
||||
The map contains the following keys:
|
||||
|
||||
@@ -490,11 +449,11 @@ defmodule Supervisor do
|
||||
* `:active` - the count of all actively running child processes managed by
|
||||
this supervisor
|
||||
|
||||
* `:supervisors` - the count of all supervisors whether or not these
|
||||
child supervisors are still alive
|
||||
* `:supervisors` - the count of all supervisors whether or not the child
|
||||
process is still alive
|
||||
|
||||
* `:workers` - the count of all workers, whether or not these child workers
|
||||
are still alive
|
||||
* `:workers` - the count of all workers, whether or not the child process
|
||||
is still alive
|
||||
|
||||
"""
|
||||
@spec count_children(supervisor) ::
|
||||
@@ -505,14 +464,15 @@ defmodule Supervisor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the given supervisor with the given `reason`.
|
||||
Stops the supervisor with the given `reason`.
|
||||
|
||||
It returns `:ok` if the supervisor terminates with the given
|
||||
reason. If it terminates with another reason, the call exits.
|
||||
reason, if it terminates with another reason, the call will
|
||||
exit.
|
||||
|
||||
This function keeps OTP semantics regarding error reporting.
|
||||
If the reason is any other than `:normal`, `:shutdown` or
|
||||
`{:shutdown, _}`, an error report is logged.
|
||||
`{:shutdown, _}`, an error report will be logged.
|
||||
"""
|
||||
@spec stop(supervisor, reason :: term, timeout) :: :ok
|
||||
def stop(supervisor, reason \\ :normal, timeout \\ :infinity) do
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
defmodule Supervisor.Default do
|
||||
@moduledoc false
|
||||
@behaviour :supervisor
|
||||
|
||||
@doc """
|
||||
Supervisor callback that simply returns the given args.
|
||||
|
||||
This is the supervisor used by `Supervisor.start_link/2`
|
||||
and others.
|
||||
This is the supervisor used by `Supervisor.start_link/2`.
|
||||
"""
|
||||
def init(args) do
|
||||
args
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,11 +1,11 @@
|
||||
defmodule Supervisor.Spec do
|
||||
@moduledoc """
|
||||
Convenience functions for defining supervisor specifications.
|
||||
Convenience functions for defining a supervision specification.
|
||||
|
||||
## Example
|
||||
|
||||
By using the functions in this module one can specify the children
|
||||
to be used under a supervisor, started with `Supervisor.start_link/2`:
|
||||
By using the functions in this module one can define a supervisor
|
||||
and start it with `Supervisor.start_link/2`:
|
||||
|
||||
import Supervisor.Spec
|
||||
|
||||
@@ -16,7 +16,7 @@ defmodule Supervisor.Spec do
|
||||
|
||||
Supervisor.start_link(children, strategy: :one_for_one)
|
||||
|
||||
Sometimes, it may be handy to define supervisors backed
|
||||
In many situations, it may be handy to define supervisors backed
|
||||
by a module:
|
||||
|
||||
defmodule MySupervisor do
|
||||
@@ -37,35 +37,42 @@ defmodule Supervisor.Spec do
|
||||
|
||||
Notice in this case we don't have to explicitly import
|
||||
`Supervisor.Spec` as `use Supervisor` automatically does so.
|
||||
Defining a module-based supervisor can be useful, for example,
|
||||
to perform initialization tasks in the `c:init/1` callback.
|
||||
|
||||
Explicit supervisors as above are required when there is a need to:
|
||||
|
||||
1. Partially change the supervision tree during hot-code swaps.
|
||||
|
||||
2. Define supervisors inside other supervisors.
|
||||
|
||||
3. Perform actions inside the supervision `init/1` callback.
|
||||
|
||||
For example, you may want to start an ETS table that is linked to
|
||||
the supervisor (i.e. if the supervision tree needs to be restarted,
|
||||
the ETS table must be restarted too).
|
||||
|
||||
## Supervisor and worker options
|
||||
|
||||
In the example above, we defined specs for workers and supervisors.
|
||||
These specs (both for workers as well as supervisors) accept the
|
||||
following options:
|
||||
In the example above, we defined workers and supervisors
|
||||
and each accepts the following options:
|
||||
|
||||
* `:id` - a name used to identify the child specification
|
||||
internally by the supervisor; defaults to the given module
|
||||
name for the child worker/supervisor
|
||||
name
|
||||
|
||||
* `:function` - the function to invoke on the child to start it
|
||||
|
||||
* `:restart` - an atom that defines when a terminated child process should
|
||||
be restarted (see the "Restart values" section below)
|
||||
* `:restart` - defines when a terminated child process should be restarted
|
||||
|
||||
* `:shutdown` - an atom that defines how a child process should be
|
||||
terminated (see the "Shutdown values" section below)
|
||||
* `:shutdown` - defines how a child process should be terminated
|
||||
|
||||
* `:modules` - it should be a list with one element `[module]`,
|
||||
where module is the name of the callback module only if the
|
||||
child process is a `Supervisor` or `GenServer`; if the child
|
||||
process is a `GenEvent`, `:modules` should be `:dynamic`
|
||||
process is a `GenEvent`, modules should be `:dynamic`
|
||||
|
||||
### Restart values (:restart)
|
||||
|
||||
The following restart values are supported in the `:restart` option:
|
||||
The following restart values are supported:
|
||||
|
||||
* `:permanent` - the child process is always restarted
|
||||
|
||||
@@ -73,31 +80,27 @@ defmodule Supervisor.Spec do
|
||||
when the supervisor's strategy is `:rest_for_one` or `:one_for_all`)
|
||||
|
||||
* `:transient` - the child process is restarted only if it
|
||||
terminates abnormally, i.e., with an exit reason other than
|
||||
terminates abnormally, i.e. with another exit reason than
|
||||
`:normal`, `:shutdown` or `{:shutdown, term}`
|
||||
|
||||
### Shutdown values (:shutdown)
|
||||
|
||||
The following shutdown values are supported in the `:shutdown` option:
|
||||
The following shutdown values are supported:
|
||||
|
||||
* `:brutal_kill` - the child process is unconditionally terminated
|
||||
using `Process.exit(child, :kill)`
|
||||
using `exit(child, :kill)`.
|
||||
|
||||
* `:infinity` - if the child process is a supervisor, this is a mechanism
|
||||
to give the subtree enough time to shutdown; it can also be used with
|
||||
workers with care
|
||||
|
||||
* any integer - the value of `:shutdown` can also be any integer meaning
|
||||
that the supervisor tells the child process to terminate by calling
|
||||
`Process.exit(child, :shutdown)` and then waits for an exit signal back.
|
||||
If no exit signal is received within the specified time (the value of this
|
||||
option, in milliseconds), the child process is unconditionally terminated
|
||||
using `Process.exit(child, :kill)`
|
||||
to give the subtree enough time to shutdown. It can also be used with
|
||||
workers with care.
|
||||
|
||||
* Finally, the value can also be any integer meaning that the supervisor tells
|
||||
the child process to terminate by calling `Process.exit(child, :shutdown)`
|
||||
and then waits for an exit signal back. If no exit signal is received
|
||||
within the specified time (in milliseconds), the child process is
|
||||
unconditionally terminated using `Process.exit(child, :kill)`.
|
||||
"""
|
||||
|
||||
# TODO: Update and provide a digest of strategies once we include DynamicSupervisor.
|
||||
|
||||
@typedoc "Supported strategies"
|
||||
@type strategy :: :simple_one_for_one | :one_for_one | :one_for_all | :rest_for_one
|
||||
|
||||
@@ -128,13 +131,11 @@ defmodule Supervisor.Spec do
|
||||
Receives a list of children (workers or supervisors) to
|
||||
supervise and a set of options.
|
||||
|
||||
Returns a tuple containing the supervisor specification. This tuple can be
|
||||
used as the return value of the `c:init/1` callback when implementing a
|
||||
module-based supervisor.
|
||||
Returns a tuple containing the supervisor specification.
|
||||
|
||||
## Examples
|
||||
|
||||
supervise(children, strategy: :one_for_one)
|
||||
supervise children, strategy: :one_for_one
|
||||
|
||||
## Options
|
||||
|
||||
@@ -144,20 +145,18 @@ defmodule Supervisor.Spec do
|
||||
in the `Supervisor` module docs.
|
||||
|
||||
* `:max_restarts` - the maximum amount of restarts allowed in
|
||||
a time frame. Defaults to `3`.
|
||||
a time frame. Defaults to 3.
|
||||
|
||||
* `:max_seconds` - the time frame in which `:max_restarts` applies.
|
||||
Defaults to `5`.
|
||||
Defaults to 5.
|
||||
|
||||
The `:strategy` option is required and by default a maximum of 3 restarts is
|
||||
allowed within 5 seconds. Check the `Supervisor` module for a detailed
|
||||
description of the available strategies.
|
||||
The `:strategy` option is required and by default maximum 3 restarts
|
||||
are allowed within 5 seconds. Please check the `Supervisor` module for
|
||||
a complete description of the available strategies.
|
||||
"""
|
||||
@spec supervise([spec], strategy: strategy,
|
||||
max_restarts: non_neg_integer,
|
||||
max_seconds: non_neg_integer) :: {:ok, tuple}
|
||||
# TODO: Make it return a tuple of format {:ok, children, opts}
|
||||
# TODO: Deprecate once the new tuple format has been established
|
||||
def supervise(children, options) do
|
||||
unless strategy = options[:strategy] do
|
||||
raise ArgumentError, "expected :strategy option to be given"
|
||||
@@ -170,7 +169,7 @@ defmodule Supervisor.Spec do
|
||||
{:ok, {{strategy, maxR, maxS}, children}}
|
||||
end
|
||||
|
||||
defp assert_unique_ids([id | rest]) do
|
||||
defp assert_unique_ids([id|rest]) do
|
||||
if id in rest do
|
||||
raise ArgumentError,
|
||||
"duplicated id #{inspect id} found in the supervisor specification, " <>
|
||||
@@ -188,7 +187,7 @@ defmodule Supervisor.Spec do
|
||||
Defines the given `module` as a worker which will be started
|
||||
with the given arguments.
|
||||
|
||||
worker(ExUnit.Runner, [], restart: :permanent)
|
||||
worker ExUnit.Runner, [], restart: :permanent
|
||||
|
||||
By default, the function `start_link` is invoked on the given
|
||||
module. Overall, the default values for the options are:
|
||||
@@ -199,8 +198,8 @@ defmodule Supervisor.Spec do
|
||||
shutdown: 5000,
|
||||
modules: [module]]
|
||||
|
||||
Check the documentation for the `Supervisor.Spec` module for more
|
||||
information on the options.
|
||||
Check `Supervisor.Spec` module docs for more information on
|
||||
the options.
|
||||
"""
|
||||
@spec worker(module, [term], [restart: restart, shutdown: shutdown,
|
||||
id: term, function: atom, modules: modules]) :: spec
|
||||
@@ -212,7 +211,7 @@ defmodule Supervisor.Spec do
|
||||
Defines the given `module` as a supervisor which will be started
|
||||
with the given arguments.
|
||||
|
||||
supervisor(ExUnit.Runner, [], restart: :permanent)
|
||||
supervisor ExUnit.Runner, [], restart: :permanent
|
||||
|
||||
By default, the function `start_link` is invoked on the given
|
||||
module. Overall, the default values for the options are:
|
||||
@@ -223,8 +222,8 @@ defmodule Supervisor.Spec do
|
||||
shutdown: :infinity,
|
||||
modules: [module]]
|
||||
|
||||
Check the documentation for the `Supervisor.Spec` module for more
|
||||
information on the options.
|
||||
Check `Supervisor.Spec` module docs for more information on
|
||||
the options.
|
||||
"""
|
||||
@spec supervisor(module, [term], [restart: restart, shutdown: shutdown,
|
||||
id: term, function: atom, modules: modules]) :: spec
|
||||
@@ -233,7 +232,6 @@ defmodule Supervisor.Spec do
|
||||
child(:supervisor, module, args, options)
|
||||
end
|
||||
|
||||
# TODO: Do and expose proper child validation
|
||||
defp child(type, module, args, options) do
|
||||
id = Keyword.get(options, :id, module)
|
||||
modules = Keyword.get(options, :modules, modules(module))
|
||||
@@ -245,7 +243,6 @@ defmodule Supervisor.Spec do
|
||||
restart, shutdown, type, modules}
|
||||
end
|
||||
|
||||
# TODO: Remove GenEvent when there is no more GenEvent v2.0
|
||||
defp modules(GenEvent), do: :dynamic
|
||||
defp modules(module), do: [module]
|
||||
end
|
||||
|
||||
+98
-294
@@ -1,136 +1,47 @@
|
||||
defmodule System do
|
||||
@moduledoc """
|
||||
The `System` module provides functions that interact directly
|
||||
The System module provides access to variables used or
|
||||
maintained by the VM and to functions that interact directly
|
||||
with the VM or the host system.
|
||||
|
||||
## Time
|
||||
|
||||
The `System` module also provides functions that work with time,
|
||||
returning different times kept by the system with support for
|
||||
different time units.
|
||||
|
||||
One of the complexities in relying on system times is that they
|
||||
may be adjusted. For example, when you enter and leave daylight
|
||||
saving time, the system clock will be adjusted, often adding
|
||||
or removing one hour. We call such changes "time warps". In
|
||||
order to understand how such changes may be harmful, imagine
|
||||
the following code:
|
||||
|
||||
## DO NOT DO THIS
|
||||
prev = System.os_time()
|
||||
# ... execute some code ...
|
||||
next = System.os_time()
|
||||
diff = next - prev
|
||||
|
||||
If, while the code is executing, the system clock changes,
|
||||
some code that executed in 1 second may be reported as taking
|
||||
over 1 hour! To address such concerns, the VM provides a
|
||||
monotonic time via `System.monotonic_time/0` which never
|
||||
decreases and does not leap:
|
||||
|
||||
## DO THIS
|
||||
prev = System.monotonic_time()
|
||||
# ... execute some code ...
|
||||
next = System.monotonic_time()
|
||||
diff = next - prev
|
||||
|
||||
Generally speaking, the VM provides three time measurements:
|
||||
|
||||
* `os_time/0` - the time reported by the OS. This time may be
|
||||
adjusted forwards or backwards in time with no limitation;
|
||||
|
||||
* `system_time/0` - the VM view of the `os_time/0`. The system time and OS
|
||||
time may not match in case of time warps although the VM works towards
|
||||
aligning them. This time is not monotonic (i.e., it may decrease)
|
||||
as its behaviour is configured [by the VM time warp
|
||||
mode](http://www.erlang.org/doc/apps/erts/time_correction.html#Time_Warp_Modes);
|
||||
|
||||
* `monotonic_time/0` - a monotonically increasing time provided
|
||||
by the Erlang VM.
|
||||
|
||||
The time functions in this module work in the `:native` unit
|
||||
(unless specified otherwise), which is OS dependent. Most of
|
||||
the time, all calculations are done in the `:native` unit, to
|
||||
avoid loss of precision, with `convert_time_unit/3` being
|
||||
invoked at the end to convert to a specific time unit like
|
||||
`:millisecond` or `:microsecond`. See the `t:time_unit/0` type for
|
||||
more information.
|
||||
|
||||
For a more complete rundown on the VM support for different
|
||||
times, see the [chapter on time and time
|
||||
correction](http://www.erlang.org/doc/apps/erts/time_correction.html)
|
||||
in the Erlang docs.
|
||||
"""
|
||||
|
||||
@typedoc """
|
||||
The time unit to be passed to functions like `monotonic_time/1` and others.
|
||||
|
||||
The `:second`, `:millisecond`, `:microsecond` and `:nanosecond` time
|
||||
units controls the return value of the functions that accept a time unit.
|
||||
|
||||
A time unit can also be a strictly positive integer. In this case, it
|
||||
represents the "parts per second": the time will be returned in `1 /
|
||||
parts_per_second` seconds. For example, using the `:millisecond` time unit
|
||||
is equivalent to using `1000` as the time unit (as the time will be returned
|
||||
in 1/1000 seconds - milliseconds).
|
||||
|
||||
Keep in mind the Erlang API prior to version 19.1 will use `:milli_seconds`,
|
||||
`:micro_seconds` and `:nano_seconds` as time units although Elixir normalizes
|
||||
their spelling to match the SI convention.
|
||||
"""
|
||||
@type time_unit ::
|
||||
:second
|
||||
| :millisecond
|
||||
| :microsecond
|
||||
| :nanosecond
|
||||
| pos_integer
|
||||
# TODO: Deprecate these in Elixir 2.0
|
||||
| :seconds
|
||||
| :milliseconds
|
||||
| :microseconds
|
||||
| :nanoseconds
|
||||
|
||||
@base_dir :filename.join(__DIR__, "../../..")
|
||||
@version_file :filename.join(@base_dir, "VERSION")
|
||||
|
||||
defp strip(iodata) do
|
||||
:re.replace(iodata, "^[\s\r\n\t]+|[\s\r\n\t]+$", "", [:global, return: :binary])
|
||||
defp strip_re(iodata, pattern) do
|
||||
:re.replace(iodata, pattern, "", [return: :binary])
|
||||
end
|
||||
|
||||
defp read_stripped(path) do
|
||||
case :file.read_file(path) do
|
||||
{:ok, binary} ->
|
||||
strip(binary)
|
||||
_ ->
|
||||
""
|
||||
strip_re(binary, "^\s+|\s+$")
|
||||
_ -> ""
|
||||
end
|
||||
end
|
||||
|
||||
# Read and strip the version from the VERSION file.
|
||||
defmacrop get_version do
|
||||
case read_stripped(@version_file) do
|
||||
case read_stripped(:filename.join(__DIR__, "../../../VERSION")) do
|
||||
"" -> raise RuntimeError, message: "could not read the version number from VERSION"
|
||||
data -> data
|
||||
end
|
||||
end
|
||||
|
||||
# Tries to run "git rev-parse --short HEAD". In the case of success returns
|
||||
# the short revision hash. If that fails, returns an empty string.
|
||||
# the short revision hash. If that is not available, tries to read the commit hash
|
||||
# from .git/HEAD. If that fails, returns an empty string.
|
||||
defmacrop get_revision do
|
||||
null =
|
||||
case :os.type do
|
||||
{:win32, _} -> 'NUL'
|
||||
_ -> '/dev/null'
|
||||
end
|
||||
|
||||
'git rev-parse --short HEAD 2> '
|
||||
|> Kernel.++(null)
|
||||
|> :os.cmd()
|
||||
|> strip
|
||||
dirpath = :filename.join(__DIR__, "../../../.git")
|
||||
case :file.read_file_info(dirpath) do
|
||||
{:ok, _} ->
|
||||
if :os.find_executable('git') do
|
||||
data = :os.cmd('git rev-parse --short HEAD')
|
||||
strip_re(data, "\n")
|
||||
else
|
||||
read_stripped(:filename.join(".git", "HEAD"))
|
||||
end
|
||||
_ -> ""
|
||||
end
|
||||
end
|
||||
|
||||
defp revision, do: get_revision()
|
||||
|
||||
# Get the date at compilation time.
|
||||
defmacrop get_date do
|
||||
IO.iodata_to_binary :httpd_util.rfc1123_date
|
||||
@@ -157,31 +68,16 @@ defmodule System do
|
||||
Returns Elixir's version as binary.
|
||||
"""
|
||||
@spec version() :: String.t
|
||||
def version, do: get_version()
|
||||
def version, do: get_version
|
||||
|
||||
@doc """
|
||||
Elixir build information.
|
||||
|
||||
Returns a keyword list with Elixir version, Git short revision hash and compilation date.
|
||||
Returns a keyword list with Elixir version, git short revision hash and compilation date.
|
||||
"""
|
||||
@spec build_info() :: map
|
||||
def build_info do
|
||||
%{build: build(),
|
||||
date: get_date(),
|
||||
revision: revision(),
|
||||
version: version()}
|
||||
end
|
||||
|
||||
# Returns a string of the build info
|
||||
defp build do
|
||||
{:ok, v} = Version.parse(version())
|
||||
|
||||
cond do
|
||||
([] == v.pre) or ("" == revision()) ->
|
||||
version()
|
||||
true ->
|
||||
"#{version()} (#{revision()})"
|
||||
end
|
||||
%{version: version, date: get_date, revision: get_revision}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -233,7 +129,7 @@ defmodule System do
|
||||
Returns the current working directory or raises `RuntimeError`.
|
||||
"""
|
||||
def cwd! do
|
||||
cwd() ||
|
||||
cwd ||
|
||||
raise RuntimeError, message: "could not get a current working directory, the current location is not accessible"
|
||||
end
|
||||
|
||||
@@ -253,7 +149,7 @@ defmodule System do
|
||||
instead of returning `nil` if no user home is set.
|
||||
"""
|
||||
def user_home! do
|
||||
user_home() ||
|
||||
user_home ||
|
||||
raise RuntimeError, message: "could not find the user home, please set the HOME environment variable"
|
||||
end
|
||||
|
||||
@@ -286,7 +182,7 @@ defmodule System do
|
||||
instead of returning `nil` if no temp dir is set.
|
||||
"""
|
||||
def tmp_dir! do
|
||||
tmp_dir() ||
|
||||
tmp_dir ||
|
||||
raise RuntimeError, message: "could not get a writable temporary directory, " <>
|
||||
"please set the TMPDIR environment variable"
|
||||
end
|
||||
@@ -325,7 +221,7 @@ defmodule System do
|
||||
The function must receive the exit status code as an argument.
|
||||
"""
|
||||
def at_exit(fun) when is_function(fun, 1) do
|
||||
:elixir_config.update :at_exit, &[fun | &1]
|
||||
:elixir_config.update :at_exit, &[fun|&1]
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -339,7 +235,7 @@ defmodule System do
|
||||
"""
|
||||
@spec find_executable(binary) :: binary | nil
|
||||
def find_executable(program) when is_binary(program) do
|
||||
case :os.find_executable(String.to_charlist(program)) do
|
||||
case :os.find_executable(String.to_char_list(program)) do
|
||||
false -> nil
|
||||
other -> List.to_string(other)
|
||||
end
|
||||
@@ -351,7 +247,7 @@ defmodule System do
|
||||
Returns a list of all environment variables. Each variable is given as a
|
||||
`{name, value}` tuple where both `name` and `value` are strings.
|
||||
"""
|
||||
@spec get_env() :: %{optional(String.t) => String.t}
|
||||
@spec get_env() :: %{String.t => String.t}
|
||||
def get_env do
|
||||
Enum.into(:os.getenv, %{}, fn var ->
|
||||
var = IO.chardata_to_string var
|
||||
@@ -369,7 +265,7 @@ defmodule System do
|
||||
"""
|
||||
@spec get_env(binary) :: binary | nil
|
||||
def get_env(varname) when is_binary(varname) do
|
||||
case :os.getenv(String.to_charlist(varname)) do
|
||||
case :os.getenv(String.to_char_list(varname)) do
|
||||
false -> nil
|
||||
other -> List.to_string(other)
|
||||
end
|
||||
@@ -393,7 +289,7 @@ defmodule System do
|
||||
"""
|
||||
@spec put_env(binary, binary) :: :ok
|
||||
def put_env(varname, value) when is_binary(varname) and is_binary(value) do
|
||||
:os.putenv String.to_charlist(varname), String.to_charlist(value)
|
||||
:os.putenv String.to_char_list(varname), String.to_char_list(value)
|
||||
:ok
|
||||
end
|
||||
|
||||
@@ -415,7 +311,7 @@ defmodule System do
|
||||
"""
|
||||
@spec delete_env(String.t) :: :ok
|
||||
def delete_env(varname) do
|
||||
:os.unsetenv(String.to_charlist(varname))
|
||||
:os.unsetenv(String.to_char_list(varname))
|
||||
:ok
|
||||
end
|
||||
|
||||
@@ -468,7 +364,7 @@ defmodule System do
|
||||
end
|
||||
|
||||
def halt(status) when is_binary(status) do
|
||||
:erlang.halt(String.to_charlist(status))
|
||||
:erlang.halt(String.to_char_list(status))
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@@ -477,24 +373,14 @@ defmodule System do
|
||||
`command` is expected to be an executable available in PATH
|
||||
unless an absolute path is given.
|
||||
|
||||
`args` must be a list of binaries which the executable will receive
|
||||
as its arguments as is. This means that:
|
||||
|
||||
* environment variables will not be interpolated
|
||||
* wildcard expansion will not happen (unless `Path.wildcard/2` is used
|
||||
explicitly)
|
||||
* arguments do not need to be escaped or quoted for shell safety
|
||||
`args` must be a list of strings which are not expanded
|
||||
in any way. For example, this means wildcard expansion will
|
||||
not happen unless `Path.wildcard/2` is used. On Windows though,
|
||||
wildcard expansion is up to the program.
|
||||
|
||||
This function returns a tuple containing the collected result
|
||||
and the command exit status.
|
||||
|
||||
Internally, this function uses a `Port` for interacting with the
|
||||
outside world. However, if you plan to run a long-running program,
|
||||
ports guarantee stdin/stdout devices will be closed but it does not
|
||||
automatically terminate the problem. The documentation for the
|
||||
`Port` module describes this problem and possible solutions under
|
||||
the "Zombie processes" section.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> System.cmd "echo", ["hello"]
|
||||
@@ -512,7 +398,7 @@ defmodule System do
|
||||
* `:into` - injects the result into the given collectable, defaults to `""`
|
||||
* `:cd` - the directory to run the command in
|
||||
* `:env` - an enumerable of tuples containing environment key-value as binary
|
||||
* `:arg0` - sets the command arg0
|
||||
* `:arg0` - set the command arg0
|
||||
* `:stderr_to_stdout` - redirects stderr to stdout when `true`
|
||||
* `:parallelism` - when `true`, the VM will schedule port tasks to improve
|
||||
parallelism in the system. If set to `false`, the VM will try to perform
|
||||
@@ -555,7 +441,7 @@ defmodule System do
|
||||
@spec cmd(binary, [binary], Keyword.t) ::
|
||||
{Collectable.t, exit_status :: non_neg_integer}
|
||||
def cmd(command, args, opts \\ []) when is_binary(command) and is_list(args) do
|
||||
cmd = String.to_charlist(command)
|
||||
cmd = String.to_char_list(command)
|
||||
|
||||
cmd =
|
||||
if Path.type(cmd) == :absolute do
|
||||
@@ -587,28 +473,28 @@ defmodule System do
|
||||
end
|
||||
end
|
||||
|
||||
defp cmd_opts([{:into, any} | t], opts, _into),
|
||||
defp cmd_opts([{:into, any}|t], opts, _into),
|
||||
do: cmd_opts(t, opts, any)
|
||||
|
||||
defp cmd_opts([{:cd, bin} | t], opts, into) when is_binary(bin),
|
||||
do: cmd_opts(t, [{:cd, bin} | opts], into)
|
||||
defp cmd_opts([{:cd, bin}|t], opts, into) when is_binary(bin),
|
||||
do: cmd_opts(t, [{:cd, bin}|opts], into)
|
||||
|
||||
defp cmd_opts([{:arg0, bin} | t], opts, into) when is_binary(bin),
|
||||
do: cmd_opts(t, [{:arg0, bin} | opts], into)
|
||||
defp cmd_opts([{:arg0, bin}|t], opts, into) when is_binary(bin),
|
||||
do: cmd_opts(t, [{:arg0, bin}|opts], into)
|
||||
|
||||
defp cmd_opts([{:stderr_to_stdout, true} | t], opts, into),
|
||||
do: cmd_opts(t, [:stderr_to_stdout | opts], into)
|
||||
defp cmd_opts([{:stderr_to_stdout, true}|t], opts, into),
|
||||
do: cmd_opts(t, [:stderr_to_stdout|opts], into)
|
||||
|
||||
defp cmd_opts([{:stderr_to_stdout, false} | t], opts, into),
|
||||
defp cmd_opts([{:stderr_to_stdout, false}|t], opts, into),
|
||||
do: cmd_opts(t, opts, into)
|
||||
|
||||
defp cmd_opts([{:parallelism, bool} | t], opts, into) when is_boolean(bool),
|
||||
do: cmd_opts(t, [{:parallelism, bool} | opts], into)
|
||||
defp cmd_opts([{:parallelism, bool}|t], opts, into) when is_boolean(bool),
|
||||
do: cmd_opts(t, [{:parallelism, bool}|opts], into)
|
||||
|
||||
defp cmd_opts([{:env, enum} | t], opts, into),
|
||||
do: cmd_opts(t, [{:env, validate_env(enum)} | opts], into)
|
||||
defp cmd_opts([{:env, enum}|t], opts, into),
|
||||
do: cmd_opts(t, [{:env, validate_env(enum)}|opts], into)
|
||||
|
||||
defp cmd_opts([{key, val} | _], _opts, _into),
|
||||
defp cmd_opts([{key, val}|_], _opts, _into),
|
||||
do: raise(ArgumentError, "invalid option #{inspect key} with value #{inspect val}")
|
||||
|
||||
defp cmd_opts([], opts, into),
|
||||
@@ -617,9 +503,9 @@ defmodule System do
|
||||
defp validate_env(enum) do
|
||||
Enum.map enum, fn
|
||||
{k, nil} ->
|
||||
{String.to_charlist(k), false}
|
||||
{String.to_char_list(k), false}
|
||||
{k, v} ->
|
||||
{String.to_charlist(k), String.to_charlist(v)}
|
||||
{String.to_char_list(k), String.to_char_list(v)}
|
||||
other ->
|
||||
raise ArgumentError, "invalid environment key-value #{inspect other}"
|
||||
end
|
||||
@@ -628,8 +514,12 @@ defmodule System do
|
||||
@doc """
|
||||
Returns the current monotonic time in the `:native` time unit.
|
||||
|
||||
This time is monotonically increasing and starts in an unspecified
|
||||
point in time.
|
||||
This time is monotonically increasing and starts in an unspecified point in
|
||||
time.
|
||||
|
||||
For more information, see the [chapter on time and time
|
||||
correction](http://www.erlang.org/doc/apps/erts/time_correction.html) in the
|
||||
Erlang docs.
|
||||
|
||||
Inlined by the compiler into `:erlang.monotonic_time/0`.
|
||||
"""
|
||||
@@ -641,20 +531,23 @@ defmodule System do
|
||||
@doc """
|
||||
Returns the current monotonic time in the given time unit.
|
||||
|
||||
This time is monotonically increasing and starts in an unspecified
|
||||
point in time.
|
||||
For more information, see the [chapter on time and time
|
||||
correction](http://www.erlang.org/doc/apps/erts/time_correction.html) in the
|
||||
Erlang docs.
|
||||
|
||||
Inlined by the compiler into `:erlang.monotonic_time/1`.
|
||||
"""
|
||||
@spec monotonic_time(time_unit) :: integer
|
||||
@spec monotonic_time(:erlang.time_unit) :: integer
|
||||
def monotonic_time(unit) do
|
||||
:erlang.monotonic_time(normalize_time_unit(unit))
|
||||
:erlang.monotonic_time(unit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current system time in the `:native` time unit.
|
||||
|
||||
It is the VM view of the `os_time/0`. They may not match in
|
||||
case of time warps although the VM works towards aligning
|
||||
them. This time is not monotonic.
|
||||
For more information, see the [chapter on time and time
|
||||
correction](http://www.erlang.org/doc/apps/erts/time_correction.html) in the
|
||||
Erlang docs.
|
||||
|
||||
Inlined by the compiler into `:erlang.system_time/0`.
|
||||
"""
|
||||
@@ -666,36 +559,31 @@ defmodule System do
|
||||
@doc """
|
||||
Returns the current system time in the given time unit.
|
||||
|
||||
It is the VM view of the `os_time/0`. They may not match in
|
||||
case of time warps although the VM works towards aligning
|
||||
them. This time is not monotonic.
|
||||
For more information, see the [chapter on time and time
|
||||
correction](http://www.erlang.org/doc/apps/erts/time_correction.html) in the
|
||||
Erlang docs.
|
||||
|
||||
Inlined by the compiler into `:erlang.system_time/1`.
|
||||
"""
|
||||
@spec system_time(time_unit) :: integer
|
||||
@spec system_time(:erlang.time_unit) :: integer
|
||||
def system_time(unit) do
|
||||
:erlang.system_time(normalize_time_unit(unit))
|
||||
:erlang.system_time(unit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts `time` from time unit `from_unit` to time unit `to_unit`.
|
||||
Converts `time` from time unit `from_unit` to time unit `to_unit`. The result
|
||||
is rounded via the floor function.
|
||||
|
||||
The result is rounded via the floor function.
|
||||
|
||||
`convert_time_unit/3` accepts an additional time unit (other than the
|
||||
ones in the `t:time_unit/0` type) called `:native`. `:native` is the time
|
||||
unit used by the Erlang runtime system. It's determined when the runtime
|
||||
starts and stays the same until the runtime is stopped. To determine what
|
||||
the `:native` unit amounts to in a system, you can call this function to
|
||||
convert 1 second to the `:native` time unit (i.e.,
|
||||
`System.convert_time_unit(1, :second, :native)`).
|
||||
Inlined by the compiler into `:erlang.convert_time_unit/3`.
|
||||
"""
|
||||
@spec convert_time_unit(integer, time_unit | :native, time_unit | :native) :: integer
|
||||
@spec convert_time_unit(integer, :erlang.time_unit, :erlang.time_unit) :: integer
|
||||
def convert_time_unit(time, from_unit, to_unit) do
|
||||
:erlang.convert_time_unit(time, normalize_time_unit(from_unit), normalize_time_unit(to_unit))
|
||||
:erlang.convert_time_unit(time, from_unit, to_unit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current time offset between the Erlang VM monotonic
|
||||
time and the Erlang VM system time.
|
||||
Returns the current time offset between the Erlang monotonic time and the
|
||||
Erlang system time.
|
||||
|
||||
The result is returned in the `:native` time unit.
|
||||
|
||||
@@ -709,67 +597,22 @@ defmodule System do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current time offset between the Erlang VM monotonic
|
||||
time and the Erlang VM system time.
|
||||
Returns the current time offset between the Erlang monotonic time and the
|
||||
Erlang system time.
|
||||
|
||||
The result is returned in the given time unit `unit`. The returned
|
||||
offset, added to an Erlang monotonic time (e.g., obtained with
|
||||
`monotonic_time/1`), gives the Erlang system time that corresponds
|
||||
to that monotonic time.
|
||||
The result is returned in the given time unit `unit`. The returned offset,
|
||||
added to an Erlang monotonic time (e.g., obtained with `monotonic_time/1`),
|
||||
gives the Erlang system time that corresponds to that monotonic time.
|
||||
|
||||
For more information, see the [chapter on time and time
|
||||
correction](http://www.erlang.org/doc/apps/erts/time_correction.html) in the
|
||||
Erlang docs.
|
||||
|
||||
Inlined by the compiler into `:erlang.time_offset/1`.
|
||||
"""
|
||||
@spec time_offset(time_unit) :: integer
|
||||
@spec time_offset(:erlang.time_unit) :: integer
|
||||
def time_offset(unit) do
|
||||
:erlang.time_offset(normalize_time_unit(unit))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current OS time.
|
||||
|
||||
The result is returned in the `:native` time unit.
|
||||
|
||||
This time may be adjusted forwards or backwards in time
|
||||
with no limitation and is not monotonic.
|
||||
|
||||
Inlined by the compiler into `:os.system_time/0`.
|
||||
"""
|
||||
@spec os_time() :: integer
|
||||
def os_time do
|
||||
:os.system_time()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current OS time in the given time `unit`.
|
||||
|
||||
This time may be adjusted forwards or backwards in time
|
||||
with no limitation and is not monotonic.
|
||||
"""
|
||||
@spec os_time(time_unit) :: integer
|
||||
def os_time(unit) do
|
||||
:os.system_time(normalize_time_unit(unit))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the OTP release number.
|
||||
"""
|
||||
@spec otp_release :: String.t
|
||||
def otp_release do
|
||||
:erlang.list_to_binary :erlang.system_info(:otp_release)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the number of schedulers in the VM.
|
||||
"""
|
||||
@spec schedulers :: pos_integer
|
||||
def schedulers do
|
||||
:erlang.system_info(:schedulers)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the number of schedulers online in the VM.
|
||||
"""
|
||||
@spec schedulers_online :: pos_integer
|
||||
def schedulers_online do
|
||||
:erlang.system_info(:schedulers_online)
|
||||
:erlang.time_offset(unit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -780,7 +623,7 @@ defmodule System do
|
||||
will never return the same integer more than once on the current runtime
|
||||
instance.
|
||||
|
||||
If `modifiers` is `[]`, then a unique integer (that can be positive or negative) is returned.
|
||||
If `modifiers` is `[]`, then an unique integer (that can be positive or negative) is returned.
|
||||
Other modifiers can be passed to change the properties of the returned integer:
|
||||
|
||||
* `:positive` - the returned integer is guaranteed to be positive.
|
||||
@@ -799,43 +642,4 @@ defmodule System do
|
||||
def unique_integer(modifiers \\ []) do
|
||||
:erlang.unique_integer(modifiers)
|
||||
end
|
||||
|
||||
defp normalize_time_unit(:native),
|
||||
do: :native
|
||||
|
||||
# TODO: Remove these mappings once Elixir requires Erlang/OTP 19.1
|
||||
defp normalize_time_unit(:second),
|
||||
do: :seconds
|
||||
defp normalize_time_unit(:millisecond),
|
||||
do: :milli_seconds
|
||||
defp normalize_time_unit(:microsecond),
|
||||
do: :micro_seconds
|
||||
defp normalize_time_unit(:nanosecond),
|
||||
do: :nano_seconds
|
||||
|
||||
# TODO: Warn on Elixir 1.5
|
||||
defp normalize_time_unit(:seconds),
|
||||
do: :seconds
|
||||
defp normalize_time_unit(:milliseconds),
|
||||
do: :milli_seconds
|
||||
defp normalize_time_unit(:microseconds),
|
||||
do: :micro_seconds
|
||||
defp normalize_time_unit(:nanoseconds),
|
||||
do: :nano_seconds
|
||||
|
||||
defp normalize_time_unit(unit) when is_integer(unit) and unit > 0,
|
||||
do: unit
|
||||
|
||||
# TODO: Warn on Elixir 1.5
|
||||
defp normalize_time_unit(erlang_unit)
|
||||
when erlang_unit in [:milli_seconds, :micro_seconds, :nano_seconds] do
|
||||
erlang_unit
|
||||
end
|
||||
|
||||
defp normalize_time_unit(other) do
|
||||
raise ArgumentError,
|
||||
"unsupported time unit. Expected :second, :millisecond, " <>
|
||||
":microsecond, :nanosecond, or a positive integer, " <>
|
||||
"got #{inspect other}"
|
||||
end
|
||||
end
|
||||
|
||||
+148
-252
@@ -3,7 +3,7 @@ defmodule Task do
|
||||
Conveniences for spawning and awaiting tasks.
|
||||
|
||||
Tasks are processes meant to execute one particular
|
||||
action throughout their lifetime, often with little or no
|
||||
action throughout their life-cycle, often with little or no
|
||||
communication with other processes. The most common use case
|
||||
for tasks is to convert sequential code into concurrent code
|
||||
by computing a value asynchronously:
|
||||
@@ -12,71 +12,72 @@ defmodule Task do
|
||||
res = do_some_other_work()
|
||||
res + Task.await(task)
|
||||
|
||||
Tasks spawned with `async` can be awaited on by their caller
|
||||
Tasks spawned with `async` can be waited on by their caller
|
||||
process (and only their caller) as shown in the example above.
|
||||
They are implemented by spawning a process that sends a message
|
||||
to the caller once the given computation is performed.
|
||||
|
||||
Besides `async/1` and `await/2`, tasks can also be
|
||||
started as part of a supervision tree and dynamically spawned
|
||||
on remote nodes. We will explore all three scenarios next.
|
||||
started as part of supervision tree and dynamically spawned
|
||||
in remote nodes. We will explore all three scenarios next.
|
||||
|
||||
## async and await
|
||||
|
||||
One of the common uses of tasks is to convert sequential code
|
||||
One of the common use of tasks is to convert sequential code
|
||||
into concurrent code with `Task.async/1` while keeping its semantics.
|
||||
When invoked, a new process will be created, linked and monitored
|
||||
by the caller. Once the task action finishes, a message will be sent
|
||||
to the caller with the result.
|
||||
|
||||
`Task.await/2` is used to read the message sent by the task.
|
||||
`await` will check the monitor setup by the call to `async/1` to
|
||||
verify if the process exited for any abnormal reason (or in case
|
||||
exits are being trapped by the caller).
|
||||
|
||||
There are two important things to consider when using `async`:
|
||||
There are two important things to consider when using async:
|
||||
|
||||
1. If you are using async tasks, you **must await** a reply
|
||||
1. If you are using async tasks, you must await a reply
|
||||
as they are *always* sent. If you are not expecting a reply,
|
||||
consider using `Task.start_link/1` detailed below.
|
||||
consider using `Task.start_link/1` detailed below
|
||||
|
||||
2. async tasks link the caller and the spawned process. This
|
||||
means that, if the caller crashes, the task will crash
|
||||
too and vice-versa. This is on purpose: if the process
|
||||
too and vice-versa. This is on purpose, if the process
|
||||
meant to receive the result no longer exists, there is
|
||||
no purpose in completing the computation.
|
||||
|
||||
If this is not desired, use `Task.start/1` or consider starting
|
||||
the task under a `Task.Supervisor` using `async_nolink` or
|
||||
`start_child`.
|
||||
no purpose in completing computation of the result. If this
|
||||
is not desired, consider using `Task.start_link/1` as well
|
||||
|
||||
`Task.yield/2` is an alternative to `await/2` where the caller will
|
||||
temporarily block, waiting until the task replies or crashes. If the
|
||||
result does not arrive within the timeout, it can be called again at a
|
||||
result does not arrive within the timeout it can be called again at a
|
||||
later moment. This allows checking for the result of a task multiple
|
||||
times. If a reply does not arrive within the desired time,
|
||||
`Task.shutdown/2` can be used to stop the task.
|
||||
times or to handle a timeout. If a reply does not arrive within the
|
||||
desired time, `Task.shutdown/2` can be used to stop the task.
|
||||
|
||||
## Supervised tasks
|
||||
|
||||
It is also possible to spawn a task under a supervisor:
|
||||
It is also possible to spawn a task inside a supervision tree
|
||||
with `start_link/1` and `start_link/3`:
|
||||
|
||||
Task.start_link(fn -> IO.puts "ok" end)
|
||||
|
||||
Such tasks can be mounted in your supervision tree as:
|
||||
|
||||
import Supervisor.Spec
|
||||
|
||||
children = [
|
||||
#
|
||||
worker(Task, [fn -> IO.puts "ok" end])
|
||||
]
|
||||
|
||||
Internally the supervisor will invoke `Task.start_link/1`.
|
||||
|
||||
Since these tasks are supervised and not directly linked to
|
||||
the caller, they cannot be awaited on. Note `start_link/1`,
|
||||
the caller, they cannot be waited on. Note `start_link/1`,
|
||||
unlike `async/1`, returns `{:ok, pid}` (which is
|
||||
the result expected by supervision trees).
|
||||
|
||||
By default, most supervision strategies will try to restart
|
||||
a worker after it exits regardless of the reason. If you design
|
||||
the task to terminate normally (as in the example with `IO.puts/2`
|
||||
above), consider passing `restart: :transient` in the options
|
||||
to `Supervisor.Spec.worker/3`.
|
||||
a worker after it exits regardless of reason. If you design the
|
||||
task to terminate normally (as in the example with `IO.puts/2` above),
|
||||
consider passing `restart: :transient` in the options to `worker/3`.
|
||||
|
||||
## Dynamically supervised tasks
|
||||
|
||||
@@ -112,35 +113,36 @@ defmodule Task do
|
||||
# Do something
|
||||
end) |> Task.await()
|
||||
|
||||
Finally, check `Task.Supervisor` for other supported operations.
|
||||
Finally, check `Task.Supervisor` for other operations supported by the
|
||||
Task supervisor.
|
||||
|
||||
## Distributed tasks
|
||||
|
||||
Since Elixir provides a Task supervisor, it is easy to use one
|
||||
to dynamically spawn tasks across nodes:
|
||||
Since Elixir provides a Task supervisor, it is easy to use a task
|
||||
supervisor to dynamically spawn tasks across nodes:
|
||||
|
||||
# On the remote node
|
||||
# In the remote node
|
||||
Task.Supervisor.start_link(name: MyApp.DistSupervisor)
|
||||
|
||||
# On the client
|
||||
# In the client
|
||||
Task.Supervisor.async({MyApp.DistSupervisor, :remote@local},
|
||||
MyMod, :my_fun, [arg1, arg2, arg3])
|
||||
|
||||
Note that, when working with distributed tasks, one should use the `Task.Supervisor.async/4` function
|
||||
that expects explicit module, function and arguments, instead of `Task.Supervisor.async/2` that
|
||||
Note that, when working with distributed tasks, one should use the `async/4` function
|
||||
that expects explicit module, function and arguments, instead of `async/2` that
|
||||
works with anonymous functions. That's because anonymous functions expect
|
||||
the same module version to exist on all involved nodes. Check the `Agent` module
|
||||
documentation for more information on distributed processes as the limitations
|
||||
described there apply to the whole ecosystem.
|
||||
described in the agents documentation apply to the whole ecosystem.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
The Task struct.
|
||||
|
||||
It contains these fields:
|
||||
It contains two fields:
|
||||
|
||||
* `:pid` - the PID of the task process; `nil` if the task does
|
||||
not use a task process
|
||||
* `:pid` - the process reference of the task process; `nil` if the task does
|
||||
not use a task process.
|
||||
|
||||
* `:ref` - the task monitor reference
|
||||
|
||||
@@ -164,7 +166,7 @@ defmodule Task do
|
||||
"""
|
||||
@spec start_link(module, atom, [term]) :: {:ok, pid}
|
||||
def start_link(mod, fun, args) do
|
||||
Task.Supervised.start_link(get_info(self()), {mod, fun, args})
|
||||
Task.Supervised.start_link(get_info(self), {mod, fun, args})
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -188,11 +190,11 @@ defmodule Task do
|
||||
"""
|
||||
@spec start(module, atom, [term]) :: {:ok, pid}
|
||||
def start(mod, fun, args) do
|
||||
Task.Supervised.start(get_info(self()), {mod, fun, args})
|
||||
Task.Supervised.start(get_info(self), {mod, fun, args})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a task that must be awaited on.
|
||||
Starts a task that can be awaited on.
|
||||
|
||||
This function spawns a process that is linked to and monitored
|
||||
by the caller process. A `Task` struct is returned containing
|
||||
@@ -201,7 +203,10 @@ defmodule Task do
|
||||
Read the `Task` module documentation for more info on general
|
||||
usage of `async/1` and `async/3`.
|
||||
|
||||
See also `async/3`.
|
||||
## Task's message format
|
||||
|
||||
The reply sent by the task will be in the format `{ref, msg}`,
|
||||
where `ref` is the monitoring reference held by the task.
|
||||
"""
|
||||
@spec async(fun) :: t
|
||||
def async(fun) do
|
||||
@@ -239,10 +244,10 @@ defmodule Task do
|
||||
As before, if `heavy_fun/0` fails, the whole computation will
|
||||
fail, including the parent process. If you don't want the task
|
||||
to fail then you must change the `heavy_fun/0` code in the
|
||||
same way you would achieve it if you didn't have the async call.
|
||||
For example, to either return `{:ok, val} | :error` results or,
|
||||
same way you would if you didn't have the async call. For
|
||||
example to either return `{:ok, val} | :error` results or,
|
||||
in more extreme cases, by using `try/rescue`. In other words,
|
||||
an asynchronous task should be thought of as an extension of a
|
||||
an asynchronous task should be considered an extension of a
|
||||
process rather than a mechanism to isolate it from all errors.
|
||||
|
||||
If you don't want to link the caller to the task, then you
|
||||
@@ -251,15 +256,11 @@ defmodule Task do
|
||||
|
||||
In any case, avoid any of the following:
|
||||
|
||||
* Setting `:trap_exit` to `true` - trapping exits should be
|
||||
* Setting `:trap_exit` to true - trapping exists should be
|
||||
used only in special circumstances as it would make your
|
||||
process immune to not only exits from the task but from
|
||||
any other processes.
|
||||
|
||||
Moreover, even when trapping exits, calling `await` will
|
||||
still exit if the task has terminated without sending its
|
||||
result back.
|
||||
|
||||
* Unlinking the task process started with `async`/`await`.
|
||||
If you unlink the processes and the task does not belong
|
||||
to any supervisor, you may leave dangling tasks in case
|
||||
@@ -267,9 +268,8 @@ defmodule Task do
|
||||
|
||||
## Message format
|
||||
|
||||
The reply sent by the task will be in the format `{ref, result}`,
|
||||
where `ref` is the monitor reference held by the task struct
|
||||
and `result` is the return value of the task function.
|
||||
The reply sent by the task will be in the format `{ref, msg}`,
|
||||
where `ref` is the monitoring reference held by the task.
|
||||
"""
|
||||
@spec async(module, atom, [term]) :: t
|
||||
def async(mod, fun, args) do
|
||||
@@ -281,80 +281,6 @@ defmodule Task do
|
||||
%Task{pid: pid, ref: ref, owner: owner}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a stream that runs the given `module`, `function` and `args`
|
||||
concurrently on each item in `enumerable`.
|
||||
|
||||
Each item will be prepended to the given `args` and processed by its
|
||||
own task. The tasks will be linked to an intermediate process that is
|
||||
then linked to the current process. This means a failure in a task
|
||||
terminates the current process and a failure in the current process
|
||||
terminates all tasks.
|
||||
|
||||
When streamed, each task will emit `{:ok, val}` upon successful
|
||||
completion or `{:exit, val}` if the caller is trapping exits. Results
|
||||
are emitted in the same order as the original `enumerable`.
|
||||
|
||||
The level of concurrency can be controlled via the `:max_concurrency`
|
||||
option and defaults to `System.schedulers_online/0`. The timeout
|
||||
can also be given as option and defaults to 5000 and it defaults to
|
||||
the maximum amount of time to wait without a task reply.
|
||||
|
||||
Finally, consider using `Task.Supervisor.async_stream/6` to start tasks
|
||||
under a supervisor. If you find yourself trapping exits to handle exits
|
||||
inside the async stream, consider using `Task.Supervisor.async_stream_nolink/6`
|
||||
to start tasks that are not linked to the current process.
|
||||
|
||||
## Options
|
||||
|
||||
* `:max_concurrency` - sets the maximum number of tasks to run
|
||||
at the same time. Defaults to `System.schedulers_online/0`.
|
||||
* `:timeout` - the maximum amount of time to wait without
|
||||
receiving a task reply (across all running tasks).
|
||||
Defaults to `5000`.
|
||||
|
||||
## Example
|
||||
|
||||
Let's build a stream and then enumerate it:
|
||||
|
||||
stream = Task.async_stream(collection, Mod, :expensive_fun, [])
|
||||
Enum.to_list(stream)
|
||||
|
||||
The concurrency can be increased or decreased using the `:max_concurrency`
|
||||
option. For example, if the tasks are IO heavy, the value can be increased:
|
||||
|
||||
max_concurrency = System.schedulers_online * 2
|
||||
stream = Task.async_stream(collection, Mod, :expensive_fun, [], max_concurrency: max_concurrency)
|
||||
Enum.to_list(stream)
|
||||
|
||||
"""
|
||||
@spec async_stream(Enumerable.t, module, atom, [term], Keyword.t) :: Enumerable.t
|
||||
def async_stream(enumerable, module, function, args, options \\ [])
|
||||
when is_atom(module) and is_atom(function) and is_list(args) do
|
||||
build_stream(enumerable, {module, function, args}, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a stream that runs the given `function` concurrently on each
|
||||
item in `enumerable`.
|
||||
|
||||
Each `enumerable` item is passed as argument to the `function` and
|
||||
processed by its own task. The tasks will be linked to the current
|
||||
process, similar to `async/1`.
|
||||
|
||||
See `async_stream/5` for discussion and examples.
|
||||
"""
|
||||
@spec async_stream(Enumerable.t, (term -> term), Keyword.t) :: Enumerable.t
|
||||
def async_stream(enumerable, fun, options \\ []) when is_function(fun, 1) do
|
||||
build_stream(enumerable, fun, options)
|
||||
end
|
||||
|
||||
defp build_stream(enumerable, fun, options) do
|
||||
&Task.Supervised.stream(enumerable, &1, &2, fun, options, fn owner, mfa ->
|
||||
{:link, Task.Supervised.spawn_link(owner, get_info(owner), mfa)}
|
||||
end)
|
||||
end
|
||||
|
||||
defp get_info(self) do
|
||||
{node(),
|
||||
case Process.info(self, :registered_name) do
|
||||
@@ -364,46 +290,40 @@ defmodule Task do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Awaits a task reply and returns it.
|
||||
Awaits a task reply.
|
||||
|
||||
A timeout, in milliseconds, can be given with default value
|
||||
of `5000`. In case the task process dies, this function will
|
||||
exit with the same reason as the task.
|
||||
|
||||
If the timeout is exceeded, `await` will exit; however,
|
||||
If the timeout is exceeded, `await` will exit, however,
|
||||
the task will continue to run. When the calling process exits, its
|
||||
exit signal will terminate the task if it is not trapping exits.
|
||||
|
||||
This function assumes the task's monitor is still active or the monitor's
|
||||
`:DOWN` message is in the message queue. If it has been demonitored, or the
|
||||
message already received, this function will wait for the duration of the
|
||||
message already received, this function may wait for the duration of the
|
||||
timeout awaiting the message.
|
||||
|
||||
This function can only be called once for any given task. If you want
|
||||
to be able to check multiple times if a long-running task has finished
|
||||
its computation, use `yield/2` instead.
|
||||
|
||||
## Compatibility with OTP behaviours
|
||||
|
||||
It is not recommended to `await` a long-running task inside an OTP
|
||||
behaviour such as `GenServer`. Instead, you should match on the message
|
||||
coming from a task inside your `GenServer.handle_info/2` callback.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> task = Task.async(fn -> 1 + 1 end)
|
||||
iex> Task.await(task)
|
||||
2
|
||||
|
||||
This function will always exit and demonitor if the task crashes or if
|
||||
it times out, so the task can not be used again. To explicitly handle
|
||||
the timeout or the crash, use `yield/2` instead.
|
||||
"""
|
||||
@spec await(t, timeout) :: term | no_return
|
||||
def await(task, timeout \\ 5000)
|
||||
|
||||
def await(%Task{owner: owner} = task, _) when owner != self() do
|
||||
# TODO: Remove nil check in Elixir 1.3
|
||||
def await(%Task{owner: owner}=task, _) when owner != nil and owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
end
|
||||
|
||||
def await(%Task{ref: ref} = task, timeout) do
|
||||
def await(%Task{ref: ref, owner: owner}=task, timeout) do
|
||||
if is_nil(owner) do
|
||||
IO.write :stderr, "warning: a Task was created with the :owner field no set, " <>
|
||||
"please ensure the owner field is correctly set to self()\n" <>
|
||||
Exception.format_stacktrace
|
||||
end
|
||||
|
||||
receive do
|
||||
{^ref, reply} ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
@@ -417,14 +337,25 @@ defmodule Task do
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove on 2.0
|
||||
def find(tasks, msg) do
|
||||
IO.warn "Task.find/2 is deprecated, please match on the message directly"
|
||||
do_find(tasks, msg)
|
||||
end
|
||||
@doc """
|
||||
Receives a group of tasks and a message and finds
|
||||
a task that matches the given message.
|
||||
|
||||
defp do_find(tasks, {ref, reply}) when is_reference(ref) do
|
||||
This function returns a tuple with the returned value
|
||||
in case the message matches a task that exited with
|
||||
success alongside the matching task. It returns `nil`
|
||||
if no task was found. It exits if the task has failed.
|
||||
|
||||
This function is useful in situations where multiple
|
||||
tasks are spawned and their results are collected
|
||||
later on. For example, a `GenServer` can spawn tasks,
|
||||
store the tasks in a list and later use `Task.find/2`
|
||||
to see if incoming messages are from any of the tasks.
|
||||
"""
|
||||
@spec find([t], any) :: {term, t} | nil | no_return
|
||||
def find(tasks, msg)
|
||||
|
||||
def find(tasks, {ref, reply}) when is_reference(ref) do
|
||||
Enum.find_value tasks, fn
|
||||
%Task{ref: ^ref} = task ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
@@ -434,64 +365,51 @@ defmodule Task do
|
||||
end
|
||||
end
|
||||
|
||||
defp do_find(tasks, {:DOWN, ref, _, proc, reason} = msg) when is_reference(ref) do
|
||||
def find(tasks, {:DOWN, ref, _, proc, reason} = msg) when is_reference(ref) do
|
||||
find = fn %Task{ref: task_ref} -> task_ref == ref end
|
||||
if Enum.find(tasks, find) do
|
||||
exit({reason(reason, proc), {__MODULE__, :find, [tasks, msg]}})
|
||||
end
|
||||
end
|
||||
|
||||
defp do_find(_tasks, _msg) do
|
||||
def find(_tasks, _msg) do
|
||||
nil
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Temporarily blocks the current process waiting for a task reply.
|
||||
@doc """
|
||||
Yields for a task reply in the given time interval.
|
||||
|
||||
Returns `{:ok, reply}` if the reply is received, `nil` if
|
||||
no reply has arrived, or `{:exit, reason}` if the task has already
|
||||
exited. Keep in mind that normally a task failure also causes
|
||||
the process owning the task to exit. Therefore this function can
|
||||
return `{:exit, reason}` only if
|
||||
|
||||
* the task process exited with the reason `:normal`
|
||||
* it isn't linked to the caller
|
||||
* the caller is trapping exits
|
||||
Returns `{:ok, reply}` if the reply is received, `{:exit, reason}`
|
||||
if the task exited or `nil` if no reply arrived.
|
||||
|
||||
A timeout, in milliseconds, can be given with default value
|
||||
of `5000`. If the time runs out before a message from
|
||||
the task is received, this function will return `nil`
|
||||
of `5000`. In case of the timeout, this function will return `nil`
|
||||
and the monitor will remain active. Therefore `yield/2` can be
|
||||
called multiple times on the same task.
|
||||
|
||||
In case the task process dies, this function will exit with the
|
||||
same reason as the task.
|
||||
|
||||
This function assumes the task's monitor is still active or the
|
||||
monitor's `:DOWN` message is in the message queue. If it has been
|
||||
demonitored or the message already received, this function will wait
|
||||
demonitored, or the message already received, this function waits
|
||||
for the duration of the timeout awaiting the message.
|
||||
|
||||
If you intend to shut the task down if it has not responded within `timeout`
|
||||
milliseconds, you should chain this together with `shutdown/1`, like so:
|
||||
|
||||
case Task.yield(task, timeout) || Task.shutdown(task) do
|
||||
{:ok, result} ->
|
||||
result
|
||||
nil ->
|
||||
Logger.warn "Failed to get a result in #{timeout}ms"
|
||||
nil
|
||||
end
|
||||
|
||||
That ensures that if the task completes after the `timeout` but before `shutdown/1`
|
||||
has been called, you will still get the result, since `shutdown/1` is designed to
|
||||
handle this case and return the result.
|
||||
"""
|
||||
@spec yield(t, timeout) :: {:ok, term} | {:exit, term} | nil
|
||||
def yield(task, timeout \\ 5_000)
|
||||
|
||||
def yield(%Task{owner: owner} = task, _) when owner != self() do
|
||||
# TODO: Remove nil check in Elixir 1.3
|
||||
def yield(%Task{owner: owner} = task, _) when owner != nil and owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
end
|
||||
|
||||
def yield(%Task{ref: ref} = task, timeout) do
|
||||
def yield(%Task{ref: ref, owner: owner} = task, timeout) do
|
||||
if is_nil(owner) do
|
||||
IO.write :stderr, "warning: a Task was created with the :owner field no set, " <>
|
||||
"please ensure the owner field is correctly set to self()\n" <>
|
||||
Exception.format_stacktrace
|
||||
end
|
||||
|
||||
receive do
|
||||
{^ref, reply} ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
@@ -509,19 +427,14 @@ defmodule Task do
|
||||
@doc """
|
||||
Yields to multiple tasks in the given time interval.
|
||||
|
||||
This function receives a list of tasks and waits for their
|
||||
replies in the given time interval. It returns a list
|
||||
of tuples of two elements, with the task as the first element
|
||||
and the yielded result as the second.
|
||||
This function receives a list of tasks and await for their
|
||||
replies at once in the given time interval. It returns a list
|
||||
of tuples of two elements, with tasks as the first element and
|
||||
the `yield` result as the second.
|
||||
|
||||
Similarly to `yield/2`, each task's result will be
|
||||
|
||||
* `{:ok, term}` if the task has successfully reported its
|
||||
result back in the given time interval
|
||||
* `{:exit, reason}` if the task has died
|
||||
* `nil` if the task keeps running past the timeout
|
||||
|
||||
Check `yield/2` for more information.
|
||||
Similar to `yield/2`, if the task replied in the given interval,
|
||||
it will return `{:ok, term}`, `{:exit, reason}`if it crashed or
|
||||
`nil` if it timed out. Check `yield/2` for more information.
|
||||
|
||||
## Example
|
||||
|
||||
@@ -529,13 +442,12 @@ defmodule Task do
|
||||
and retrieve the results received in a given timeframe.
|
||||
If we combine it with `Task.shutdown/2`, it allows us to gather
|
||||
those results and cancel the tasks that have not replied in time.
|
||||
|
||||
Let's see an example.
|
||||
|
||||
tasks =
|
||||
for i <- 1..10 do
|
||||
Task.async(fn ->
|
||||
Process.sleep(i * 1000)
|
||||
:timer.sleep(i * 1000)
|
||||
i
|
||||
end)
|
||||
end
|
||||
@@ -557,8 +469,8 @@ defmodule Task do
|
||||
up to 10 seconds and return the amount of seconds they slept.
|
||||
If you execute the code all at once, you should see 1 up to 5
|
||||
printed, as those were the tasks that have replied in the
|
||||
given time. All other tasks will have been shut down using
|
||||
the `Task.shutdown/2` call.
|
||||
given time. All other tasks will have been shutdown, according
|
||||
to the `Task.shutdown/2` call.
|
||||
"""
|
||||
@spec yield_many([t], timeout) :: [{t, {:ok, term} | {:exit, term} | nil}]
|
||||
def yield_many(tasks, timeout \\ 5000) do
|
||||
@@ -575,7 +487,7 @@ defmodule Task do
|
||||
end
|
||||
end
|
||||
|
||||
defp yield_many([%Task{ref: ref, owner: owner}=task | rest], timeout_ref, timeout) do
|
||||
defp yield_many([%Task{ref: ref, owner: owner}=task|rest], timeout_ref, timeout) do
|
||||
if owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
end
|
||||
@@ -583,20 +495,20 @@ defmodule Task do
|
||||
receive do
|
||||
{^ref, reply} ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
[{task, {:ok, reply}} | yield_many(rest, timeout_ref, timeout)]
|
||||
[{task, {:ok, reply}}|yield_many(rest, timeout_ref, timeout)]
|
||||
|
||||
{:DOWN, ^ref, _, proc, :noconnection} ->
|
||||
throw({:noconnection, reason(:noconnection, proc)})
|
||||
|
||||
{:DOWN, ^ref, _, _, reason} ->
|
||||
[{task, {:exit, reason}} | yield_many(rest, timeout_ref, timeout)]
|
||||
[{task, {:exit, reason}}|yield_many(rest, timeout_ref, timeout)]
|
||||
|
||||
^timeout_ref ->
|
||||
[{task, nil} | yield_many(rest, timeout_ref, 0)]
|
||||
[{task, nil}|yield_many(rest, timeout_ref, 0)]
|
||||
|
||||
after
|
||||
timeout ->
|
||||
[{task, nil} | yield_many(rest, timeout_ref, 0)]
|
||||
[{task, nil}|yield_many(rest, timeout_ref, 0)]
|
||||
|
||||
end
|
||||
end
|
||||
@@ -606,27 +518,27 @@ defmodule Task do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Unlinks and shuts down the task, and then checks for a reply.
|
||||
Unlinks and shutdowns the task, and then checks for a reply.
|
||||
|
||||
Returns `{:ok, reply}` if the reply is received while shutting down the task,
|
||||
`{:exit, reason}` if the task died, otherwise `nil`.
|
||||
`{:exit, reason}` if the task exited abornormally, otherwise `nil`.
|
||||
|
||||
The shutdown method is either a timeout or `:brutal_kill`. In case
|
||||
of a `timeout`, a `:shutdown` exit signal is sent to the task process
|
||||
and if it does not exit within the timeout, it is killed. With `:brutal_kill`
|
||||
the task is killed straight away. In case the task terminates abnormally
|
||||
(possibly killed by another process), this function will exit with the same reason.
|
||||
and if it does not exit within the timeout it is killed. With `:brutal_kill`
|
||||
the task is killed straight away. In case the task exits abnormally, or a
|
||||
timeout shutdown kills the task, this function will exit with the same reason.
|
||||
|
||||
It is not required to call this function when terminating the caller, unless
|
||||
exiting with reason `:normal` or if the task is trapping exits. If the caller is
|
||||
exiting with a reason other than `:normal` and the task is not trapping exits, the
|
||||
exiting with reason `:normal` or the task is trapping exits. If the caller is
|
||||
exiting with a reason other than `:normal` and the task is not trapping exits the
|
||||
caller's exit signal will stop the task. The caller can exit with reason
|
||||
`:shutdown` to shutdown all of its linked processes, including tasks, that
|
||||
are not trapping exits without generating any log messages.
|
||||
`:shutdown` to shutdown linked processes, such as tasks, that are not trapping
|
||||
exits without generating any log messages.
|
||||
|
||||
If a task's monitor has already been demonitored or received and there is not
|
||||
a response waiting in the message queue this function will return
|
||||
`{:exit, :noproc}` as the result or exit reason can not be determined.
|
||||
This function assumes the task's monitor is still active or the monitor's
|
||||
`:DOWN` message is in the message queue. If it has been demonitored, or the
|
||||
message already received, this function will block forever awaiting the message.
|
||||
"""
|
||||
@spec shutdown(t, timeout | :brutal_kill) :: {:ok, term} | {:exit, term} | nil
|
||||
def shutdown(task, shutdown \\ 5_000)
|
||||
@@ -635,15 +547,21 @@ defmodule Task do
|
||||
raise ArgumentError, "task #{inspect task} does not have an associated task process"
|
||||
end
|
||||
|
||||
def shutdown(%Task{owner: owner} = task, _) when owner != self() do
|
||||
# TODO: Remove nil check in Elixir 1.3
|
||||
def shutdown(%Task{owner: owner} = task, _) when owner != nil and owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
end
|
||||
|
||||
def shutdown(%Task{pid: pid} = task, :brutal_kill) do
|
||||
mon = Process.monitor(pid)
|
||||
def shutdown(%Task{pid: pid, owner: owner} = task, :brutal_kill) do
|
||||
if is_nil(owner) do
|
||||
IO.write :stderr, "warning: a Task was created with the :owner field no set, " <>
|
||||
"please ensure the owner field is correctly set to self()\n" <>
|
||||
Exception.format_stacktrace
|
||||
end
|
||||
|
||||
exit(pid, :kill)
|
||||
|
||||
case shutdown_receive(task, mon, :brutal_kill, :infinity) do
|
||||
case shutdown_receive(task, :brutal_kill, :infinity) do
|
||||
{:down, proc, :noconnection} ->
|
||||
exit({reason(:noconnection, proc), {__MODULE__, :shutdown, [task, :brutal_kill]}})
|
||||
{:down, _, reason} ->
|
||||
@@ -654,9 +572,8 @@ defmodule Task do
|
||||
end
|
||||
|
||||
def shutdown(%Task{pid: pid} = task, timeout) do
|
||||
mon = Process.monitor(pid)
|
||||
exit(pid, :shutdown)
|
||||
case shutdown_receive(task, mon, :shutdown, timeout) do
|
||||
case shutdown_receive(task, :shutdown, timeout) do
|
||||
{:down, proc, :noconnection} ->
|
||||
exit({reason(:noconnection, proc), {__MODULE__, :shutdown, [task, timeout]}})
|
||||
{:down, _, reason} ->
|
||||
@@ -694,24 +611,18 @@ defmodule Task do
|
||||
end
|
||||
end
|
||||
|
||||
defp shutdown_receive(%{ref: ref} = task, mon, type, timeout) do
|
||||
defp shutdown_receive(%{ref: ref} = task, type, timeout) do
|
||||
receive do
|
||||
{:DOWN, ^mon, _, _, :shutdown} when type in [:shutdown, :timeout_kill] ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
{:DOWN, ^ref, _, _, :shutdown} when type in [:shutdown, :timeout_kill] ->
|
||||
flush_reply(ref)
|
||||
{:DOWN, ^mon, _, _, :killed} when type == :brutal_kill ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
{:DOWN, ^ref, _, _, :killed} when type == :brutal_kill ->
|
||||
flush_reply(ref)
|
||||
{:DOWN, ^mon, _, proc, :noproc} ->
|
||||
reason = flush_noproc(ref, proc, type)
|
||||
flush_reply(ref) || reason
|
||||
{:DOWN, ^mon, _, proc, reason} ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
{:DOWN, ^ref, _, proc, reason} ->
|
||||
flush_reply(ref) || {:down, proc, reason}
|
||||
after
|
||||
timeout ->
|
||||
Process.exit(task.pid, :kill)
|
||||
shutdown_receive(task, mon, :timeout_kill, :infinity)
|
||||
shutdown_receive(task, :timeout_kill, :infinity)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -723,21 +634,6 @@ defmodule Task do
|
||||
end
|
||||
end
|
||||
|
||||
defp flush_noproc(ref, proc, type) do
|
||||
receive do
|
||||
{:DOWN, ^ref, _, _, :shutdown} when type in [:shutdown, :timeout_kill] ->
|
||||
nil
|
||||
{:DOWN, ^ref, _, _, :killed} when type == :brutal_kill ->
|
||||
nil
|
||||
{:DOWN, ^ref, _, _, reason} ->
|
||||
{:down, proc, reason}
|
||||
after
|
||||
0 ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
{:down, proc, :noproc}
|
||||
end
|
||||
end
|
||||
|
||||
defp invalid_owner_error(task) do
|
||||
"task #{inspect task} must be queried from the owner but was queried from #{inspect self()}"
|
||||
end
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
defmodule Task.Supervised do
|
||||
@moduledoc false
|
||||
|
||||
@ref_timeout 5_000
|
||||
|
||||
def start(info, fun) do
|
||||
@@ -10,21 +11,24 @@ defmodule Task.Supervised do
|
||||
{:ok, :proc_lib.spawn_link(__MODULE__, :noreply, [info, fun])}
|
||||
end
|
||||
|
||||
def start_link(caller, monitor, info, fun) do
|
||||
{:ok, spawn_link(caller, monitor, info, fun)}
|
||||
def start_link(caller, link, info, fun) do
|
||||
{:ok, spawn_link(caller, link, info, fun)}
|
||||
end
|
||||
|
||||
def spawn_link(caller, monitor \\ :nomonitor, info, fun) do
|
||||
:proc_lib.spawn_link(__MODULE__, :reply, [caller, monitor, info, fun])
|
||||
def spawn_link(caller, link \\ :nolink, info, fun) do
|
||||
:proc_lib.spawn_link(__MODULE__, :reply, [caller, link, info, fun])
|
||||
end
|
||||
|
||||
def reply(caller, monitor, info, mfa) do
|
||||
def reply(caller, link, info, mfa) do
|
||||
initial_call(mfa)
|
||||
case monitor do
|
||||
case link do
|
||||
:link ->
|
||||
Process.link(caller)
|
||||
reply(caller, nil, @ref_timeout, info, mfa)
|
||||
:monitor ->
|
||||
mref = Process.monitor(caller)
|
||||
reply(caller, mref, @ref_timeout, info, mfa)
|
||||
:nomonitor ->
|
||||
:nolink ->
|
||||
reply(caller, nil, :infinity, info, mfa)
|
||||
end
|
||||
end
|
||||
@@ -35,7 +39,7 @@ defmodule Task.Supervised do
|
||||
_ = if mref, do: Process.demonitor(mref, [:flush])
|
||||
send caller, {ref, do_apply(info, mfa)}
|
||||
{:DOWN, ^mref, _, _, reason} when is_reference(mref) ->
|
||||
exit({:shutdown, reason})
|
||||
exit(reason)
|
||||
after
|
||||
# There is a race condition on this operation when working across
|
||||
# node that manifests if a "Task.Supervisor.async/2" call is made
|
||||
@@ -96,9 +100,9 @@ defmodule Task.Supervised do
|
||||
end
|
||||
|
||||
defp exit(_info, _mfa, _log_reason, reason)
|
||||
when reason == :normal
|
||||
when reason == :shutdown
|
||||
when tuple_size(reason) == 2 and elem(reason, 0) == :shutdown do
|
||||
when reason == :normal
|
||||
when reason == :shutdown
|
||||
when tuple_size(reason) == 2 and elem(reason, 0) == :shutdown do
|
||||
exit(reason)
|
||||
end
|
||||
|
||||
@@ -111,7 +115,7 @@ defmodule Task.Supervised do
|
||||
'** When function == ~p~n' ++
|
||||
'** arguments == ~p~n' ++
|
||||
'** Reason for termination == ~n' ++
|
||||
'** ~p~n', [self(), get_from(info), fun, args, get_reason(log_reason)])
|
||||
'** ~p~n', [self, get_from(info), fun, args, get_reason(log_reason)])
|
||||
|
||||
exit(reason)
|
||||
end
|
||||
@@ -123,7 +127,7 @@ defmodule Task.Supervised do
|
||||
defp get_running({mod, fun, args}), do: {:erlang.make_fun(mod, fun, length(args)), args}
|
||||
|
||||
defp get_reason({:undef, [{mod, fun, args, _info} | _] = stacktrace} = reason)
|
||||
when is_atom(mod) and is_atom(fun) do
|
||||
when is_atom(mod) and is_atom(fun) do
|
||||
cond do
|
||||
:code.is_loaded(mod) === false ->
|
||||
{:"module could not be loaded", stacktrace}
|
||||
@@ -139,221 +143,4 @@ defmodule Task.Supervised do
|
||||
defp get_reason(reason) do
|
||||
reason
|
||||
end
|
||||
|
||||
## Stream
|
||||
|
||||
def stream(enumerable, acc, reducer, mfa, options, spawn) do
|
||||
next = &Enumerable.reduce(enumerable, &1, fn x, acc -> {:suspend, [x | acc]} end)
|
||||
max_concurrency = Keyword.get(options, :max_concurrency, System.schedulers_online)
|
||||
timeout = Keyword.get(options, :timeout, 5000)
|
||||
parent = self()
|
||||
|
||||
# Start a process responsible for translating down messages.
|
||||
{:trap_exit, trap_exit} =
|
||||
Process.info(self(), :trap_exit)
|
||||
{monitor_pid, monitor_ref} =
|
||||
Process.spawn(fn -> stream_monitor(parent, mfa, spawn, trap_exit) end, [:link, :monitor])
|
||||
|
||||
send(monitor_pid, {parent, monitor_ref})
|
||||
|
||||
stream_reduce(acc, max_concurrency, 0, 0, %{}, next,
|
||||
reducer, monitor_pid, monitor_ref, timeout)
|
||||
end
|
||||
|
||||
defp stream_reduce({:halt, acc}, _max, _spawned, _delivered, _waiting, next,
|
||||
_reducer, monitor_pid, monitor_ref, timeout) do
|
||||
stream_close(monitor_pid, monitor_ref, timeout)
|
||||
is_function(next) && next.({:halt, []})
|
||||
{:halted, acc}
|
||||
end
|
||||
|
||||
defp stream_reduce({:suspend, acc}, max, spawned, delivered, waiting, next,
|
||||
reducer, monitor_pid, monitor_ref, timeout) do
|
||||
{:suspended, acc, &stream_reduce(&1, max, spawned, delivered, waiting, next,
|
||||
reducer, monitor_pid, monitor_ref, timeout)}
|
||||
end
|
||||
|
||||
# All spawned, all delivered, next is done.
|
||||
defp stream_reduce({:cont, acc}, _max, spawned, delivered, _waiting, next,
|
||||
_reducer, monitor_pid, monitor_ref, timeout)
|
||||
when spawned == delivered and next == :done do
|
||||
stream_close(monitor_pid, monitor_ref, timeout)
|
||||
{:done, acc}
|
||||
end
|
||||
|
||||
# No more tasks to spawned because max == 0 or next is done.
|
||||
defp stream_reduce({:cont, acc}, max, spawned, delivered, waiting, next,
|
||||
reducer, monitor_pid, monitor_ref, timeout)
|
||||
when max == 0
|
||||
when next == :done do
|
||||
receive do
|
||||
{{^monitor_ref, position}, value} ->
|
||||
%{^position => {pid, :running}} = waiting
|
||||
waiting = Map.put(waiting, position, {pid, {:ok, value}})
|
||||
stream_reduce({:cont, acc}, max, spawned, delivered, waiting, next,
|
||||
reducer, monitor_pid, monitor_ref, timeout)
|
||||
{:down, {^monitor_ref, position}, reason} ->
|
||||
waiting =
|
||||
case waiting do
|
||||
%{^position => {_, {:ok, _} = ok}} -> Map.put(waiting, position, {nil, ok})
|
||||
%{^position => {_, :running}} -> Map.put(waiting, position, {nil, {:exit, reason}})
|
||||
end
|
||||
stream_deliver({:cont, acc}, max + 1, spawned, delivered, waiting, next,
|
||||
reducer, monitor_pid, monitor_ref, timeout)
|
||||
{:DOWN, ^monitor_ref, _, ^monitor_pid, reason} ->
|
||||
stream_cleanup_inbox(monitor_pid, monitor_ref)
|
||||
exit({reason, {__MODULE__, :stream, [timeout]}})
|
||||
after
|
||||
timeout ->
|
||||
stream_close(monitor_pid, monitor_ref, timeout)
|
||||
exit({:timeout, {__MODULE__, :stream, [timeout]}})
|
||||
end
|
||||
end
|
||||
|
||||
defp stream_reduce({:cont, acc}, max, spawned, delivered, waiting, next,
|
||||
reducer, monitor_pid, monitor_ref, timeout) do
|
||||
try do
|
||||
next.({:cont, []})
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
stream_close(monitor_pid, monitor_ref, timeout)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
else
|
||||
{:suspended, [value], next} ->
|
||||
waiting = stream_spawn(value, spawned, waiting, monitor_pid, monitor_ref, timeout)
|
||||
stream_reduce({:cont, acc}, max - 1, spawned + 1, delivered, waiting, next,
|
||||
reducer, monitor_pid, monitor_ref, timeout)
|
||||
{_, [value]} ->
|
||||
waiting = stream_spawn(value, spawned, waiting, monitor_pid, monitor_ref, timeout)
|
||||
stream_reduce({:cont, acc}, max - 1, spawned + 1, delivered, waiting, :done,
|
||||
reducer, monitor_pid, monitor_ref, timeout)
|
||||
{_, []} ->
|
||||
stream_reduce({:cont, acc}, max, spawned, delivered, waiting, :done,
|
||||
reducer, monitor_pid, monitor_ref, timeout)
|
||||
end
|
||||
end
|
||||
|
||||
defp stream_deliver({:suspend, acc}, max, spawned, delivered, waiting, next,
|
||||
reducer, monitor_pid, monitor_ref, timeout) do
|
||||
{:suspended, acc, &stream_deliver(&1, max, spawned, delivered, waiting, next,
|
||||
reducer, monitor_pid, monitor_ref, timeout)}
|
||||
end
|
||||
defp stream_deliver({:halt, acc}, max, spawned, delivered, waiting, next,
|
||||
reducer, monitor_pid, monitor_ref, timeout) do
|
||||
stream_reduce({:halt, acc}, max, spawned, delivered, waiting, next,
|
||||
reducer, monitor_pid, monitor_ref, timeout)
|
||||
end
|
||||
defp stream_deliver({:cont, acc}, max, spawned, delivered, waiting, next,
|
||||
reducer, monitor_pid, monitor_ref, timeout) do
|
||||
case waiting do
|
||||
%{^delivered => {nil, reply}} ->
|
||||
try do
|
||||
reducer.(reply, acc)
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
is_function(next) && next.({:halt, []})
|
||||
stream_close(monitor_pid, monitor_ref, timeout)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
else
|
||||
pair ->
|
||||
stream_deliver(pair, max, spawned, delivered + 1, Map.delete(waiting, delivered), next,
|
||||
reducer, monitor_pid, monitor_ref, timeout)
|
||||
end
|
||||
%{} ->
|
||||
stream_reduce({:cont, acc}, max, spawned, delivered, waiting, next,
|
||||
reducer, monitor_pid, monitor_ref, timeout)
|
||||
end
|
||||
end
|
||||
|
||||
defp stream_close(monitor_pid, monitor_ref, timeout) do
|
||||
send(monitor_pid, {:stop, monitor_ref})
|
||||
receive do
|
||||
{:DOWN, ^monitor_ref, _, _, :normal} ->
|
||||
stream_cleanup_inbox(monitor_pid, monitor_ref)
|
||||
:ok
|
||||
{:DOWN, ^monitor_ref, _, _, reason} ->
|
||||
stream_cleanup_inbox(monitor_pid, monitor_ref)
|
||||
exit({reason, {__MODULE__, :stream, [timeout]}})
|
||||
end
|
||||
end
|
||||
|
||||
defp stream_cleanup_inbox(monitor_pid, monitor_ref) do
|
||||
receive do
|
||||
{:EXIT, ^monitor_pid, _} -> stream_cleanup_inbox(monitor_ref)
|
||||
after
|
||||
0 -> stream_cleanup_inbox(monitor_ref)
|
||||
end
|
||||
end
|
||||
|
||||
defp stream_cleanup_inbox(monitor_ref) do
|
||||
receive do
|
||||
{{^monitor_ref, _}, _} ->
|
||||
stream_cleanup_inbox(monitor_ref)
|
||||
{:down, {^monitor_ref, _}, _} ->
|
||||
stream_cleanup_inbox(monitor_ref)
|
||||
after
|
||||
0 ->
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
defp stream_mfa({mod, fun, args}, arg), do: {mod, fun, [arg | args]}
|
||||
defp stream_mfa(fun, arg), do: {:erlang, :apply, [fun, [arg]]}
|
||||
|
||||
defp stream_spawn(value, spawned, waiting, monitor_pid, monitor_ref, timeout) do
|
||||
send(monitor_pid, {:spawn, spawned, value})
|
||||
|
||||
receive do
|
||||
{:spawned, {^monitor_ref, ^spawned}, pid} ->
|
||||
send(pid, {self(), {monitor_ref, spawned}})
|
||||
Map.put(waiting, spawned, {pid, :running})
|
||||
{:DOWN, ^monitor_ref, _, ^monitor_pid, reason} ->
|
||||
stream_cleanup_inbox(monitor_pid, monitor_ref)
|
||||
exit({reason, {__MODULE__, :stream, [timeout]}})
|
||||
end
|
||||
end
|
||||
|
||||
defp stream_monitor(parent_pid, mfa, spawn, trap_exit) do
|
||||
Process.flag(:trap_exit, trap_exit)
|
||||
parent_ref = Process.monitor(parent_pid)
|
||||
receive do
|
||||
{^parent_pid, monitor_ref} ->
|
||||
stream_monitor(parent_pid, parent_ref, mfa, spawn, monitor_ref, %{})
|
||||
{:DOWN, ^parent_ref, _, _, reason} ->
|
||||
exit(reason)
|
||||
end
|
||||
end
|
||||
|
||||
defp stream_monitor(parent_pid, parent_ref, mfa, spawn, monitor_ref, counters) do
|
||||
receive do
|
||||
{:spawn, counter, value} ->
|
||||
{type, pid} = spawn.(parent_pid, stream_mfa(mfa, value))
|
||||
ref = Process.monitor(pid)
|
||||
send(parent_pid, {:spawned, {monitor_ref, counter}, pid})
|
||||
counters = Map.put(counters, ref, {counter, type, pid})
|
||||
stream_monitor(parent_pid, parent_ref, mfa, spawn, monitor_ref, counters)
|
||||
{:stop, ^monitor_ref} ->
|
||||
Process.flag(:trap_exit, true)
|
||||
for {ref, {_counter, _, pid}} <- counters do
|
||||
Process.exit(pid, :kill)
|
||||
receive do
|
||||
{:DOWN, ^ref, _, _, _} -> :ok
|
||||
end
|
||||
end
|
||||
exit(:normal)
|
||||
{:DOWN, ^parent_ref, _, _, reason} ->
|
||||
for {_ref, {_counter, :link, pid}} <- counters do
|
||||
Process.exit(pid, reason)
|
||||
end
|
||||
exit(reason)
|
||||
{:DOWN, ref, _, _, reason} ->
|
||||
{{counter, _, _}, counters} = Map.pop(counters, ref)
|
||||
send(parent_pid, {:down, {monitor_ref, counter}, reason})
|
||||
stream_monitor(parent_pid, parent_ref, mfa, spawn, monitor_ref, counters)
|
||||
{:EXIT, _, _} ->
|
||||
stream_monitor(parent_pid, parent_ref, mfa, spawn, monitor_ref, counters)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,14 +4,12 @@ defmodule Task.Supervisor do
|
||||
|
||||
This module defines a supervisor which can be used to dynamically
|
||||
supervise tasks. Behind the scenes, this module is implemented as a
|
||||
`:simple_one_for_one` supervisor where the workers are temporary by
|
||||
default (that is, they are not restarted after they die; read the docs
|
||||
for `start_link/1` for more information on choosing the restart
|
||||
strategy).
|
||||
`:simple_one_for_one` supervisor where the workers are temporary
|
||||
(i.e. they are not restarted after they die).
|
||||
|
||||
See the `Task` module for more information.
|
||||
|
||||
## Name registration
|
||||
## Name Registration
|
||||
|
||||
A `Task.Supervisor` is bound to the same name registration rules as a
|
||||
`GenServer`. Read more about them in the `GenServer` docs.
|
||||
@@ -28,8 +26,8 @@ defmodule Task.Supervisor do
|
||||
|
||||
* `:restart` - the restart strategy, may be `:temporary` (the default),
|
||||
`:transient` or `:permanent`. Check `Supervisor.Spec` for more info.
|
||||
Defaults to `:temporary` so tasks aren't automatically restarted when
|
||||
they complete nor in case of crashes;
|
||||
Defaults to `:temporary` as most tasks can't be effectively restarted after
|
||||
a crash;
|
||||
|
||||
* `:shutdown` - `:brutal_kill` if the tasks must be killed directly on shutdown
|
||||
or an integer indicating the timeout value, defaults to 5000 milliseconds;
|
||||
@@ -67,7 +65,13 @@ defmodule Task.Supervisor do
|
||||
"""
|
||||
@spec async(Supervisor.supervisor, module, atom, [term]) :: Task.t
|
||||
def async(supervisor, module, fun, args) do
|
||||
do_async(supervisor, :link, module, fun, args)
|
||||
owner = self()
|
||||
args = [owner, :link, get_info(owner), {module, fun, args}]
|
||||
{:ok, pid} = Supervisor.start_child(supervisor, args)
|
||||
Process.link(pid)
|
||||
ref = Process.monitor(pid)
|
||||
send pid, {owner, ref}
|
||||
%Task{pid: pid, ref: ref, owner: owner}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -76,21 +80,6 @@ defmodule Task.Supervisor do
|
||||
The `supervisor` must be a reference as defined in `Task.Supervisor`.
|
||||
The task won't be linked to the caller, see `Task.async/3` for
|
||||
more information.
|
||||
|
||||
## Compatibility with OTP behaviours
|
||||
|
||||
If you create a task using `async_nolink` inside an OTP behaviour
|
||||
like `GenServer`, you should match on the message coming from the
|
||||
task inside your `GenServer.handle_info/2` callback.
|
||||
|
||||
The reply sent by the task will be in the format `{ref, result}`,
|
||||
where `ref` is the monitor reference held by the task struct
|
||||
and `result` is the return value of the task function.
|
||||
|
||||
Keep in mind that, regardless of how the task created with `async_nolink`
|
||||
terminates, the caller's process will always receive a `:DOWN` message
|
||||
with the same `ref` value that is held by the task struct. If the task
|
||||
terminates normally, the reason in the `:DOWN` message will be `:normal`.
|
||||
"""
|
||||
@spec async_nolink(Supervisor.supervisor, fun) :: Task.t
|
||||
def async_nolink(supervisor, fun) do
|
||||
@@ -106,99 +95,12 @@ defmodule Task.Supervisor do
|
||||
"""
|
||||
@spec async_nolink(Supervisor.supervisor, module, atom, [term]) :: Task.t
|
||||
def async_nolink(supervisor, module, fun, args) do
|
||||
do_async(supervisor, :nolink, module, fun, args)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a stream that runs the given `module`, `function` and `args`
|
||||
concurrently on each item in `enumerable`.
|
||||
|
||||
Each item will be appended to the given `args` and processed by its
|
||||
own task. The tasks will be spawned under the given `supervisor` and
|
||||
linked to the current process, similar to `async/4`.
|
||||
|
||||
When streamed, each task will emit `{:ok, val}` upon successful
|
||||
completion or `{:exit, val}` if the caller is trapping exits. Results
|
||||
are emitted in the same order as the original `enumerable`.
|
||||
|
||||
The level of concurrency can be controlled via the `:max_concurrency`
|
||||
option and defaults to `System.schedulers_online/0`. The timeout
|
||||
can also be given as option and defaults to 5000 and it defaults to
|
||||
the maximum amount of time to wait without a task reply.
|
||||
|
||||
Finally, if you find yourself trapping exits to handle exits inside
|
||||
the async stream, consider using `async_stream_nolink/6` to start tasks
|
||||
that are not linked to the current process.
|
||||
|
||||
## Options
|
||||
|
||||
* `:max_concurrency` - sets the maximum number of tasks to run
|
||||
at the same time. Defaults to `System.schedulers_online/0`.
|
||||
* `:timeout` - the maximum amount of time to wait without
|
||||
receiving a task reply (across all running tasks).
|
||||
Defaults to `5000`.
|
||||
|
||||
## Examples
|
||||
|
||||
Let's build a stream and then enumerate it:
|
||||
|
||||
stream = Task.Supervisor.async_stream(MySupervisor, collection, Mod, :expensive_fun, [])
|
||||
Enum.to_list(stream)
|
||||
"""
|
||||
@spec async_stream(Supervisor.supervisor, Enumerable.t, module, atom, [term], Keyword.t) ::
|
||||
Enumerable.t
|
||||
def async_stream(supervisor, enumerable, module, function, args, options \\ [])
|
||||
when is_atom(module) and is_atom(function) and is_list(args) do
|
||||
build_stream(supervisor, :link, enumerable, {module, function, args}, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a stream that runs the given `function` concurrently on each
|
||||
item in `enumerable`.
|
||||
|
||||
Each item will be appended to the given `args` and processed by its
|
||||
own task. The tasks will be spawned under the given `supervisor` and
|
||||
are linked to the current process, similar to `async/2`.
|
||||
|
||||
See `async_stream/6` for discussion and examples.
|
||||
"""
|
||||
@spec async_stream(Supervisor.supervisor, Enumerable.t, (term -> term), Keyword.t) ::
|
||||
Enumerable.t
|
||||
def async_stream(supervisor, enumerable, fun, options \\ []) when is_function(fun, 1) do
|
||||
build_stream(supervisor, :link, enumerable, fun, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a stream that runs the given `module`, `function` and `args`
|
||||
concurrently on each item in `enumerable`.
|
||||
|
||||
Each item will be appended to the given `args` and processed by its
|
||||
own task. The tasks will be spawned under the given `supervisor` and
|
||||
are not linked to the current process, similar to `async_nolink/4`.
|
||||
|
||||
See `async_stream/6` for discussion and examples.
|
||||
"""
|
||||
@spec async_stream_nolink(Supervisor.supervisor, Enumerable.t, module, atom, [term], Keyword.t) ::
|
||||
Enumerable.t
|
||||
def async_stream_nolink(supervisor, enumerable, module, function, args, options \\ [])
|
||||
when is_atom(module) and is_atom(function) and is_list(args) do
|
||||
build_stream(supervisor, :nolink, enumerable, {module, function, args}, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a stream that runs the given `function` concurrently on each
|
||||
item in `enumerable`.
|
||||
|
||||
Each item will be appended to the given `args` and processed by its
|
||||
own task. The tasks will be spawned under the given `supervisor` and
|
||||
are not linked to the current process, similar to `async_nolink/2`.
|
||||
|
||||
See `async_stream/6` for discussion and examples.
|
||||
"""
|
||||
@spec async_stream_nolink(Supervisor.supervisor, Enumerable.t, (term -> term), Keyword.t) ::
|
||||
Enumerable.t
|
||||
def async_stream_nolink(supervisor, enumerable, fun, options \\ []) when is_function(fun, 1) do
|
||||
build_stream(supervisor, :nolink, enumerable, fun, options)
|
||||
owner = self()
|
||||
args = [owner, :monitor, get_info(owner), {module, fun, args}]
|
||||
{:ok, pid} = Supervisor.start_child(supervisor, args)
|
||||
ref = Process.monitor(pid)
|
||||
send pid, {owner, ref}
|
||||
%Task{pid: pid, ref: ref, owner: owner}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -210,15 +112,15 @@ defmodule Task.Supervisor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all children PIDs.
|
||||
Returns all children pids.
|
||||
"""
|
||||
@spec children(Supervisor.supervisor) :: [pid]
|
||||
def children(supervisor) do
|
||||
for {_, pid, _, _} <- Supervisor.which_children(supervisor), is_pid(pid), do: pid
|
||||
Supervisor.which_children(supervisor) |> Enum.map(&elem(&1, 1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a task as a child of the given `supervisor`.
|
||||
Starts a task as child of the given `supervisor`.
|
||||
|
||||
Note that the spawned process is not linked to the caller, but
|
||||
only to the supervisor. This command is useful in case the
|
||||
@@ -231,14 +133,14 @@ defmodule Task.Supervisor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a task as a child of the given `supervisor`.
|
||||
Starts a task as child of the given `supervisor`.
|
||||
|
||||
Similar to `start_child/2` except the task is specified
|
||||
by the given `module`, `fun` and `args`.
|
||||
"""
|
||||
@spec start_child(Supervisor.supervisor, module, atom, [term]) :: {:ok, pid}
|
||||
def start_child(supervisor, module, fun, args) do
|
||||
Supervisor.start_child(supervisor, [get_info(self()), {module, fun, args}])
|
||||
Supervisor.start_child(supervisor, [get_info(self), {module, fun, args}])
|
||||
end
|
||||
|
||||
defp get_info(self) do
|
||||
@@ -248,23 +150,4 @@ defmodule Task.Supervisor do
|
||||
{:registered_name, name} -> name
|
||||
end}
|
||||
end
|
||||
|
||||
defp do_async(supervisor, link_type, module, fun, args) do
|
||||
owner = self()
|
||||
args = [owner, :monitor, get_info(owner), {module, fun, args}]
|
||||
{:ok, pid} = Supervisor.start_child(supervisor, args)
|
||||
if link_type == :link, do: Process.link(pid)
|
||||
ref = Process.monitor(pid)
|
||||
send pid, {owner, ref}
|
||||
%Task{pid: pid, ref: ref, owner: owner}
|
||||
end
|
||||
|
||||
defp build_stream(supervisor, link_type, enumerable, fun, options) do
|
||||
&Task.Supervised.stream(enumerable, &1, &2, fun, options, fn owner, mfa ->
|
||||
args = [owner, :monitor, get_info(owner), mfa]
|
||||
{:ok, pid} = Supervisor.start_child(supervisor, args)
|
||||
if link_type == :link, do: Process.link(pid)
|
||||
{link_type, pid}
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
+2
-43
@@ -2,49 +2,8 @@ defmodule Tuple do
|
||||
@moduledoc """
|
||||
Functions for working with tuples.
|
||||
|
||||
Tuples are ordered collection of elements; tuples can contain elements of any
|
||||
type, and a tuple can contain elements of different types. Curly braces can be
|
||||
used to create tuples:
|
||||
|
||||
iex> {}
|
||||
{}
|
||||
iex> {1, :two, "three"}
|
||||
{1, :two, "three"}
|
||||
|
||||
Tuples store elements contiguously in memory; this means that accessing a
|
||||
tuple element by index (which can be done through the `Kernel.elem/2`
|
||||
function) is a constant-time operation:
|
||||
|
||||
iex> tuple = {1, :two, "three"}
|
||||
iex> elem(tuple, 0)
|
||||
1
|
||||
iex> elem(tuple, 2)
|
||||
"three"
|
||||
|
||||
Same goes for getting the tuple size (via `Kernel.tuple_size/1`):
|
||||
|
||||
iex> tuple_size({})
|
||||
0
|
||||
iex> tuple_size({1, 2, 3})
|
||||
3
|
||||
|
||||
Tuples being stored contiguously in memory also means that updating a tuple
|
||||
(for example replacing an element with `Kernel.put_elem/3`) will make a copy
|
||||
of the whole tuple.
|
||||
|
||||
Tuples are not meant to be used as a "collection" type (which is also
|
||||
suggested by the absence of an implementation of the `Enumerable` protocol for
|
||||
tuples): they're mostly meant to be used as a fixed-size container for
|
||||
multiple elements. For example, tuples are often used to have functions return
|
||||
"enriched" values: a common pattern is for functions to return `{:ok, value}`
|
||||
for successful cases and `{:error, reason}` for unsuccessful cases. For
|
||||
example, this is exactly what `File.read/1` does: it returns `{:ok, contents}`
|
||||
if reading the given file is successful, or `{:error, reason}` otherwise
|
||||
(e.g., `{:error, :enoent}` if the file doesn't exist).
|
||||
|
||||
This module provides functions to work with tuples; some more functions to
|
||||
work with tuples can be found in `Kernel` (`Kernel.tuple_size/1`,
|
||||
`Kernel.elem/2`, `Kernel.put_elem/3`, and others).
|
||||
See also `Kernel.elem/2`, `Kernel.is_tuple/1`,
|
||||
`Kernel.put_elem/3`, and `Kernel.tuple_size/1`.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
|
||||
+130
-303
@@ -1,35 +1,21 @@
|
||||
defmodule URI do
|
||||
@moduledoc """
|
||||
Utilities for working with URIs.
|
||||
|
||||
This module provides functions for working with URIs (for example, parsing
|
||||
URIs or encoding query strings). For reference, most of the functions in this
|
||||
module refer to [RFC 3986](https://tools.ietf.org/html/rfc3986).
|
||||
Utilities for working with and creating URIs.
|
||||
"""
|
||||
|
||||
defstruct scheme: nil, path: nil, query: nil,
|
||||
fragment: nil, authority: nil,
|
||||
userinfo: nil, host: nil, port: nil
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
scheme: nil | binary,
|
||||
path: nil | binary,
|
||||
query: nil | binary,
|
||||
fragment: nil | binary,
|
||||
authority: nil | binary,
|
||||
userinfo: nil | binary,
|
||||
host: nil | binary,
|
||||
port: nil | :inet.port_number,
|
||||
}
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
import Bitwise
|
||||
|
||||
@doc """
|
||||
Returns the default port for a given scheme.
|
||||
|
||||
If the scheme is unknown to the `URI` module, this function returns
|
||||
`nil`. The default port for any scheme can be configured globally
|
||||
via `default_port/2`.
|
||||
If the scheme is unknown to URI, returns `nil`.
|
||||
Any scheme may be registered via `default_port/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -40,35 +26,27 @@ defmodule URI do
|
||||
nil
|
||||
|
||||
"""
|
||||
@spec default_port(binary) :: nil | non_neg_integer
|
||||
def default_port(scheme) when is_binary(scheme) do
|
||||
:elixir_config.get({:uri, scheme})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Registers the default port `port` for the given `scheme`.
|
||||
|
||||
After this function is called, `port` will be returned by
|
||||
`default_port/1` for the given scheme `scheme`. Note that this function
|
||||
changes the default port for the given `scheme` *globally*, meaning for
|
||||
every application.
|
||||
Registers a scheme with a default port.
|
||||
|
||||
It is recommended for this function to be invoked in your
|
||||
application's start callback in case you want to register
|
||||
application start callback in case you want to register
|
||||
new URIs.
|
||||
"""
|
||||
@spec default_port(binary, non_neg_integer) :: :ok
|
||||
def default_port(scheme, port) when is_binary(scheme) and is_integer(port) and port >= 0 do
|
||||
def default_port(scheme, port) when is_binary(scheme) and port > 0 do
|
||||
:elixir_config.put({:uri, scheme}, port)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes an enumerable into a query string.
|
||||
|
||||
Takes an enumerable that enumerates as a list of two-element
|
||||
tuples (e.g., a map or a keyword list) and returns a string
|
||||
in the form of `key1=value1&key2=value2...` where keys and
|
||||
values are URL encoded as per `encode_www_form/1`.
|
||||
Takes an enumerable (containing a sequence of two-item tuples)
|
||||
and returns a string of the form "key1=value1&key2=value2..." where
|
||||
keys and values are URL encoded as per `encode/2`.
|
||||
|
||||
Keys and values can be any term that implements the `String.Chars`
|
||||
protocol, except lists which are explicitly forbidden.
|
||||
@@ -79,39 +57,15 @@ defmodule URI do
|
||||
iex> URI.encode_query(hd)
|
||||
"bar=2&foo=1"
|
||||
|
||||
iex> query = %{"key" => "value with spaces"}
|
||||
iex> URI.encode_query(query)
|
||||
"key=value+with+spaces"
|
||||
|
||||
iex> URI.encode_query %{key: [:a, :list]}
|
||||
** (ArgumentError) encode_query/1 values cannot be lists, got: [:a, :list]
|
||||
|
||||
"""
|
||||
@spec encode_query(term) :: binary
|
||||
def encode_query(enumerable) do
|
||||
Enum.map_join(enumerable, "&", &encode_kv_pair/1)
|
||||
end
|
||||
|
||||
defp encode_kv_pair({key, _}) when is_list(key) do
|
||||
raise ArgumentError, "encode_query/1 keys cannot be lists, got: #{inspect key}"
|
||||
end
|
||||
|
||||
defp encode_kv_pair({_, value}) when is_list(value) do
|
||||
raise ArgumentError, "encode_query/1 values cannot be lists, got: #{inspect value}"
|
||||
end
|
||||
|
||||
defp encode_kv_pair({key, value}) do
|
||||
encode_www_form(Kernel.to_string(key)) <>
|
||||
"=" <> encode_www_form(Kernel.to_string(value))
|
||||
end
|
||||
def encode_query(l), do: Enum.map_join(l, "&", &pair/1)
|
||||
|
||||
@doc """
|
||||
Decodes a query string into a map.
|
||||
Decodes a query string into a dictionary (by default uses a map).
|
||||
|
||||
Given a query string of the form of `key1=value1&key2=value2...`, this
|
||||
function inserts each key-value pair in the query string as one entry in the
|
||||
given `map`. Keys and values in the resulting map will be binaries. Keys and
|
||||
values will be percent-unescaped.
|
||||
Given a query string of the form "key1=value1&key2=value2...", produces a
|
||||
map with one entry for each key-value pair. Each key and value will be a
|
||||
binary. Keys and values will be percent-unescaped.
|
||||
|
||||
Use `query_decoder/1` if you want to iterate over each value manually.
|
||||
|
||||
@@ -120,119 +74,83 @@ defmodule URI do
|
||||
iex> URI.decode_query("foo=1&bar=2")
|
||||
%{"bar" => "2", "foo" => "1"}
|
||||
|
||||
iex> URI.decode_query("percent=oh+yes%21", %{"starting" => "map"})
|
||||
%{"percent" => "oh yes!", "starting" => "map"}
|
||||
|
||||
"""
|
||||
@spec decode_query(binary, map) :: map
|
||||
def decode_query(query, map \\ %{})
|
||||
|
||||
# TODO: Remove on 2.0
|
||||
def decode_query(query, %{__struct__: _} = dict) when is_binary(query) do
|
||||
IO.warn "URI.decode_query/2 is deprecated, please use URI.decode_query/1"
|
||||
decode_query_into_dict(query, dict)
|
||||
end
|
||||
|
||||
def decode_query(query, map) when is_binary(query) and is_map(map) do
|
||||
decode_query_into_map(query, map)
|
||||
end
|
||||
|
||||
# TODO: Remove on 2.0
|
||||
def decode_query(query, dict) when is_binary(query) do
|
||||
IO.warn "URI.decode_query/2 is deprecated, please use URI.decode_query/1"
|
||||
decode_query_into_dict(query, dict)
|
||||
end
|
||||
|
||||
defp decode_query_into_map(query, map) do
|
||||
case decode_next_query_pair(query) do
|
||||
nil ->
|
||||
map
|
||||
{{key, value}, rest} ->
|
||||
decode_query_into_map(rest, Map.put(map, key, value))
|
||||
end
|
||||
end
|
||||
|
||||
defp decode_query_into_dict(query, dict) do
|
||||
case decode_next_query_pair(query) do
|
||||
nil ->
|
||||
dict
|
||||
{{key, value}, rest} ->
|
||||
decode_query_into_dict(rest, Dict.put(dict, key, value))
|
||||
# TODO: Deprecate giving not a map on 1.3
|
||||
def decode_query(q, dict \\ %{}) when is_binary(q) do
|
||||
case do_decode_query(q) do
|
||||
nil -> dict
|
||||
{{k, v}, q} -> decode_query(q, Dict.put(dict, k, v))
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a stream of two-element tuples representing key-value pairs in the
|
||||
given `query`.
|
||||
|
||||
Key and value in each tuple will be binaries and will be percent-unescaped.
|
||||
Returns an iterator function over the query string that decodes
|
||||
the query string in steps.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> URI.query_decoder("foo=1&bar=2") |> Enum.to_list()
|
||||
iex> URI.query_decoder("foo=1&bar=2") |> Enum.map(&(&1))
|
||||
[{"foo", "1"}, {"bar", "2"}]
|
||||
|
||||
"""
|
||||
@spec query_decoder(binary) :: Enumerable.t
|
||||
def query_decoder(query) when is_binary(query) do
|
||||
Stream.unfold(query, &decode_next_query_pair/1)
|
||||
def query_decoder(q) when is_binary(q) do
|
||||
Stream.unfold(q, &do_decode_query/1)
|
||||
end
|
||||
|
||||
defp decode_next_query_pair("") do
|
||||
defp do_decode_query("") do
|
||||
nil
|
||||
end
|
||||
|
||||
defp decode_next_query_pair(query) do
|
||||
{undecoded_next_pair, rest} =
|
||||
case :binary.split(query, "&") do
|
||||
[next_pair, rest] -> {next_pair, rest}
|
||||
[next_pair] -> {next_pair, ""}
|
||||
defp do_decode_query(q) do
|
||||
{first, next} =
|
||||
case :binary.split(q, "&") do
|
||||
[first, rest] -> {first, rest}
|
||||
[first] -> {first, ""}
|
||||
end
|
||||
|
||||
next_pair =
|
||||
case :binary.split(undecoded_next_pair, "=") do
|
||||
[key, value] -> {decode_www_form(key), decode_www_form(value)}
|
||||
[key] -> {decode_www_form(key), nil}
|
||||
current =
|
||||
case :binary.split(first, "=") do
|
||||
[key, value] ->
|
||||
{decode_www_form(key), decode_www_form(value)}
|
||||
[key] ->
|
||||
{decode_www_form(key), nil}
|
||||
end
|
||||
|
||||
{next_pair, rest}
|
||||
{current, next}
|
||||
end
|
||||
|
||||
defp pair({k, _}) when is_list(k) do
|
||||
raise ArgumentError, "encode_query/1 keys cannot be lists, got: #{inspect k}"
|
||||
end
|
||||
|
||||
defp pair({_, v}) when is_list(v) do
|
||||
raise ArgumentError, "encode_query/1 values cannot be lists, got: #{inspect v}"
|
||||
end
|
||||
|
||||
defp pair({k, v}) do
|
||||
encode_www_form(Kernel.to_string(k)) <>
|
||||
"=" <> encode_www_form(Kernel.to_string(v))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the character is a "reserved" character in a URI.
|
||||
|
||||
Reserved characters are specified in
|
||||
[RFC 3986, section 2.2](http://tools.ietf.org/html/rfc3986#section-2.2).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> URI.char_reserved?(?+)
|
||||
true
|
||||
|
||||
Reserved characters are specified in [RFC3986, section 2.2](http://tools.ietf.org/html/rfc3986#section-2.2).
|
||||
"""
|
||||
@spec char_reserved?(char) :: boolean
|
||||
def char_reserved?(char) when char in 0..0x10FFFF do
|
||||
char in ':/?#[]@!$&\'()*+,;='
|
||||
def char_reserved?(c) do
|
||||
c in ':/?#[]@!$&\'()*+,;='
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the character is a "unreserved" character in a URI.
|
||||
|
||||
Unreserved characters are specified in
|
||||
[RFC 3986, section 2.3](http://tools.ietf.org/html/rfc3986#section-2.3).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> URI.char_unreserved?(?_)
|
||||
true
|
||||
|
||||
Unreserved characters are specified in [RFC3986, section 2.3](http://tools.ietf.org/html/rfc3986#section-2.3).
|
||||
"""
|
||||
@spec char_unreserved?(char) :: boolean
|
||||
def char_unreserved?(char) when char in 0..0x10FFFF do
|
||||
char in ?0..?9 or
|
||||
char in ?a..?z or
|
||||
char in ?A..?Z or
|
||||
char in '~_-.'
|
||||
def char_unreserved?(c) do
|
||||
c in ?0..?9 or
|
||||
c in ?a..?z or
|
||||
c in ?A..?Z or
|
||||
c in '~_-.'
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -240,39 +158,23 @@ defmodule URI do
|
||||
|
||||
This is the default used by `URI.encode/2` where both
|
||||
reserved and unreserved characters are kept unescaped.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> URI.char_unescaped?(?{)
|
||||
false
|
||||
|
||||
"""
|
||||
@spec char_unescaped?(char) :: boolean
|
||||
def char_unescaped?(char) when char in 0..0x10FFFF do
|
||||
char_reserved?(char) or char_unreserved?(char)
|
||||
def char_unescaped?(c) do
|
||||
char_reserved?(c) or char_unreserved?(c)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Percent-escapes the given string.
|
||||
Percent-escapes a URI.
|
||||
Accepts `predicate` function as an argument to specify if char can be left as is.
|
||||
|
||||
This function accepts a `predicate` function as an optional argument; if
|
||||
passed, this function will be called with each character (byte) in `string` as
|
||||
its argument and should return `true` if that character should not be escaped
|
||||
and left as is.
|
||||
|
||||
## Examples
|
||||
## Example
|
||||
|
||||
iex> URI.encode("ftp://s-ite.tld/?value=put it+й")
|
||||
"ftp://s-ite.tld/?value=put%20it+%D0%B9"
|
||||
|
||||
iex> URI.encode("a string", &(&1 != ?i))
|
||||
"a str%69ng"
|
||||
|
||||
"""
|
||||
@spec encode(binary, (byte -> boolean)) :: binary
|
||||
def encode(string, predicate \\ &char_unescaped?/1)
|
||||
when is_binary(string) and is_function(predicate, 1) do
|
||||
for <<char <- string>>, into: "", do: percent(char, predicate)
|
||||
def encode(str, predicate \\ &char_unescaped?/1) when is_binary(str) do
|
||||
for <<c <- str>>, into: "", do: percent(c, predicate)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -284,26 +186,25 @@ defmodule URI do
|
||||
"put%3A+it%2B%D0%B9"
|
||||
|
||||
"""
|
||||
@spec encode_www_form(binary) :: binary
|
||||
def encode_www_form(string) when is_binary(string) do
|
||||
for <<char <- string>>, into: "" do
|
||||
case percent(char, &char_unreserved?/1) do
|
||||
def encode_www_form(str) when is_binary(str) do
|
||||
for <<c <- str>>, into: "" do
|
||||
case percent(c, &char_unreserved?/1) do
|
||||
"%20" -> "+"
|
||||
percent -> percent
|
||||
pct -> pct
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp percent(char, predicate) do
|
||||
if predicate.(char) do
|
||||
<<char>>
|
||||
defp percent(c, predicate) do
|
||||
if predicate.(c) do
|
||||
<<c>>
|
||||
else
|
||||
<<"%", hex(bsr(char, 4)), hex(band(char, 15))>>
|
||||
"%" <> hex(bsr(c, 4)) <> hex(band(c, 15))
|
||||
end
|
||||
end
|
||||
|
||||
defp hex(n) when n <= 9, do: n + ?0
|
||||
defp hex(n), do: n + ?A - 10
|
||||
defp hex(n) when n <= 9, do: <<n + ?0>>
|
||||
defp hex(n), do: <<n + ?A - 10>>
|
||||
|
||||
@doc """
|
||||
Percent-unescapes a URI.
|
||||
@@ -314,7 +215,6 @@ defmodule URI do
|
||||
"http://elixir-lang.org"
|
||||
|
||||
"""
|
||||
@spec decode(binary) :: binary
|
||||
def decode(uri) do
|
||||
unpercent(uri, "", false)
|
||||
catch
|
||||
@@ -331,20 +231,19 @@ defmodule URI do
|
||||
"<all in/"
|
||||
|
||||
"""
|
||||
@spec decode_www_form(binary) :: binary
|
||||
def decode_www_form(string) do
|
||||
unpercent(string, "", true)
|
||||
def decode_www_form(str) do
|
||||
unpercent(str, "", true)
|
||||
catch
|
||||
:malformed_uri ->
|
||||
raise ArgumentError, "malformed URI #{inspect string}"
|
||||
raise ArgumentError, "malformed URI #{inspect str}"
|
||||
end
|
||||
|
||||
defp unpercent(<<?+, tail::binary>>, acc, spaces = true) do
|
||||
unpercent(tail, <<acc::binary, ?\s>>, spaces)
|
||||
end
|
||||
|
||||
defp unpercent(<<?%, hex1, hex2, tail::binary>>, acc, spaces) do
|
||||
unpercent(tail, <<acc::binary, bsl(hex_to_dec(hex1), 4) + hex_to_dec(hex2)>>, spaces)
|
||||
defp unpercent(<<?%, hex_1, hex_2, tail::binary>>, acc, spaces) do
|
||||
unpercent(tail, <<acc::binary, bsl(hex_to_dec(hex_1), 4) + hex_to_dec(hex_2)>>, spaces)
|
||||
end
|
||||
defp unpercent(<<?%, _::binary>>, _acc, _spaces), do: throw(:malformed_uri)
|
||||
|
||||
@@ -362,17 +261,14 @@ defmodule URI do
|
||||
Parses a well-formed URI reference into its components.
|
||||
|
||||
Note this function expects a well-formed URI and does not perform
|
||||
any validation. See the "Examples" section below for examples of how
|
||||
`URI.parse/1` can be used to parse a wide range of URIs.
|
||||
any validation. See the examples section below of how `URI.parse/1`
|
||||
can be used to parse a wide range of relative URIs.
|
||||
|
||||
This function uses the parsing regular expression as defined
|
||||
in [RFC 3986, Appendix B](http://tools.ietf.org/html/rfc3986#appendix-B).
|
||||
in the [Appendix B of RFC3986](http://tools.ietf.org/html/rfc3986#appendix-B).
|
||||
|
||||
When a URI is given without a port, the value returned by
|
||||
`URI.default_port/1` for the URI's scheme is used for the `:port` field.
|
||||
|
||||
If a `%URI{}` struct is given to this function, this function returns it
|
||||
unmodified.
|
||||
When a URI is given without a port, the values registered via
|
||||
`URI.default_port/1` and `URI.default_port/2` are used.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -394,21 +290,29 @@ defmodule URI do
|
||||
port: nil, query: nil, scheme: nil, userinfo: nil}
|
||||
|
||||
"""
|
||||
@spec parse(t | binary) :: t
|
||||
def parse(uri)
|
||||
|
||||
def parse(%URI{} = uri), do: uri
|
||||
|
||||
def parse(string) when is_binary(string) do
|
||||
def parse(s) when is_binary(s) do
|
||||
# From http://tools.ietf.org/html/rfc3986#appendix-B
|
||||
regex = ~r/^(([a-z][a-z0-9\+\-\.]*):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/i
|
||||
parts = nillify(Regex.run(regex, string))
|
||||
parts = nillify(Regex.run(regex, s))
|
||||
|
||||
destructure [_, _, scheme, _, authority, path, _, query, _, fragment], parts
|
||||
{userinfo, host, port} = split_authority(authority)
|
||||
|
||||
scheme = scheme && String.downcase(scheme)
|
||||
port = port || (scheme && default_port(scheme))
|
||||
if authority do
|
||||
authority = ""
|
||||
|
||||
if userinfo, do: authority = authority <> userinfo <> "@"
|
||||
if host, do: authority = authority <> host
|
||||
if port, do: authority = authority <> ":" <> Integer.to_string(port)
|
||||
end
|
||||
|
||||
scheme = normalize_scheme(scheme)
|
||||
|
||||
if is_nil(port) and not is_nil(scheme) do
|
||||
port = default_port(scheme)
|
||||
end
|
||||
|
||||
%URI{
|
||||
scheme: scheme, path: path, query: query,
|
||||
@@ -418,141 +322,64 @@ defmodule URI do
|
||||
end
|
||||
|
||||
# Split an authority into its userinfo, host and port parts.
|
||||
defp split_authority(string) do
|
||||
components = Regex.run(~r/(^(.*)@)?(\[[a-zA-Z0-9:.]*\]|[^:]*)(:(\d*))?/, string || "")
|
||||
defp split_authority(s) do
|
||||
s = s || ""
|
||||
components = Regex.run ~r/(^(.*)@)?(\[[a-zA-Z0-9:.]*\]|[^:]*)(:(\d*))?/, s
|
||||
|
||||
destructure [_, _, userinfo, host, _, port], nillify(components)
|
||||
host = if host, do: host |> String.trim_leading("[") |> String.trim_trailing("]")
|
||||
port = if port, do: String.to_integer(port)
|
||||
host = if host, do: host |> String.lstrip(?[) |> String.rstrip(?])
|
||||
|
||||
{userinfo, host, port}
|
||||
end
|
||||
|
||||
defp normalize_scheme(nil), do: nil
|
||||
defp normalize_scheme(scheme), do: String.downcase(scheme)
|
||||
|
||||
# Regex.run returns empty strings sometimes. We want
|
||||
# to replace those with nil for consistency.
|
||||
defp nillify(list) do
|
||||
for string <- list do
|
||||
if byte_size(string) > 0, do: string
|
||||
defp nillify(l) do
|
||||
for s <- l do
|
||||
if byte_size(s) > 0, do: s
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the string representation of the given `URI` struct.
|
||||
Converts the URI to string.
|
||||
|
||||
iex> URI.to_string(URI.parse("http://google.com"))
|
||||
"http://google.com"
|
||||
|
||||
iex> URI.to_string(%URI{scheme: "foo", host: "bar.baz"})
|
||||
"foo://bar.baz"
|
||||
|
||||
"""
|
||||
@spec to_string(t) :: binary
|
||||
defdelegate to_string(uri), to: String.Chars.URI
|
||||
|
||||
@doc ~S"""
|
||||
Merges two URIs.
|
||||
|
||||
This function merges two URIs as per
|
||||
[RFC 3986, section 5.2](http://tools.ietf.org/html/rfc3986#section-5.2).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> URI.merge(URI.parse("http://google.com"), "/query") |> to_string
|
||||
"http://google.com/query"
|
||||
|
||||
iex> URI.merge("http://example.com", "http://google.com") |> to_string
|
||||
"http://google.com"
|
||||
|
||||
"""
|
||||
@spec merge(t | binary, t | binary) :: t
|
||||
def merge(uri, rel)
|
||||
|
||||
def merge(%URI{authority: nil}, _rel) do
|
||||
raise ArgumentError, "you must merge onto an absolute URI"
|
||||
end
|
||||
def merge(_base, %URI{scheme: rel_scheme} = rel) when rel_scheme != nil do
|
||||
rel
|
||||
end
|
||||
def merge(%URI{} = base, %URI{path: rel_path} = rel) when rel_path in ["", nil] do
|
||||
%{base | query: rel.query || base.query, fragment: rel.fragment}
|
||||
end
|
||||
def merge(%URI{} = base, %URI{} = rel) do
|
||||
new_path = merge_paths(base.path, rel.path)
|
||||
%{base | path: new_path, query: rel.query, fragment: rel.fragment}
|
||||
end
|
||||
def merge(base, rel) do
|
||||
merge(parse(base), parse(rel))
|
||||
end
|
||||
|
||||
defp merge_paths(nil, rel_path),
|
||||
do: merge_paths("/", rel_path)
|
||||
defp merge_paths(_, "/" <> _ = rel_path),
|
||||
do: rel_path
|
||||
defp merge_paths(base_path, rel_path) do
|
||||
[_ | base_segments] = path_to_segments(base_path)
|
||||
path_to_segments(rel_path)
|
||||
|> Kernel.++(base_segments)
|
||||
|> remove_dot_segments([])
|
||||
|> Enum.join("/")
|
||||
end
|
||||
|
||||
defp remove_dot_segments([], [head, ".." | acc]),
|
||||
do: remove_dot_segments([], [head | acc])
|
||||
defp remove_dot_segments([], acc),
|
||||
do: acc
|
||||
defp remove_dot_segments(["." | tail], acc),
|
||||
do: remove_dot_segments(tail, acc)
|
||||
defp remove_dot_segments([head | tail], ["..", ".." | _] = acc),
|
||||
do: remove_dot_segments(tail, [head | acc])
|
||||
defp remove_dot_segments(segments, [_, ".." | acc]),
|
||||
do: remove_dot_segments(segments, acc)
|
||||
defp remove_dot_segments([head | tail], acc),
|
||||
do: remove_dot_segments(tail, [head | acc])
|
||||
|
||||
def path_to_segments(path) do
|
||||
[head | tail] = String.split(path, "/")
|
||||
reverse_and_discard_empty(tail, [head])
|
||||
end
|
||||
|
||||
defp reverse_and_discard_empty([], acc),
|
||||
do: acc
|
||||
defp reverse_and_discard_empty([head], acc),
|
||||
do: [head | acc]
|
||||
defp reverse_and_discard_empty(["" | tail], acc),
|
||||
do: reverse_and_discard_empty(tail, acc)
|
||||
defp reverse_and_discard_empty([head | tail], acc),
|
||||
do: reverse_and_discard_empty(tail, [head | acc])
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: URI do
|
||||
def to_string(%{scheme: scheme, port: port, path: path,
|
||||
query: query, fragment: fragment} = uri) do
|
||||
uri =
|
||||
case scheme && URI.default_port(scheme) do
|
||||
^port -> %{uri | port: nil}
|
||||
_ -> uri
|
||||
end
|
||||
def to_string(uri) do
|
||||
scheme = uri.scheme
|
||||
|
||||
if scheme && (port = URI.default_port(scheme)) do
|
||||
if uri.port == port, do: uri = %{uri | port: nil}
|
||||
end
|
||||
|
||||
# Based on http://tools.ietf.org/html/rfc3986#section-5.3
|
||||
authority = extract_authority(uri)
|
||||
|
||||
if(scheme, do: scheme <> ":", else: "") <>
|
||||
if(authority, do: "//" <> authority, else: "") <>
|
||||
if(path, do: path, else: "") <>
|
||||
if(query, do: "?" <> query, else: "") <>
|
||||
if(fragment, do: "#" <> fragment, else: "")
|
||||
result = ""
|
||||
if uri.scheme, do: result = result <> uri.scheme <> ":"
|
||||
if authority, do: result = result <> "//" <> authority
|
||||
if uri.path, do: result = result <> uri.path
|
||||
if uri.query, do: result = result <> "?" <> uri.query
|
||||
if uri.fragment, do: result = result <> "#" <> uri.fragment
|
||||
result
|
||||
end
|
||||
|
||||
defp extract_authority(%{host: nil, authority: authority}) do
|
||||
authority
|
||||
end
|
||||
defp extract_authority(%{host: host, userinfo: userinfo, port: port}) do
|
||||
# According to the grammar at
|
||||
# https://tools.ietf.org/html/rfc3986#appendix-A, a "host" can have a colon
|
||||
# in it only if it's an IPv6 or "IPvFuture" address), so if there's a colon
|
||||
# in the host we can safely surround it with [].
|
||||
if(userinfo, do: userinfo <> "@", else: "") <>
|
||||
if(String.contains?(host, ":"), do: "[" <> host <> "]", else: host) <>
|
||||
if(port, do: ":" <> Integer.to_string(port), else: "")
|
||||
authority = host
|
||||
if userinfo, do: authority = userinfo <> "@" <> authority
|
||||
if port, do: authority = authority <> ":" <> Integer.to_string(port)
|
||||
authority
|
||||
end
|
||||
end
|
||||
|
||||
+76
-186
@@ -64,24 +64,6 @@ defmodule Version do
|
||||
`~> 2.0` | `>= 2.0.0 and < 3.0.0`
|
||||
`~> 2.1` | `>= 2.1.0 and < 3.0.0`
|
||||
|
||||
When `allow_pre: false` is set the requirement will not match a
|
||||
pre-release version unless the operand is a pre-release version.
|
||||
The default is to allow always allow pre-releases but note that in
|
||||
Hex `:allow_pre` is set to `false.` See the table below for examples.
|
||||
|
||||
Requirement | Version | `:allow_pre` | Matches
|
||||
:------------- | :---------- | :----------- | :------
|
||||
`~> 2.0` | `2.1.0` | - | `true`
|
||||
`~> 2.0` | `3.0.0` | - | `false`
|
||||
`~> 2.0.0` | `2.0.1` | - | `true`
|
||||
`~> 2.0.0` | `2.1.0` | - | `false`
|
||||
`~> 2.1.2` | `2.1.3-dev` | `true` | `true`
|
||||
`~> 2.1.2` | `2.1.3-dev` | `false` | `false`
|
||||
`~> 2.1-dev` | `2.2.0-dev` | `false` | `true`
|
||||
`~> 2.1.2-dev` | `2.1.3-dev` | `false` | `true`
|
||||
`>= 2.1.0` | `2.2.0-dev` | `false` | `false`
|
||||
`>= 2.1.0-dev` | `2.2.3-dev` | `true` | `true`
|
||||
|
||||
"""
|
||||
|
||||
import Kernel, except: [match?: 2]
|
||||
@@ -106,7 +88,7 @@ defmodule Version do
|
||||
build: build}
|
||||
|
||||
defmodule Requirement do
|
||||
defstruct [:source, :matchspec, :compiled]
|
||||
defstruct [:source, :matchspec]
|
||||
@type t :: %__MODULE__{}
|
||||
end
|
||||
|
||||
@@ -127,12 +109,6 @@ defmodule Version do
|
||||
If given an already parsed version and requirement this function won't
|
||||
raise.
|
||||
|
||||
## Options
|
||||
|
||||
* `:allow_pre` - when `false` pre-release versions will not match
|
||||
unless the operand is a pre-release version, see the table above
|
||||
for examples (default: `true`);
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Version.match?("2.0.0", ">1.0.0")
|
||||
@@ -148,56 +124,34 @@ defmodule Version do
|
||||
** (Version.InvalidRequirementError) == ==1.0.0
|
||||
|
||||
"""
|
||||
@spec match?(version, requirement, Keyword.t) :: boolean
|
||||
def match?(version, requirement, opts \\ [])
|
||||
|
||||
def match?(version, requirement, opts) when is_binary(requirement) do
|
||||
@spec match?(version, requirement) :: boolean
|
||||
def match?(version, requirement) when is_binary(requirement) do
|
||||
case parse_requirement(requirement) do
|
||||
{:ok, requirement} ->
|
||||
match?(version, requirement, opts)
|
||||
match?(version, requirement)
|
||||
:error ->
|
||||
raise InvalidRequirementError, message: requirement
|
||||
end
|
||||
end
|
||||
|
||||
def match?(version, %Requirement{matchspec: spec, compiled: false}, opts) do
|
||||
allow_pre = Keyword.get(opts, :allow_pre, true)
|
||||
{:ok, result} = :ets.test_ms(to_matchable(version, allow_pre), spec)
|
||||
def match?(version, %Requirement{matchspec: spec}) do
|
||||
{:ok, result} = :ets.test_ms(to_matchable(version), spec)
|
||||
result != false
|
||||
end
|
||||
|
||||
def match?(version, %Requirement{matchspec: spec, compiled: true}, opts) do
|
||||
allow_pre = Keyword.get(opts, :allow_pre, true)
|
||||
:ets.match_spec_run([to_matchable(version, allow_pre)], spec) != []
|
||||
end
|
||||
|
||||
@doc """
|
||||
Compares two versions. Returns `:gt` if the first version is greater than
|
||||
the second one, and `:lt` for vice versa. If the two versions are equal `:eq`
|
||||
is returned.
|
||||
Compares two versions. Returns `:gt` if first version is greater than
|
||||
the second and `:lt` for vice versa. If the two versions are equal `:eq`
|
||||
is returned
|
||||
|
||||
Pre-releases are strictly less than their corresponding release versions.
|
||||
|
||||
Patch segments are compared lexicographically if they are alphanumeric, and
|
||||
numerically otherwise.
|
||||
|
||||
Build segments are ignored, if two versions differ only in their build segment
|
||||
they are considered to be equal.
|
||||
|
||||
Raises a `Version.InvalidVersionError` exception if any of the two are not
|
||||
parsable. If given an already parsed version this function won't raise.
|
||||
Raises a `Version.InvalidVersionError` exception if `version` is not parsable.
|
||||
If given an already parsed version this function won't raise.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Version.compare("2.0.1-alpha1", "2.0.0")
|
||||
:gt
|
||||
|
||||
iex> Version.compare("1.0.0-beta", "1.0.0-rc1")
|
||||
:lt
|
||||
|
||||
iex> Version.compare("1.0.0-10", "1.0.0-2")
|
||||
:gt
|
||||
|
||||
iex> Version.compare("2.0.1+build0", "2.0.1")
|
||||
:eq
|
||||
|
||||
@@ -207,10 +161,10 @@ defmodule Version do
|
||||
"""
|
||||
@spec compare(version, version) :: :gt | :eq | :lt
|
||||
def compare(version1, version2) do
|
||||
do_compare(to_matchable(version1, true), to_matchable(version2, true))
|
||||
do_compare(to_matchable(version1), to_matchable(version2))
|
||||
end
|
||||
|
||||
defp do_compare({major1, minor1, patch1, pre1, _}, {major2, minor2, patch2, pre2, _}) do
|
||||
defp do_compare({major1, minor1, patch1, pre1}, {major2, minor2, patch2, pre2}) do
|
||||
cond do
|
||||
{major1, minor1, patch1} > {major2, minor2, patch2} -> :gt
|
||||
{major1, minor1, patch1} < {major2, minor2, patch2} -> :lt
|
||||
@@ -247,28 +201,6 @@ defmodule Version do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses a version string into a `Version`.
|
||||
|
||||
If `string` is an invalid version, an `InvalidVersionError` is raised.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Version.parse!("2.0.1-alpha1")
|
||||
#Version<2.0.1-alpha1>
|
||||
|
||||
iex> Version.parse!("2.0-alpha1")
|
||||
** (Version.InvalidVersionError) 2.0-alpha1
|
||||
|
||||
"""
|
||||
@spec parse!(String.t) :: t | no_return
|
||||
def parse!(string) when is_binary(string) do
|
||||
case parse(string) do
|
||||
{:ok, version} -> version
|
||||
:error -> raise InvalidVersionError, message: string
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses a version requirement string into a `Version.Requirement`.
|
||||
|
||||
@@ -286,36 +218,20 @@ defmodule Version do
|
||||
def parse_requirement(string) when is_binary(string) do
|
||||
case Version.Parser.parse_requirement(string) do
|
||||
{:ok, spec} ->
|
||||
{:ok, %Requirement{source: string, matchspec: spec, compiled: false}}
|
||||
{:ok, %Requirement{source: string, matchspec: spec}}
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Compiles a requirement to its internal representation with
|
||||
`:ets.match_spec_compile/1` for faster matching.
|
||||
|
||||
The internal representation is opaque and can not be converted to external
|
||||
term format and then back again without losing its properties (meaning it
|
||||
can not be sent to a process on another node and still remain a valid
|
||||
compiled match_spec, nor can it be stored on disk).
|
||||
"""
|
||||
@spec compile_requirement(Requirement.t) :: Requirement.t
|
||||
def compile_requirement(%Requirement{matchspec: spec} = req) do
|
||||
%{req | matchspec: :ets.match_spec_compile(spec), compiled: true}
|
||||
defp to_matchable(%Version{major: major, minor: minor, patch: patch, pre: pre}) do
|
||||
{major, minor, patch, pre}
|
||||
end
|
||||
|
||||
defp to_matchable(%Version{major: major, minor: minor, patch: patch, pre: pre}, allow_pre?) do
|
||||
{major, minor, patch, pre, allow_pre?}
|
||||
end
|
||||
|
||||
defp to_matchable(string, allow_pre?) do
|
||||
defp to_matchable(string) do
|
||||
case Version.Parser.parse_version(string) do
|
||||
{:ok, {major, minor, patch, pre}} ->
|
||||
{major, minor, patch, pre, allow_pre?}
|
||||
:error ->
|
||||
raise InvalidVersionError, message: string
|
||||
{:ok, version} -> version
|
||||
:error -> raise InvalidVersionError, message: string
|
||||
end
|
||||
end
|
||||
|
||||
@@ -363,16 +279,16 @@ defmodule Version do
|
||||
@moduledoc false
|
||||
import Parser.DSL
|
||||
|
||||
deflexer ">=", do: :>=
|
||||
deflexer "<=", do: :<=
|
||||
deflexer "~>", do: :~>
|
||||
deflexer ">", do: :>
|
||||
deflexer "<", do: :<
|
||||
deflexer "==", do: :==
|
||||
deflexer "!=", do: :!=
|
||||
deflexer "!", do: :!=
|
||||
deflexer " or ", do: :||
|
||||
deflexer " and ", do: :&&
|
||||
deflexer ">=", do: :'>='
|
||||
deflexer "<=", do: :'<='
|
||||
deflexer "~>", do: :'~>'
|
||||
deflexer ">", do: :'>'
|
||||
deflexer "<", do: :'<'
|
||||
deflexer "==", do: :'=='
|
||||
deflexer "!=", do: :'!='
|
||||
deflexer "!", do: :'!='
|
||||
deflexer " or ", do: :'||'
|
||||
deflexer " and ", do: :'&&'
|
||||
deflexer " ", do: :' '
|
||||
|
||||
deflexer x, [] do
|
||||
@@ -384,8 +300,8 @@ defmodule Version do
|
||||
is_binary h ->
|
||||
[h <> x | acc]
|
||||
|
||||
h in [:||, :&&] ->
|
||||
[x, :==, h | acc]
|
||||
h in [:'||', :'&&'] ->
|
||||
[x, :'==', h | acc]
|
||||
|
||||
true ->
|
||||
[x, h | acc]
|
||||
@@ -401,10 +317,10 @@ defmodule Version do
|
||||
(?:\.(\d+))? # minor
|
||||
(?:\.(\d+))? # patch
|
||||
(?:\-([\d\w\.\-]+))? # pre
|
||||
(?:\+([\d\w\.\-]+))? # build
|
||||
(?:\+([\d\w\-]+))? # build
|
||||
$/x
|
||||
|
||||
@spec parse_requirement(String.t) :: {:ok, term} | :error
|
||||
@spec parse_requirement(String.t) :: {:ok, Version.Requirement.t} | :error
|
||||
def parse_requirement(source) do
|
||||
lexed = lexer(source, [])
|
||||
to_matchspec(lexed)
|
||||
@@ -442,14 +358,14 @@ defmodule Version do
|
||||
defp parse_pre(nil), do: {:ok, []}
|
||||
defp parse_pre(pre), do: parse_pre(String.split(pre, "."), [])
|
||||
|
||||
defp parse_pre([piece | t], acc) do
|
||||
defp parse_pre([piece|t], acc) do
|
||||
cond do
|
||||
piece =~ ~r/^(0|[1-9][0-9]*)$/ ->
|
||||
parse_pre(t, [String.to_integer(piece) | acc])
|
||||
parse_pre(t, [String.to_integer(piece)|acc])
|
||||
piece =~ ~r/^[0-9]*$/ ->
|
||||
:error
|
||||
true ->
|
||||
parse_pre(t, [piece | acc])
|
||||
parse_pre(t, [piece|acc])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -503,7 +419,7 @@ defmodule Version do
|
||||
if valid_requirement?(lexed) do
|
||||
first = to_condition(lexed)
|
||||
rest = Enum.drop(lexed, 2)
|
||||
{:ok, [{{:'$1', :'$2', :'$3', :'$4', :'$5'}, [to_condition(first, rest)], [:'$_']}]}
|
||||
{:ok, [{{:'$1', :'$2', :'$3', :'$4'}, [to_condition(first, rest)], [:'$_']}]}
|
||||
else
|
||||
:error
|
||||
end
|
||||
@@ -511,64 +427,77 @@ defmodule Version do
|
||||
:invalid_matchspec -> :error
|
||||
end
|
||||
|
||||
defp to_condition([:==, version | _]) do
|
||||
matchable = parse_condition(version)
|
||||
main_condition(:==, matchable)
|
||||
defp to_condition([:'==', version | _]) do
|
||||
version = parse_condition(version)
|
||||
{:'==', :'$_', {:const, version}}
|
||||
end
|
||||
|
||||
defp to_condition([:!=, version | _]) do
|
||||
matchable = parse_condition(version)
|
||||
main_condition(:'/=', matchable)
|
||||
defp to_condition([:'!=', version | _]) do
|
||||
version = parse_condition(version)
|
||||
{:'/=', :'$_', {:const, version}}
|
||||
end
|
||||
|
||||
defp to_condition([:~>, version | _]) do
|
||||
defp to_condition([:'~>', version | _]) do
|
||||
from = parse_condition(version, true)
|
||||
to = approximate_upper(from)
|
||||
|
||||
{:andalso, to_condition([:>=, matchable_to_string(from)]),
|
||||
to_condition([:<, matchable_to_string(to)])}
|
||||
{:andalso, to_condition([:'>=', matchable_to_string(from)]),
|
||||
to_condition([:'<', matchable_to_string(to)])}
|
||||
end
|
||||
|
||||
defp to_condition([:>, version | _]) do
|
||||
defp to_condition([:'>', version | _]) do
|
||||
{major, minor, patch, pre} = parse_condition(version)
|
||||
|
||||
{:andalso, {:orelse, main_condition(:>, {major, minor, patch}),
|
||||
{:andalso, main_condition(:==, {major, minor, patch}),
|
||||
pre_condition(:>, pre)}},
|
||||
no_pre_condition(pre)}
|
||||
{:orelse, {:'>', {{:'$1', :'$2', :'$3'}},
|
||||
{:const, {major, minor, patch}}},
|
||||
{:andalso, {:'==', {{:'$1', :'$2', :'$3'}},
|
||||
{:const, {major, minor, patch}}},
|
||||
{:orelse, {:andalso, {:'==', {:length, :'$4'}, 0},
|
||||
{:'/=', length(pre), 0}},
|
||||
{:andalso, {:'/=', length(pre), 0},
|
||||
{:orelse, {:'>', {:length, :'$4'}, length(pre)},
|
||||
{:andalso, {:'==', {:length, :'$4'}, length(pre)},
|
||||
{:'>', :'$4', {:const, pre}}}}}}}}
|
||||
end
|
||||
|
||||
defp to_condition([:>=, version | _]) do
|
||||
defp to_condition([:'>=', version | _]) do
|
||||
matchable = parse_condition(version)
|
||||
|
||||
{:orelse, main_condition(:==, matchable),
|
||||
to_condition([:>, version])}
|
||||
{:orelse, {:'==', :'$_', {:const, matchable}},
|
||||
to_condition([:'>', version])}
|
||||
end
|
||||
|
||||
defp to_condition([:<, version | _]) do
|
||||
defp to_condition([:'<', version | _]) do
|
||||
{major, minor, patch, pre} = parse_condition(version)
|
||||
|
||||
{:orelse, main_condition(:<, {major, minor, patch}),
|
||||
{:andalso, main_condition(:==, {major, minor, patch}),
|
||||
pre_condition(:<, pre)}}
|
||||
{:orelse, {:'<', {{:'$1', :'$2', :'$3'}},
|
||||
{:const, {major, minor, patch}}},
|
||||
{:andalso, {:'==', {{:'$1', :'$2', :'$3'}},
|
||||
{:const, {major, minor, patch}}},
|
||||
{:orelse, {:andalso, {:'/=', {:length, :'$4'}, 0},
|
||||
{:'==', length(pre), 0}},
|
||||
{:andalso, {:'/=', {:length, :'$4'}, 0},
|
||||
{:orelse, {:'<', {:length, :'$4'}, length(pre)},
|
||||
{:andalso, {:'==', {:length, :'$4'}, length(pre)},
|
||||
{:'<', :'$4', {:const, pre}}}}}}}}
|
||||
end
|
||||
|
||||
defp to_condition([:<=, version | _]) do
|
||||
defp to_condition([:'<=', version | _]) do
|
||||
matchable = parse_condition(version)
|
||||
|
||||
{:orelse, main_condition(:==, matchable),
|
||||
to_condition([:<, version])}
|
||||
{:orelse, {:'==', :'$_', {:const, matchable}},
|
||||
to_condition([:'<', version])}
|
||||
end
|
||||
|
||||
defp to_condition(current, []) do
|
||||
current
|
||||
end
|
||||
|
||||
defp to_condition(current, [:&&, operator, version | rest]) do
|
||||
defp to_condition(current, [:'&&', operator, version | rest]) do
|
||||
to_condition({:andalso, current, to_condition([operator, version])}, rest)
|
||||
end
|
||||
|
||||
defp to_condition(current, [:||, operator, version | rest]) do
|
||||
defp to_condition(current, [:'||', operator, version | rest]) do
|
||||
to_condition({:orelse, current, to_condition([operator, version])}, rest)
|
||||
end
|
||||
|
||||
@@ -579,45 +508,6 @@ defmodule Version do
|
||||
end
|
||||
end
|
||||
|
||||
defp main_condition(op, version) when tuple_size(version) == 3 do
|
||||
{op, {{:'$1', :'$2', :'$3'}},
|
||||
{:const, version}}
|
||||
end
|
||||
|
||||
defp main_condition(op, version) when tuple_size(version) == 4 do
|
||||
{op, {{:'$1', :'$2', :'$3', :'$4'}},
|
||||
{:const, version}}
|
||||
end
|
||||
|
||||
defp pre_condition(:>, pre) do
|
||||
length_pre = length(pre)
|
||||
|
||||
{:orelse, {:andalso, {:==, {:length, :'$4'}, 0},
|
||||
{:const, length_pre != 0}},
|
||||
{:andalso, {:const, length_pre != 0},
|
||||
{:orelse, {:>, {:length, :'$4'}, length_pre},
|
||||
{:andalso, {:==, {:length, :'$4'}, length_pre},
|
||||
{:>, :'$4', {:const, pre}}}}}}
|
||||
end
|
||||
|
||||
defp pre_condition(:<, pre) do
|
||||
length_pre = length(pre)
|
||||
|
||||
{:orelse, {:andalso, {:'/=', {:length, :'$4'}, 0},
|
||||
{:const, length_pre == 0}},
|
||||
{:andalso, {:'/=', {:length, :'$4'}, 0},
|
||||
{:orelse, {:<, {:length, :'$4'}, length_pre},
|
||||
{:andalso, {:==, {:length, :'$4'}, length_pre},
|
||||
{:<, :'$4', {:const, pre}}}}}}
|
||||
end
|
||||
|
||||
defp no_pre_condition([]) do
|
||||
{:orelse, :'$5', {:==, {:length, :'$4'}, 0}}
|
||||
end
|
||||
defp no_pre_condition(_pre) do
|
||||
{:const, true}
|
||||
end
|
||||
|
||||
defp matchable_to_string({major, minor, patch, pre}) do
|
||||
patch = if patch, do: "#{patch}", else: "0"
|
||||
pre = if pre != [], do: "-#{Enum.join(pre, ".")}"
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
# Behaviours
|
||||
|
||||
Behaviours in Elixir (and Erlang) are a way to separate and abstract the generic part of a component (which becomes the *behaviour module*) from the specific part (which becomes the *callback module*).
|
||||
|
||||
A behaviour module defines a set of functions and macros (referred to as *callbacks*) that callback modules implementing that behaviour must export. This "interface" identifies the specific part of the component. For example, the `GenServer` behaviour and functions abstract away all the message-passing (sending and receiving) and error reporting that a "server" process will likely want to implement from the specific parts such as the actions that this server process has to perform.
|
||||
|
||||
If a callback module that implements a given behaviour doesn't export all the functions and macros defined by that behaviour, the user will be notified through warnings during the compilation process (no errors will happen).
|
||||
|
||||
Elixir's standard library contains a few frequently used behaviours such as `GenServer`, `Supervisor`, and `Application`.
|
||||
|
||||
## Defining a behaviour
|
||||
|
||||
A behaviour is always backed by a module (which is how the behaviour will be identified): the module where callbacks are defined. To define a behaviour module, it's enough to define one or more callbacks in that module. To define callbacks, the `@callback` and `@macrocallback` module attributes can be used (for function callbacks and macro callbacks respectively).
|
||||
|
||||
defmodule MyBehaviour do
|
||||
@callback my_fun(arg :: any) :: any
|
||||
@macrocallback my_macro(arg :: any) :: Macro.t
|
||||
end
|
||||
|
||||
As seen in the example above, defining a callback is a matter of defining a specification for that callback, made of:
|
||||
|
||||
* the callback name (`my_fun` or `my_macro` in the example)
|
||||
* the arguments that the callback must accept (`arg :: any` in the example)
|
||||
* the *expected* type of the callback return value
|
||||
|
||||
For more information on typespecs, consult the ["Typespecs"](typespecs.html) page in the Elixir documentation. As mentioned in this page, type specification are only annotations used by documentation and tools, so defining such specifications for behaviours serves mostly for such purposes.
|
||||
|
||||
### Optional callbacks
|
||||
|
||||
Optional callbacks are callbacks that callback modules may implement if they want to, but are not required to.
|
||||
Usually, behaviour modules know if they should call those callbacks based on configuration, or they check if the callbacks are defined with `function_exported?/3` or `macro_exported?/3`.
|
||||
Optional callbacks can be defined through the `@optional_callbacks` module attribute, which has to be a keyword list with function or macro name as key and arity as value. For example:
|
||||
|
||||
defmodule MyBehaviour do
|
||||
@callback vital_fun() :: any
|
||||
@callback non_vital_fun() :: any
|
||||
@macrocallback non_vital_macro(arg :: any) :: Macro.t
|
||||
@optional_callbacks non_vital_fun: 0, non_vital_macro: 1
|
||||
end
|
||||
|
||||
One example of optional callback in Elixir's standard library is `c:GenServer.format_status/2`.
|
||||
|
||||
## Implementing behaviours
|
||||
|
||||
To specify that a module implements a given behaviour, the `@behaviour` attribute must be used:
|
||||
|
||||
defmodule MyBehaviour do
|
||||
@callback my_fun(arg :: any) :: any
|
||||
end
|
||||
|
||||
defmodule MyCallbackModule do
|
||||
@behaviour MyBehaviour
|
||||
def my_fun(arg), do: arg
|
||||
end
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user